2 using System.Collections.Generic;
4 using System.ServiceModel;
5 using System.ServiceModel.Channels;
6 using System.ServiceModel.Description;
8 using System.Reflection;
9 using System.Threading;
10 using System.Configuration;
13 using MonoTests.stand_alone.WebHarness;
14 using System.ServiceModel.Dispatcher;
15 using System.Collections.ObjectModel;
17 using MonoTests.Helpers;
19 namespace MonoTests.Features
21 public class Configuration
23 static Configuration() {
24 var port = NetworkHelpers.FindFreePort ();
25 onlyServers = Boolean.Parse (ConfigurationManager.AppSettings ["onlyServers"] ?? "false");
26 onlyClients = Boolean.Parse (ConfigurationManager.AppSettings ["onlyClients"] ?? "false");
27 endpointBase = ConfigurationManager.AppSettings ["endpointBase"] ?? $"http://localhost:{port}/";
28 if (!endpointBase.EndsWith ("/"))
29 endpointBase = endpointBase + '/';
30 logMessages = Boolean.Parse (ConfigurationManager.AppSettings ["logMessages"] ?? "false");
32 public static bool onlyServers;
33 public static bool onlyClients;
34 public static string endpointBase;
35 public static bool logMessages;
36 public static bool IsLocal { get { return !onlyServers && !onlyClients; } }
39 class LoggerMessageInspector : IDispatchMessageInspector
41 #region IDispatchMessageInspector Members
43 public object AfterReceiveRequest (ref Message request, IClientChannel channel, InstanceContext instanceContext) {
44 Console.WriteLine ("****Begin message received by host:");
45 Console.WriteLine (request);
46 Console.WriteLine ("****End message received by host:");
50 public void BeforeSendReply (ref Message reply, object correlationState) {
51 Console.WriteLine ("****Begin message reply from host:");
52 Console.WriteLine (reply);
53 Console.WriteLine ("****End message reply from host:");
58 class LoggerBehavior : IServiceBehavior
61 #region IServiceBehavior Members
63 public void AddBindingParameters (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) {
67 public void ApplyDispatchBehavior (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
68 ChannelDispatcher dispatcher = serviceHostBase.ChannelDispatchers [0] as ChannelDispatcher;
69 dispatcher.Endpoints [0].DispatchRuntime.MessageInspectors.Add (new LoggerMessageInspector ());
72 public void Validate (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
79 public abstract class TestFixtureBase<TClient, TServer, IServer> where TClient : new() where TServer: new()
81 ServiceHost _hostBase;
82 ChannelFactory<IServer> factory;
84 protected TestFixtureBase () { }
87 public void TearDown ()
89 if (_hostBase != null)
96 public virtual void Run (){
97 bool runServer = true;
98 bool runClient = true;
99 if (Configuration.onlyClients)
101 if (Configuration.onlyServers)
103 Run (runServer, runClient);
106 public void CheckWsdlImpl () {
109 Assembly _assembly = Assembly.GetExecutingAssembly ();
110 StreamReader _stream = new StreamReader (_assembly.GetManifestResourceStream ("MonoTests.System.ServiceModel.Test.FeatureBased.Features.Contracts." + typeof (TServer).Name + ".xml"));
111 goldWsdl = _stream.ReadToEnd ();
114 Console.WriteLine ("Couldn't test WSDL of server " + typeof (TServer).Name + " because gold wsdl is not embedded in test !");
117 string currentWsdl = "";
119 HttpWebRequest myReq = (HttpWebRequest) WebRequest.Create (getMexEndpoint () + "?wsdl");
120 // Obtain a 'Stream' object associated with the response object.
121 WebResponse response = myReq.GetResponse ();
122 Stream ReceiveStream = response.GetResponseStream ();
124 Encoding encode = global::System.Text.Encoding.GetEncoding ("utf-8");
126 // Pipe the stream to a higher level stream reader with the required encoding format.
127 StreamReader readStream = new StreamReader (ReceiveStream, encode);
128 Console.WriteLine ("\nResponse stream received");
129 int maxLen = 10 * 1024;
130 Char [] read = new Char [maxLen];
132 // Read 256 charcters at a time.
133 int count = readStream.Read (read, 0, maxLen);
135 currentWsdl = currentWsdl + new String (read, 0, count);
136 count = readStream.Read (read, 0, 256);
141 XmlComparer comparer = new XmlComparer (XmlComparer.Flags.IgnoreAttribOrder, true);
142 Assert.IsTrue (comparer.AreEqual (goldWsdl, currentWsdl), "Service WSDL does not match gold WSDL");
146 protected void Run (bool runServer, bool runClient) {
149 _hostBase = InitializeServiceHost ();
157 return Configuration.endpointBase + typeof(TServer).Name;
160 public string getMexEndpoint ()
162 return getEndpoint () + "_wsdl"; // should be getEndpoint() but currently implementation is buggy
166 protected virtual TClient InitializeClient () {
167 //return new TClient(new BasicHttpBinding(), new EndpointAddress( getEndpoint) );
168 Type [] paramsTypes = new Type [] { typeof (Binding), typeof (EndpointAddress) };
169 object [] parameters = new object [] { new BasicHttpBinding (), new EndpointAddress (getEndpoint())};
171 ConstructorInfo info = typeof (TClient).GetConstructor (paramsTypes);
172 return (TClient) info.Invoke (parameters);
175 public TClient ClientProxy {
178 _client = InitializeClient ();
183 public IServer Client {
185 factory = new ChannelFactory<IServer> (new BasicHttpBinding (), new EndpointAddress (getEndpoint ()));
186 return factory.CreateChannel ();
190 protected virtual ServiceHost InitializeServiceHost () {
191 ServiceHost host = new ServiceHost(typeof(TServer));
192 host.AddServiceEndpoint(typeof(IServer), new BasicHttpBinding(), getEndpoint());
193 ServiceMetadataBehavior smb = new ServiceMetadataBehavior ();
194 smb.HttpGetEnabled = true;
195 smb.HttpGetUrl = new Uri (getMexEndpoint ());
196 host.Description.Behaviors.Add (smb);
197 host.Description.Behaviors.Add (new ServiceThrottlingBehavior () { MaxConcurrentCalls = 1, MaxConcurrentSessions = 1 });
198 if (Configuration.logMessages)
199 host.Description.Behaviors.Add (new LoggerBehavior ());
204 protected ServiceHost Host {
211 protected virtual void Close () {
212 if (!Configuration.onlyClients && !Configuration.onlyServers && Host.State == CommunicationState.Opened)