TestFixtureBase.cs 6.6 KB

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