2 // ServiceHostBaseTest.cs
5 // Igor Zelmanovich <igorz@mainsoft.com>
7 // Copyright (C) 2008 Mainsoft, Inc. http://www.mainsoft.com
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections.Generic;
32 using NUnit.Framework;
33 using System.ServiceModel;
34 using System.ServiceModel.Description;
35 using System.ServiceModel.Dispatcher;
36 using SMMessage = System.ServiceModel.Channels.Message;
37 using System.ServiceModel.Channels;
39 namespace MonoTests.System.ServiceModel
42 public class ServiceHostBaseTest
44 class Poker : ServiceHostBase
46 public event EventHandler OnApplyConfiguration;
48 protected override ServiceDescription CreateDescription (out IDictionary<string, ContractDescription> implementedContracts) {
49 implementedContracts = new Dictionary<string, ContractDescription> ();
50 ServiceDescription description = new ServiceDescription ();
51 description.ServiceType = typeof (MyService);
52 description.Behaviors.Add (new ServiceBehaviorAttribute ());
56 protected override void ApplyConfiguration () {
57 if (OnApplyConfiguration != null)
58 OnApplyConfiguration (this, EventArgs.Empty);
59 base.ApplyConfiguration ();
62 public void CallInitializeDescription () {
63 InitializeDescription (new UriSchemeKeyedCollection ());
66 protected override void InitializeRuntime () {
67 base.InitializeRuntime ();
70 public void CallInitializeRuntime () {
74 public void DoAddBaseAddress (Uri uri)
82 Poker host = new Poker ();
84 Assert.AreEqual (null, host.Description, "Description");
85 Assert.AreEqual (null, host.Authorization, "Authorization");
89 public void DefaultConfiguration () {
90 Poker host = new Poker ();
91 host.OnApplyConfiguration += delegate (object sender, EventArgs e) {
92 Assert.AreEqual (1, host.Description.Behaviors.Count, "Description.Behaviors.Count #1");
94 host.CallInitializeDescription ();
96 Assert.AreEqual (true, host.Description.Behaviors.Count > 1, "Description.Behaviors.Count #2");
98 Assert.IsNotNull (host.Description.Behaviors.Find<ServiceDebugBehavior> (), "ServiceDebugBehavior");
99 Assert.IsNotNull (host.Description.Behaviors.Find<ServiceAuthorizationBehavior> (), "ServiceDebugBehavior");
100 Assert.IsNotNull (host.Authorization, "Authorization #1");
102 Assert.AreEqual (host.Description.Behaviors.Find<ServiceAuthorizationBehavior> (), host.Authorization, "Authorization #2");
106 [ExpectedException (typeof (InvalidOperationException))]
107 public void ApplyConfigurationNoDescription () {
108 CustomServiceHost customHost = new CustomServiceHost ();
109 customHost.ApplyConfiguration ();
112 class CustomServiceHost : ServiceHostBase
115 public CustomServiceHost () {
119 public new void ApplyConfiguration () {
120 base.ApplyConfiguration ();
123 protected override ServiceDescription CreateDescription (out IDictionary<string, ContractDescription> implementedContracts) {
124 throw new NotImplementedException ();
129 public void InitializeRuntime () {
130 Poker host = new Poker ();
131 host.CallInitializeDescription ();
132 EndpointAddress address = new EndpointAddress ("http://localhost:8090/");
133 ContractDescription contract = ContractDescription.GetContract (typeof (IMyContract));
134 ServiceEndpoint endpoint = new ServiceEndpoint (contract, new BasicHttpBinding (), address);
135 endpoint.ListenUri = address.Uri;
136 host.Description.Endpoints.Add (endpoint);
138 Assert.AreEqual (0, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #1");
140 host.CallInitializeRuntime ();
142 Assert.AreEqual (1, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #1");
143 Assert.AreEqual (CommunicationState.Created, host.ChannelDispatchers [0].State, "ChannelDispatchers.Count #1");
147 interface IMyContract
153 class MyService : IMyContract
155 public string GetData () {
156 return "Hello World";
161 public void ChannelDispatchers_NoDebug () {
162 ServiceHost h = new ServiceHost (typeof (AllActions), new Uri ("http://localhost:8080"));
163 h.AddServiceEndpoint (typeof (AllActions).FullName, new BasicHttpBinding (), "address");
165 ServiceDebugBehavior b = h.Description.Behaviors.Find<ServiceDebugBehavior> ();
166 b.HttpHelpPageEnabled = false;
170 Assert.AreEqual (h.ChannelDispatchers.Count, 1);
171 ChannelDispatcher channelDispatcher = h.ChannelDispatchers[0] as ChannelDispatcher;
172 Assert.IsNotNull (channelDispatcher, "#1");
173 Assert.IsTrue (channelDispatcher.Endpoints.Count == 1, "#2");
174 EndpointAddressMessageFilter filter = channelDispatcher.Endpoints [0].AddressFilter as EndpointAddressMessageFilter;
175 Assert.IsNotNull (filter, "#3");
176 Assert.IsTrue (filter.Address.Equals (new EndpointAddress ("http://localhost:8080/address")), "#4");
177 Assert.IsFalse (filter.IncludeHostNameInComparison, "#5");
178 Assert.IsTrue (channelDispatcher.Endpoints [0].ContractFilter is MatchAllMessageFilter, "#6");
185 public void ChannelDispatchers_WithDebug () {
186 ServiceHost h = new ServiceHost (typeof (AllActions), new Uri ("http://localhost:8080"));
187 h.AddServiceEndpoint (typeof (AllActions).FullName, new BasicHttpBinding (), "address");
188 ServiceMetadataBehavior b = new ServiceMetadataBehavior ();
189 b.HttpGetEnabled = true;
190 b.HttpGetUrl = new Uri( "http://localhost:8080" );
191 h.Description.Behaviors.Add (b);
194 Assert.AreEqual (h.ChannelDispatchers.Count, 2, "#1");
195 ChannelDispatcher channelDispatcher = h.ChannelDispatchers[1] as ChannelDispatcher;
196 Assert.IsNotNull (channelDispatcher, "#2");
197 Assert.IsTrue (channelDispatcher.Endpoints.Count == 1, "#3");
198 EndpointAddressMessageFilter filter = channelDispatcher.Endpoints [0].AddressFilter as EndpointAddressMessageFilter;
199 Assert.IsNotNull (filter, "#4");
200 Assert.IsTrue (filter.Address.Equals (new EndpointAddress ("http://localhost:8080")), "#5");
201 Assert.IsFalse (filter.IncludeHostNameInComparison, "#6");
202 Assert.IsTrue (channelDispatcher.Endpoints [0].ContractFilter is MatchAllMessageFilter, "#7");
207 public void SpecificActionTest ()
209 //EndpointDispatcher d = new EndpointDispatcher(
210 ServiceHost h = new ServiceHost (typeof (SpecificAction), new Uri ("http://localhost:8080"));
211 h.AddServiceEndpoint (typeof (Action1Interface), new BasicHttpBinding (), "address");
214 ChannelDispatcher d = h.ChannelDispatchers [0] as ChannelDispatcher;
215 EndpointDispatcher ed = d.Endpoints [0] as EndpointDispatcher;
216 ActionMessageFilter actionFilter = ed.ContractFilter as ActionMessageFilter;
217 Assert.IsNotNull (actionFilter, "#1");
218 Assert.IsTrue (actionFilter.Actions.Count == 1, "#2");
223 public void InitializeRuntimeBehaviors1 () {
224 HostState st = new HostState ();
225 ServiceHost h = new ServiceHost (typeof (SpecificAction2), new Uri ("http://localhost:8080"));
226 h.AddServiceEndpoint (typeof (SpecificAction2), new BasicHttpBinding (), "temp");
228 h.Description.Behaviors.Add (new MyServiceBehavior (st, h));
230 h.Description.Endpoints [0].Behaviors.Add (new MyEndpointBehavior (st, h));
231 h.Description.Endpoints [0].Contract.Behaviors.Add (new MyContractBehavior (st, h));
232 h.Description.Endpoints [0].Contract.Operations [0].Behaviors.Add (new MyOperationBehavior (st, h));
237 string expected = "Start, IServiceBehavior.Validate, IContractBehavior.Validate, IEndpointBehavior.Validate, IOperationBehavior.ApplyDispatchBehavior, IServiceBehavior.AddBindingParameters, IContractBehavior.AddBindingParameters, IEndpointBehavior.AddBindingParameters, IOperationBehavior.AddBindingParameters, IServiceBehavior.ApplyDispatchBehavior, IContractBehavior.ApplyDispatchBehavior, IEndpointBehavior.ApplyDispatchBehavior, IOperationBehavior.ApplyDispatchBehavior";
238 Assert.AreEqual (expected, st.CurrentStage);
242 public void InitializeRuntimeBehaviors2 () {
243 HostState st = new HostState ();
244 ServiceHost h = new ServiceHost (typeof (SpecificAction), new Uri ("http://localhost:8080"));
245 h.AddServiceEndpoint (typeof (Action1Interface), new BasicHttpBinding (), "temp");
246 h.AddServiceEndpoint (typeof (Action2Interface), new BasicHttpBinding (), "temp2");
248 h.Description.Behaviors.Add (new MyServiceBehavior (st, h));
250 h.Description.Endpoints [0].Behaviors.Add (new MyEndpointBehavior (st, h));
251 h.Description.Endpoints [0].Contract.Behaviors.Add (new MyContractBehavior (st, h));
252 h.Description.Endpoints [0].Contract.Operations [0].Behaviors.Add (new MyOperationBehavior (st, h));
254 h.Description.Endpoints [1].Behaviors.Add (new MyEndpointBehavior (st, h));
255 h.Description.Endpoints [1].Contract.Behaviors.Add (new MyContractBehavior (st, h));
256 h.Description.Endpoints [1].Contract.Operations [0].Behaviors.Add (new MyOperationBehavior (st, h));
260 string expected = "Start, IServiceBehavior.Validate, IContractBehavior.Validate, IEndpointBehavior.Validate, IOperationBehavior.ApplyDispatchBehavior, IContractBehavior.Validate, IEndpointBehavior.Validate, IOperationBehavior.ApplyDispatchBehavior, IServiceBehavior.AddBindingParameters, IContractBehavior.AddBindingParameters, IEndpointBehavior.AddBindingParameters, IOperationBehavior.AddBindingParameters, IServiceBehavior.AddBindingParameters, IContractBehavior.AddBindingParameters, IEndpointBehavior.AddBindingParameters, IOperationBehavior.AddBindingParameters, IServiceBehavior.ApplyDispatchBehavior, IContractBehavior.ApplyDispatchBehavior, IEndpointBehavior.ApplyDispatchBehavior, IOperationBehavior.ApplyDispatchBehavior, IContractBehavior.ApplyDispatchBehavior, IEndpointBehavior.ApplyDispatchBehavior, IOperationBehavior.ApplyDispatchBehavior";
261 Assert.AreEqual (expected, st.CurrentStage);
265 public void AddBaseAddress ()
267 var host = new Poker ();
268 Assert.AreEqual (0, host.BaseAddresses.Count, "#1");
269 host.DoAddBaseAddress (new Uri ("http://localhost:37564"));
270 Assert.AreEqual (1, host.BaseAddresses.Count, "#1");
271 host.DoAddBaseAddress (new Uri ("net.tcp://localhost:893"));
272 Assert.AreEqual (2, host.BaseAddresses.Count, "#1");
276 [ExpectedException (typeof (ArgumentException))]
277 public void AddBaseAddress2 ()
279 var host = new Poker ();
280 Assert.AreEqual (0, host.BaseAddresses.Count, "#1");
281 host.DoAddBaseAddress (new Uri ("http://localhost:37564"));
282 // http base address is already added.
283 host.DoAddBaseAddress (new Uri ("http://localhost:893"));
287 public void AddServiceEndpointUri ()
289 var host = new ServiceHost (typeof (AllActions),
290 new Uri ("http://localhost:37564"));
291 var se = host.AddServiceEndpoint (typeof (AllActions),
292 new BasicHttpBinding (), "foobar");
293 Assert.AreEqual ("http://localhost:37564/foobar", se.Address.Uri.AbsoluteUri, "#1");
294 Assert.AreEqual ("http://localhost:37564/foobar", se.ListenUri.AbsoluteUri, "#2");
298 public void AddServiceEndpointUri2 ()
300 var host = new ServiceHost (typeof (AllActions),
301 new Uri ("http://localhost:37564"));
302 var se = host.AddServiceEndpoint (typeof (AllActions),
303 new BasicHttpBinding (), String.Empty);
304 Assert.AreEqual ("http://localhost:37564/", se.Address.Uri.AbsoluteUri, "#1");
305 Assert.AreEqual ("http://localhost:37564/", se.ListenUri.AbsoluteUri, "#2");
309 [ExpectedException (typeof (InvalidOperationException))]
310 public void AddServiceEndpointOnlyMex ()
312 var host = new ServiceHost (typeof (AllActions),
313 new Uri ("http://localhost:37564"));
314 host.Description.Behaviors.Add (new ServiceMetadataBehavior ());
315 host.AddServiceEndpoint ("IMetadataExchange",
316 new BasicHttpBinding (), "/wsdl");
319 // to make sure that throwing IOE from here does not count.
323 Assert.Fail ("should not open");
327 public void RunDestinationUnreachableTest ()
329 RunDestinationUnreachableTest ("BasicHttp", new BasicHttpBinding ());
333 public void RunDestinationUnreachableTest2 ()
335 RunDestinationUnreachableTest ("CustomSoap12", new CustomBinding (new HttpTransportBindingElement ()));
338 void RunDestinationUnreachableTest (string label, Binding binding)
340 string address = "http://localhost:37564/";
341 var host = OpenHost (address, binding);
344 var client = new DestinationUnreachableClient (binding, address);
345 client.NotImplementedOperation ();
346 Assert.Fail (label + " ActionNotSupportedException is expected");
347 } catch (ActionNotSupportedException) {
348 // catching it instead of ExpectedException to distinguish errors at service side.
354 ServiceHost OpenHost (string address, Binding binding)
356 var baseAddresses = new Uri[] { new Uri(address) };
358 var host = new ServiceHost (typeof (DummyService), baseAddresses);
359 host.AddServiceEndpoint (typeof (IDummyService), binding, new Uri ("", UriKind.Relative));
366 [ExpectedException (typeof (InvalidOperationException))]
367 public void AddServiceEndpoint_Directly_ContractMismatch ()
369 var host = new ServiceHost (typeof (DummyService));
370 var address = new EndpointAddress ("http://localhost:8080");
371 var binding = new BasicHttpBinding ();
372 var contract = ContractDescription.GetContract (typeof (INotImplementedService));
373 host.AddServiceEndpoint (new ServiceEndpoint (contract, binding, address));
383 public class HostState
385 public string CurrentStage = "Start";
388 public class MyServiceBehavior : IServiceBehavior
390 #region IServiceBehavior Members
394 public MyServiceBehavior (HostState state, ServiceHost h) {
399 public void AddBindingParameters (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, global::System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) {
400 _state.CurrentStage += ", IServiceBehavior.AddBindingParameters";
401 bindingParameters.Add (this);
404 public void ApplyDispatchBehavior (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
405 _state.CurrentStage += ", IServiceBehavior.ApplyDispatchBehavior";
408 public void Validate (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
409 _state.CurrentStage += ", IServiceBehavior.Validate";
410 Assert.AreEqual (_host.ChannelDispatchers.Count, 0);
416 public class MyEndpointBehavior : IEndpointBehavior
418 #region IEndpointBehavior Members
421 public MyEndpointBehavior (HostState state, ServiceHost h) {
426 public void AddBindingParameters (ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) {
427 Console.WriteLine ("IEndpointBehavior - AddBindingParameters " + _host.ChannelDispatchers.Count);
428 _state.CurrentStage += ", IEndpointBehavior.AddBindingParameters";
429 bindingParameters.Add (this);
432 public void ApplyClientBehavior (ServiceEndpoint endpoint, ClientRuntime clientRuntime) {
433 Console.WriteLine ("IEndpointBehavior - ApplyClientBehavior " + _host.ChannelDispatchers.Count);
436 public void ApplyDispatchBehavior (ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) {
437 Console.WriteLine ("IEndpointBehavior - ApplyDispatchBehavior " + _host.ChannelDispatchers.Count);
438 _state.CurrentStage += ", IEndpointBehavior.ApplyDispatchBehavior";
441 public void Validate (ServiceEndpoint endpoint) {
442 Console.WriteLine ("IEndpointBehavior - Validate " + _host.ChannelDispatchers.Count);
443 _state.CurrentStage += ", IEndpointBehavior.Validate";
444 Assert.AreEqual (_host.ChannelDispatchers.Count, 0);
450 public class MyContractBehavior : IContractBehavior
452 #region IContractBehavior Members
455 public MyContractBehavior (HostState state, ServiceHost h) {
460 public void AddBindingParameters (ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) {
461 Console.WriteLine ("Contract - AddBindingParameters " + _host.ChannelDispatchers.Count);
462 _state.CurrentStage += ", IContractBehavior.AddBindingParameters";
463 bindingParameters.Add (this);
466 public void ApplyClientBehavior (ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime) {
467 Console.WriteLine ("Contract - ApplyClientBehavior " + _host.ChannelDispatchers.Count);
470 public void ApplyDispatchBehavior (ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime) {
471 Console.WriteLine ("Contract - ApplyDispatchBehavior " + _host.ChannelDispatchers.Count);
472 _state.CurrentStage += ", IContractBehavior.ApplyDispatchBehavior";
475 public void Validate (ContractDescription contractDescription, ServiceEndpoint endpoint) {
476 Console.WriteLine ("Contract - Validate " + _host.ChannelDispatchers.Count);
477 _state.CurrentStage += ", IContractBehavior.Validate";
478 Assert.AreEqual (_host.ChannelDispatchers.Count, 0);
484 public class MyOperationBehavior : IOperationBehavior
486 #region IOperationBehavior Members
489 public MyOperationBehavior (HostState state, ServiceHost h) {
494 public void AddBindingParameters (OperationDescription operationDescription, BindingParameterCollection bindingParameters) {
495 Console.WriteLine ("IOperationBehavior - AddBindingParameters " + _host.ChannelDispatchers.Count);
496 _state.CurrentStage += ", IOperationBehavior.AddBindingParameters";
497 bindingParameters.Add (this);
500 public void ApplyClientBehavior (OperationDescription operationDescription, ClientOperation clientOperation) {
501 Console.WriteLine ("IOperationBehavior - ApplyClientBehavior " + _host.ChannelDispatchers.Count);
504 public void ApplyDispatchBehavior (OperationDescription operationDescription, DispatchOperation dispatchOperation) {
505 Console.WriteLine ("IOperationBehavior - ApplyDispatchBehavior " + _host.ChannelDispatchers.Count);
506 _state.CurrentStage += ", IOperationBehavior.ApplyDispatchBehavior";
509 public void Validate (OperationDescription operationDescription) {
510 Console.WriteLine ("IOperationBehavior - Validate " + _host.ChannelDispatchers.Count);
511 _state.CurrentStage += ", IOperationBehavior.ApplyDispatchBehavior";
512 Assert.AreEqual (_host.ChannelDispatchers.Count, 0);
521 [OperationContract (Action = "*", ReplyAction = "*")]
522 public SMMessage Get (SMMessage req) {
528 interface Action1Interface
530 [OperationContract (Action = "Specific1", ReplyAction = "*")]
531 SMMessage GetMessage1 (SMMessage req);
535 interface Action2Interface
537 [OperationContract (Action = "Specific2", ReplyAction = "*")]
538 SMMessage GetMessage2 (SMMessage req);
541 class SpecificAction : Action1Interface, Action2Interface
543 public SMMessage GetMessage1 (SMMessage req) {
547 public SMMessage GetMessage2 (SMMessage req) {
553 class SpecificAction2
555 [OperationContract (Action = "Specific", ReplyAction = "*")]
556 public SMMessage GetMessage1 (SMMessage req) {
561 class MyChannelDispatcher : ChannelDispatcher
563 public bool Attached = false;
565 public MyChannelDispatcher (IChannelListener l) : base (l) { }
566 protected override void Attach (ServiceHostBase host) {
572 class MyChannelListener : IChannelListener
574 #region IChannelListener Members
576 public IAsyncResult BeginWaitForChannel (TimeSpan timeout, AsyncCallback callback, object state) {
577 throw new NotImplementedException ();
580 public bool EndWaitForChannel (IAsyncResult result) {
581 throw new NotImplementedException ();
584 public T GetProperty<T> () where T : class {
585 throw new NotImplementedException ();
589 get { throw new NotImplementedException (); }
592 public bool WaitForChannel (TimeSpan timeout) {
593 throw new NotImplementedException ();
598 #region ICommunicationObject Members
600 public void Abort () {
601 throw new NotImplementedException ();
604 public IAsyncResult BeginClose (TimeSpan timeout, AsyncCallback callback, object state) {
605 throw new NotImplementedException ();
608 public IAsyncResult BeginClose (AsyncCallback callback, object state) {
609 throw new NotImplementedException ();
612 public IAsyncResult BeginOpen (TimeSpan timeout, AsyncCallback callback, object state) {
613 throw new NotImplementedException ();
616 public IAsyncResult BeginOpen (AsyncCallback callback, object state) {
617 throw new NotImplementedException ();
620 public void Close (TimeSpan timeout) {
621 throw new NotImplementedException ();
624 public void Close () {
625 throw new NotImplementedException ();
628 public event EventHandler Closed;
630 public event EventHandler Closing;
632 public void EndClose (IAsyncResult result) {
633 throw new NotImplementedException ();
636 public void EndOpen (IAsyncResult result) {
637 throw new NotImplementedException ();
640 public event EventHandler Faulted;
642 public void Open (TimeSpan timeout) {
643 throw new NotImplementedException ();
646 public void Open () {
647 throw new NotImplementedException ();
650 public event EventHandler Opened;
652 public event EventHandler Opening;
654 public CommunicationState State {
655 get { throw new NotImplementedException (); }
662 public interface IDummyService
665 void DummyOperation ();
667 public class DummyService : IDummyService
669 public void DummyOperation ()
675 public interface INotImplementedService
678 void NotImplementedOperation ();
680 public class DestinationUnreachableClient : ClientBase<INotImplementedService>, INotImplementedService
682 public void NotImplementedOperation ()
684 Channel.NotImplementedOperation ();
687 public DestinationUnreachableClient (Binding binding, string address)
688 : base (binding, new EndpointAddress (address))