TestFixtureBase.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.ServiceModel;
  5. using System.ServiceModel.Channels;
  6. using System.ServiceModel.Description;
  7. using NUnit.Framework;
  8. using System.Reflection;
  9. using System.Threading;
  10. using System.Configuration;
  11. using System.IO;
  12. using System.Net;
  13. using MonoTests.stand_alone.WebHarness;
  14. using System.ServiceModel.Dispatcher;
  15. using System.Collections.ObjectModel;
  16. using MonoTests.Helpers;
  17. namespace MonoTests.Features
  18. {
  19. public class Configuration
  20. {
  21. static Configuration() {
  22. var port = NetworkHelpers.FindFreePort ();
  23. onlyServers = Boolean.Parse (ConfigurationManager.AppSettings ["onlyServers"] ?? "false");
  24. onlyClients = Boolean.Parse (ConfigurationManager.AppSettings ["onlyClients"] ?? "false");
  25. endpointBase = ConfigurationManager.AppSettings ["endpointBase"] ?? $"http://localhost:{port}/";
  26. if (!endpointBase.EndsWith ("/"))
  27. endpointBase = endpointBase + '/';
  28. logMessages = Boolean.Parse (ConfigurationManager.AppSettings ["logMessages"] ?? "false");
  29. }
  30. public static bool onlyServers;
  31. public static bool onlyClients;
  32. public static string endpointBase;
  33. public static bool logMessages;
  34. public static bool IsLocal { get { return !onlyServers && !onlyClients; } }
  35. }
  36. class LoggerMessageInspector : IDispatchMessageInspector
  37. {
  38. #region IDispatchMessageInspector Members
  39. public object AfterReceiveRequest (ref Message request, IClientChannel channel, InstanceContext instanceContext) {
  40. Console.WriteLine ("****Begin message received by host:");
  41. Console.WriteLine (request);
  42. Console.WriteLine ("****End message received by host:");
  43. return new object ();
  44. }
  45. public void BeforeSendReply (ref Message reply, object correlationState) {
  46. Console.WriteLine ("****Begin message reply from host:");
  47. Console.WriteLine (reply);
  48. Console.WriteLine ("****End message reply from host:");
  49. }
  50. #endregion
  51. }
  52. class LoggerBehavior : IServiceBehavior
  53. {
  54. #region IServiceBehavior Members
  55. public void AddBindingParameters (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) {
  56. }
  57. public void ApplyDispatchBehavior (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
  58. ChannelDispatcher dispatcher = serviceHostBase.ChannelDispatchers [0] as ChannelDispatcher;
  59. dispatcher.Endpoints [0].DispatchRuntime.MessageInspectors.Add (new LoggerMessageInspector ());
  60. }
  61. public void Validate (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
  62. }
  63. #endregion
  64. }
  65. public abstract class TestFixtureBase<TClient, TServer, IServer> where TClient : new() where TServer: new()
  66. {
  67. ServiceHost _hostBase;
  68. ChannelFactory<IServer> factory;
  69. protected TestFixtureBase () { }
  70. [TearDown]
  71. public void TearDown ()
  72. {
  73. if (_hostBase != null)
  74. _hostBase.Close ();
  75. if (factory != null)
  76. factory.Close ();
  77. }
  78. [SetUp]
  79. public virtual void Run (){
  80. bool runServer = true;
  81. bool runClient = true;
  82. if (Configuration.onlyClients)
  83. runServer = false;
  84. if (Configuration.onlyServers)
  85. runClient = false;
  86. Run (runServer, runClient);
  87. }
  88. public void CheckWsdlImpl () {
  89. string goldWsdl;
  90. try {
  91. Assembly _assembly = Assembly.GetExecutingAssembly ();
  92. StreamReader _stream = new StreamReader (_assembly.GetManifestResourceStream ("MonoTests.System.ServiceModel.Test.FeatureBased.Features.Contracts." + typeof (TServer).Name + ".xml"));
  93. goldWsdl = _stream.ReadToEnd ();
  94. }
  95. catch {
  96. Console.WriteLine ("Couldn't test WSDL of server " + typeof (TServer).Name + " because gold wsdl is not embedded in test !");
  97. return;
  98. }
  99. string currentWsdl = "";
  100. HttpWebRequest myReq = (HttpWebRequest) WebRequest.Create (getMexEndpoint () + "?wsdl");
  101. // Obtain a 'Stream' object associated with the response object.
  102. WebResponse response = myReq.GetResponse ();
  103. Stream ReceiveStream = response.GetResponseStream ();
  104. Encoding encode = global::System.Text.Encoding.GetEncoding ("utf-8");
  105. // Pipe the stream to a higher level stream reader with the required encoding format.
  106. StreamReader readStream = new StreamReader (ReceiveStream, encode);
  107. Console.WriteLine ("\nResponse stream received");
  108. int maxLen = 10 * 1024;
  109. Char [] read = new Char [maxLen];
  110. // Read 256 charcters at a time.
  111. int count = readStream.Read (read, 0, maxLen);
  112. while (count > 0) {
  113. currentWsdl = currentWsdl + new String (read, 0, count);
  114. count = readStream.Read (read, 0, 256);
  115. }
  116. readStream.Close ();
  117. response.Close ();
  118. XmlComparer comparer = new XmlComparer (XmlComparer.Flags.IgnoreAttribOrder, true);
  119. Assert.IsTrue (comparer.AreEqual (goldWsdl, currentWsdl), "Service WSDL does not match gold WSDL");
  120. }
  121. protected void Run (bool runServer, bool runClient) {
  122. if (runServer) {
  123. _hostBase = InitializeServiceHost ();
  124. _hostBase.Open ();
  125. }
  126. }
  127. string getEndpoint()
  128. {
  129. return Configuration.endpointBase + typeof(TServer).Name;
  130. }
  131. public string getMexEndpoint ()
  132. {
  133. return getEndpoint () + "_wsdl"; // should be getEndpoint() but currently implementation is buggy
  134. }
  135. TClient _client;
  136. protected virtual TClient InitializeClient () {
  137. //return new TClient(new BasicHttpBinding(), new EndpointAddress( getEndpoint) );
  138. Type [] paramsTypes = new Type [] { typeof (Binding), typeof (EndpointAddress) };
  139. object [] parameters = new object [] { new BasicHttpBinding (), new EndpointAddress (getEndpoint())};
  140. ConstructorInfo info = typeof (TClient).GetConstructor (paramsTypes);
  141. return (TClient) info.Invoke (parameters);
  142. }
  143. public TClient ClientProxy {
  144. get {
  145. if (_client == null)
  146. _client = InitializeClient ();
  147. return _client;
  148. }
  149. }
  150. public IServer Client {
  151. get {
  152. factory = new ChannelFactory<IServer> (new BasicHttpBinding (), new EndpointAddress (getEndpoint ()));
  153. return factory.CreateChannel ();
  154. }
  155. }
  156. protected virtual ServiceHost InitializeServiceHost () {
  157. ServiceHost host = new ServiceHost(typeof(TServer));
  158. host.AddServiceEndpoint(typeof(IServer), new BasicHttpBinding(), getEndpoint());
  159. ServiceMetadataBehavior smb = new ServiceMetadataBehavior ();
  160. smb.HttpGetEnabled = true;
  161. smb.HttpGetUrl = new Uri (getMexEndpoint ());
  162. host.Description.Behaviors.Add (smb);
  163. host.Description.Behaviors.Add (new ServiceThrottlingBehavior () { MaxConcurrentCalls = 1, MaxConcurrentSessions = 1 });
  164. if (Configuration.logMessages)
  165. host.Description.Behaviors.Add (new LoggerBehavior ());
  166. return host;
  167. }
  168. protected ServiceHost Host {
  169. get {
  170. return _hostBase;
  171. }
  172. }
  173. [TearDown]
  174. protected virtual void Close () {
  175. if (!Configuration.onlyClients && !Configuration.onlyServers && Host.State == CommunicationState.Opened)
  176. Host.Close ();
  177. }
  178. }
  179. }