[System/ServiceModel] Fix more hardcoded test ports
[mono.git] / mcs / class / System.ServiceModel / Test / System.ServiceModel / ServiceHostBaseTest.cs
1 //
2 // ServiceHostBaseTest.cs
3 //
4 // Author:
5 //      Igor Zelmanovich <igorz@mainsoft.com>
6 //
7 // Copyright (C) 2008 Mainsoft, Inc.  http://www.mainsoft.com
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28
29 using System;
30 using System.Collections.Generic;
31 using System.Text;
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;
38
39 using MonoTests.Helpers;
40
41 namespace MonoTests.System.ServiceModel
42 {
43         [TestFixture]
44         public class ServiceHostBaseTest
45         {
46                 class Poker : ServiceHostBase
47                 {
48                         public event EventHandler OnApplyConfiguration;
49
50                         protected override ServiceDescription CreateDescription (out IDictionary<string, ContractDescription> implementedContracts) {
51                                 implementedContracts = new Dictionary<string, ContractDescription> ();
52                                 ServiceDescription description = new ServiceDescription ();
53                                 description.ServiceType = typeof (MyService);
54                                 description.Behaviors.Add (new ServiceBehaviorAttribute ());
55                                 return description;
56                         }
57
58                         protected override void ApplyConfiguration () {
59                                 if (OnApplyConfiguration != null)
60                                         OnApplyConfiguration (this, EventArgs.Empty);
61                                 base.ApplyConfiguration ();
62                         }
63
64                         public void CallInitializeDescription () {
65                                 InitializeDescription (new UriSchemeKeyedCollection ());
66                         }
67
68                         protected override void InitializeRuntime () {
69                                 base.InitializeRuntime ();
70                         }
71
72                         public void CallInitializeRuntime () {
73                                 InitializeRuntime ();
74                         }
75
76                         public void DoAddBaseAddress (Uri uri)
77                         {
78                                 AddBaseAddress (uri);
79                         }
80                 }
81
82                 [Test]
83                 public void Ctor () {
84                         Poker host = new Poker ();
85
86                         Assert.AreEqual (null, host.Description, "Description");
87                         Assert.AreEqual (null, host.Authorization, "Authorization");
88                 }
89
90                 [Test]
91                 public void DefaultConfiguration () {
92                         Poker host = new Poker ();
93                         host.OnApplyConfiguration += delegate (object sender, EventArgs e) {
94                                 Assert.AreEqual (1, host.Description.Behaviors.Count, "Description.Behaviors.Count #1");
95                         };
96                         host.CallInitializeDescription ();
97
98                         Assert.AreEqual (true, host.Description.Behaviors.Count > 1, "Description.Behaviors.Count #2");
99
100                         Assert.IsNotNull (host.Description.Behaviors.Find<ServiceDebugBehavior> (), "ServiceDebugBehavior");
101                         Assert.IsNotNull (host.Description.Behaviors.Find<ServiceAuthorizationBehavior> (), "ServiceDebugBehavior");
102                         Assert.IsNotNull (host.Authorization, "Authorization #1");
103
104                         Assert.AreEqual (host.Description.Behaviors.Find<ServiceAuthorizationBehavior> (), host.Authorization, "Authorization #2");
105                 }
106
107                 [Test]
108                 [ExpectedException (typeof (InvalidOperationException))]
109                 public void ApplyConfigurationNoDescription () {
110                         CustomServiceHost customHost = new CustomServiceHost ();
111                         customHost.ApplyConfiguration ();
112                 }
113
114                 class CustomServiceHost : ServiceHostBase
115                 {
116
117                         public CustomServiceHost () {
118
119                         }
120
121                         public new void ApplyConfiguration () {
122                                 base.ApplyConfiguration ();
123                         }
124
125                         protected override ServiceDescription CreateDescription (out IDictionary<string, ContractDescription> implementedContracts) {
126                                 throw new NotImplementedException ();
127                         }
128                 }
129
130                 [Test]
131                 public void InitializeRuntime () {
132                         Poker host = new Poker ();
133                         host.CallInitializeDescription ();
134                         var port = NetworkHelpers.FindFreePort ();
135                         EndpointAddress address = new EndpointAddress ("http://localhost:" + port + "/");
136                         ContractDescription contract = ContractDescription.GetContract (typeof (IMyContract));
137                         ServiceEndpoint endpoint = new ServiceEndpoint (contract, new BasicHttpBinding (), address);
138                         endpoint.ListenUri = address.Uri;
139                         host.Description.Endpoints.Add (endpoint);
140
141                         Assert.AreEqual (0, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #1");
142
143                         host.CallInitializeRuntime ();
144
145                         Assert.AreEqual (1, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #1");
146                         Assert.AreEqual (CommunicationState.Created, host.ChannelDispatchers [0].State, "ChannelDispatchers.Count #1");
147                 }
148
149                 [ServiceContract]
150                 interface IMyContract
151                 {
152                         [OperationContract]
153                         string GetData ();
154                 }
155
156                 class MyService : IMyContract
157                 {
158                         public string GetData () {
159                                 return "Hello World";
160                         }
161                 }
162
163                 [Test]
164                 public void ChannelDispatchers_NoDebug () {
165                         var ep = "http://" + NetworkHelpers.LocalEphemeralEndPoint().ToString();
166                         ServiceHost h = new ServiceHost (typeof (AllActions), new Uri (ep));
167                         h.AddServiceEndpoint (typeof (AllActions).FullName, new BasicHttpBinding (), "address");
168
169                         ServiceDebugBehavior b = h.Description.Behaviors.Find<ServiceDebugBehavior> ();
170                         b.HttpHelpPageEnabled = false;                                          
171
172                         h.Open ();
173                         try {
174                         Assert.AreEqual (h.ChannelDispatchers.Count, 1);
175                         ChannelDispatcher channelDispatcher =  h.ChannelDispatchers[0] as ChannelDispatcher;
176                         Assert.IsNotNull (channelDispatcher, "#1");
177                         Assert.IsTrue (channelDispatcher.Endpoints.Count == 1, "#2");
178                         EndpointAddressMessageFilter filter = channelDispatcher.Endpoints [0].AddressFilter as EndpointAddressMessageFilter;
179                         Assert.IsNotNull (filter, "#3");
180                         Assert.IsTrue (filter.Address.Equals (new EndpointAddress (ep + "/address")), "#4");
181                         Assert.IsFalse (filter.IncludeHostNameInComparison, "#5");
182                         Assert.IsTrue (channelDispatcher.Endpoints [0].ContractFilter is MatchAllMessageFilter, "#6");
183                         } finally {
184                         h.Close ();
185                         }
186                 }
187
188                 [Test]
189                 public void ChannelDispatchers_WithDebug () {
190                         var ep = "http://" + NetworkHelpers.LocalEphemeralEndPoint().ToString();
191                         ServiceHost h = new ServiceHost (typeof (AllActions), new Uri (ep));
192                         h.AddServiceEndpoint (typeof (AllActions).FullName, new BasicHttpBinding (), "address");
193                         ServiceMetadataBehavior b = new ServiceMetadataBehavior ();
194                         b.HttpGetEnabled = true;
195                         b.HttpGetUrl = new Uri( ep );
196                         h.Description.Behaviors.Add (b);
197                         h.Open ();
198
199                         Assert.AreEqual (h.ChannelDispatchers.Count, 2, "#1");
200                         ChannelDispatcher channelDispatcher = h.ChannelDispatchers[1] as ChannelDispatcher;
201                         Assert.IsNotNull (channelDispatcher, "#2");
202                         Assert.IsTrue (channelDispatcher.Endpoints.Count == 1, "#3");
203                         EndpointAddressMessageFilter filter = channelDispatcher.Endpoints [0].AddressFilter as EndpointAddressMessageFilter;
204                         Assert.IsNotNull (filter, "#4");
205                         Assert.IsTrue (filter.Address.Equals (new EndpointAddress (ep)), "#5");
206                         Assert.IsFalse (filter.IncludeHostNameInComparison, "#6");
207                         Assert.IsTrue (channelDispatcher.Endpoints [0].ContractFilter is MatchAllMessageFilter, "#7");
208                         h.Close ();
209                 }
210
211                 [Test]
212                 public void SpecificActionTest ()
213                 {
214                         //EndpointDispatcher d = new EndpointDispatcher(
215                         var ep = NetworkHelpers.LocalEphemeralEndPoint().ToString();
216                         ServiceHost h = new ServiceHost (typeof (SpecificAction), new Uri ("http://" + ep));
217                         h.AddServiceEndpoint (typeof (Action1Interface), new BasicHttpBinding (), "address");
218                                                 
219                         h.Open ();
220                         ChannelDispatcher d = h.ChannelDispatchers [0] as ChannelDispatcher;
221                         EndpointDispatcher ed = d.Endpoints [0] as EndpointDispatcher;
222                         ActionMessageFilter actionFilter = ed.ContractFilter as ActionMessageFilter;
223                         Assert.IsNotNull (actionFilter, "#1");
224                         Assert.IsTrue (actionFilter.Actions.Count == 1, "#2");
225                         h.Close();
226                 }
227
228                 [Test]
229                 public void InitializeRuntimeBehaviors1 () {
230                         HostState st = new HostState ();
231                         var ep = NetworkHelpers.LocalEphemeralEndPoint().ToString();
232                         ServiceHost h = new ServiceHost (typeof (SpecificAction2), new Uri ("http://" + ep));
233                         h.AddServiceEndpoint (typeof (SpecificAction2), new BasicHttpBinding (), "temp");                       
234
235                         h.Description.Behaviors.Add (new MyServiceBehavior (st, h));
236
237                         h.Description.Endpoints [0].Behaviors.Add (new MyEndpointBehavior (st, h));
238                         h.Description.Endpoints [0].Contract.Behaviors.Add (new MyContractBehavior (st, h));
239                         h.Description.Endpoints [0].Contract.Operations [0].Behaviors.Add (new MyOperationBehavior (st, h));
240                         
241                         h.Open ();
242                         h.Close ();
243                         
244                         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";
245                         Assert.AreEqual (expected, st.CurrentStage);
246                 }
247
248                 [Test]
249                 public void InitializeRuntimeBehaviors2 () {
250                         HostState st = new HostState ();
251                         var ep = NetworkHelpers.LocalEphemeralEndPoint().ToString();
252                         ServiceHost h = new ServiceHost (typeof (SpecificAction), new Uri ("http://" + ep));
253                         h.AddServiceEndpoint (typeof (Action1Interface), new BasicHttpBinding (), "temp");
254                         h.AddServiceEndpoint (typeof (Action2Interface), new BasicHttpBinding (), "temp2");
255
256                         h.Description.Behaviors.Add (new MyServiceBehavior (st, h));                    
257                         
258                         h.Description.Endpoints [0].Behaviors.Add (new MyEndpointBehavior (st, h));
259                         h.Description.Endpoints [0].Contract.Behaviors.Add (new MyContractBehavior (st, h));
260                         h.Description.Endpoints [0].Contract.Operations [0].Behaviors.Add (new MyOperationBehavior (st, h));
261
262                         h.Description.Endpoints [1].Behaviors.Add (new MyEndpointBehavior (st, h));
263                         h.Description.Endpoints [1].Contract.Behaviors.Add (new MyContractBehavior (st, h));
264                         h.Description.Endpoints [1].Contract.Operations [0].Behaviors.Add (new MyOperationBehavior (st, h));
265                         h.Open ();
266                         h.Close ();
267
268                         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";
269                         Assert.AreEqual (expected, st.CurrentStage);
270                 }
271
272                 [Test]
273                 public void AddBaseAddress ()
274                 {
275                         var host = new Poker ();
276                         Assert.AreEqual (0, host.BaseAddresses.Count, "#1");
277                         host.DoAddBaseAddress (new Uri ("http://localhost:" + NetworkHelpers.FindFreePort ()));
278                         Assert.AreEqual (1, host.BaseAddresses.Count, "#1");
279                         host.DoAddBaseAddress (new Uri ("net.tcp://localhost:" + NetworkHelpers.FindFreePort ()));
280                         Assert.AreEqual (2, host.BaseAddresses.Count, "#1");
281                 }
282
283                 [Test]
284                 [ExpectedException (typeof (ArgumentException))]
285                 public void AddBaseAddress2 ()
286                 {
287                         var host = new Poker ();
288                         Assert.AreEqual (0, host.BaseAddresses.Count, "#1");
289                         host.DoAddBaseAddress (new Uri ("http://localhost:" + NetworkHelpers.FindFreePort ()));
290                         // http base address is already added.
291                         host.DoAddBaseAddress (new Uri ("http://localhost:" + NetworkHelpers.FindFreePort ()));
292                 }
293
294                 [Test]
295                 public void AddServiceEndpointUri ()
296                 {
297                         int port = NetworkHelpers.FindFreePort ();
298                         var host = new ServiceHost (typeof (AllActions),
299                                 new Uri ("http://localhost:" + port));
300                         var se = host.AddServiceEndpoint (typeof (AllActions),
301                                 new BasicHttpBinding (), "foobar");
302                         Assert.AreEqual ("http://localhost:" + port + "/foobar", se.Address.Uri.AbsoluteUri, "#1");
303                         Assert.AreEqual ("http://localhost:" + port + "/foobar", se.ListenUri.AbsoluteUri, "#2");
304                 }
305
306                 [Test]
307                 public void AddServiceEndpointUri2 ()
308                 {
309                         int port = NetworkHelpers.FindFreePort ();
310                         var host = new ServiceHost (typeof (AllActions),
311                                 new Uri ("http://localhost:" + port));
312                         var se = host.AddServiceEndpoint (typeof (AllActions),
313                                 new BasicHttpBinding (), String.Empty);
314                         Assert.AreEqual ("http://localhost:" + port + "/", se.Address.Uri.AbsoluteUri, "#1");
315                         Assert.AreEqual ("http://localhost:" + port + "/", se.ListenUri.AbsoluteUri, "#2");
316                 }
317
318                 [Test]
319                 [ExpectedException (typeof (InvalidOperationException))]
320                 public void AddServiceEndpointOnlyMex ()
321                 {
322             var ep = NetworkHelpers.LocalEphemeralEndPoint().ToString();
323                         var host = new ServiceHost (typeof (AllActions),
324                                 new Uri ("http://" + ep));
325                         host.Description.Behaviors.Add (new ServiceMetadataBehavior ());
326                         host.AddServiceEndpoint ("IMetadataExchange",
327                                 new BasicHttpBinding (), "/wsdl");
328                         host.Open ();
329                         try {
330                                 // to make sure that throwing IOE from here does not count.
331                                 host.Close ();
332                         } catch {
333                         }
334                         Assert.Fail ("should not open");
335                 }
336
337                 [Test]
338                 [Category ("NotWorking")] // Timeouts randomly #5813
339                 public void RunDestinationUnreachableTest ()
340                 {
341                         RunDestinationUnreachableTest ("BasicHttp", new BasicHttpBinding ());
342                 }
343
344                 [Test]
345                 [Category ("NotWorking")] // Timeouts randomly #5813
346                 public void RunDestinationUnreachableTest2 ()
347                 {
348                         RunDestinationUnreachableTest ("CustomSoap12", new CustomBinding (new HttpTransportBindingElement ()));
349                 }
350
351                 void RunDestinationUnreachableTest (string label, Binding binding)
352                 {
353                         string address = "http://" + NetworkHelpers.LocalEphemeralEndPoint().ToString();
354                         var host = OpenHost (address, binding);
355                         
356                         try {
357                                 var client = new DestinationUnreachableClient (binding, address);
358                                 client.NotImplementedOperation ();
359                                 Assert.Fail (label + " ActionNotSupportedException is expected");
360                         } catch (ActionNotSupportedException) {
361                                 // catching it instead of ExpectedException to distinguish errors at service side.
362                         } finally {
363                                 host.Close ();
364                         }
365                 }
366                 
367                 ServiceHost OpenHost (string address, Binding binding)
368                 {
369                         var baseAddresses = new Uri[] { new Uri(address) };
370
371                         var host = new ServiceHost (typeof (DummyService), baseAddresses);
372                         host.AddServiceEndpoint (typeof (IDummyService), binding, new Uri ("", UriKind.Relative));
373                         host.Open ();
374                         return host;
375                 }
376
377                 [Test]
378                 public void AddServiceEndpoint_Directly ()
379                 {
380                         var host = new ServiceHost (typeof (DummyService));
381                         var port = NetworkHelpers.FindFreePort ();
382                         var address = new EndpointAddress ("http://localhost:" + port);
383                         var binding = new BasicHttpBinding ();
384                         var contract = ContractDescription.GetContract (typeof (IDummyService));
385                         host.AddServiceEndpoint (new ServiceEndpoint (contract, binding, address));
386                 }
387
388                 [Test]
389                 [ExpectedException (typeof (ArgumentException))]
390                 public void AddServiceEndpoint_Directly_NullAddress ()
391                 {
392                         var host = new ServiceHost (typeof (DummyService));
393                         var binding = new BasicHttpBinding ();
394                         var contract = ContractDescription.GetContract (typeof (IDummyService));
395                         host.AddServiceEndpoint (new ServiceEndpoint (contract, binding, null));
396                 }
397
398                 [Test]
399                 [ExpectedException (typeof (ArgumentException))]
400                 public void AddServiceEndpoint_Directly_NullBinding ()
401                 {
402                         var host = new ServiceHost (typeof (DummyService));
403                         var port = NetworkHelpers.FindFreePort ();
404                         var address = new EndpointAddress ("http://localhost:" + port);
405                         var contract = ContractDescription.GetContract (typeof (IDummyService));
406                         host.AddServiceEndpoint (new ServiceEndpoint (contract, null, address));
407                 }
408
409                 [Test]
410                 [ExpectedException (typeof (ArgumentException))]
411                 public void AddServiceMetadataEndpoint ()
412                 {
413                         var host = new ServiceHost (typeof (DummyService));
414                         host.AddServiceEndpoint (new ServiceMetadataEndpoint ());
415                 }
416
417                 [Test]
418                 [ExpectedException (typeof (InvalidOperationException))]
419                 public void AddServiceEndpoint_Directly_ContractMismatch ()
420                 {
421                         var host = new ServiceHost (typeof (DummyService));
422                         var port = NetworkHelpers.FindFreePort ();
423                         var address = new EndpointAddress ("http://localhost:" + port);
424                         var binding = new BasicHttpBinding ();
425                         var contract = ContractDescription.GetContract (typeof (INotImplementedService));
426                         host.AddServiceEndpoint (new ServiceEndpoint (contract, binding, address));
427                 }
428
429                 #region helpers
430
431                 public enum Stage
432                 {
433                 }
434
435                 public class HostState
436                 {
437                         public string CurrentStage = "Start";
438                 }
439
440                 public class MyServiceBehavior : IServiceBehavior
441                 {
442                         #region IServiceBehavior Members
443
444                         HostState _state;
445                         ServiceHost _host;
446                         public MyServiceBehavior (HostState state, ServiceHost h) {
447                                 _state = state;
448                                 _host = h;
449                         }
450
451                         public void AddBindingParameters (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, global::System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) {                         
452                                 _state.CurrentStage += ", IServiceBehavior.AddBindingParameters";                               
453                                 bindingParameters.Add (this);
454                         }
455
456                         public void ApplyDispatchBehavior (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {                            
457                                 _state.CurrentStage += ", IServiceBehavior.ApplyDispatchBehavior";                              
458                         }
459
460                         public void Validate (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
461                                 _state.CurrentStage += ", IServiceBehavior.Validate";
462                                 Assert.AreEqual (_host.ChannelDispatchers.Count, 0);
463                         }
464
465                         #endregion
466                 }
467
468                 public class MyEndpointBehavior : IEndpointBehavior
469                 {
470                         #region IEndpointBehavior Members
471                         HostState _state;
472                         ServiceHost _host;
473                         public MyEndpointBehavior (HostState state, ServiceHost h) {
474                                 _state = state;
475                                 _host = h;
476                         }
477
478                         public void AddBindingParameters (ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) {
479                                 Console.WriteLine ("IEndpointBehavior - AddBindingParameters " + _host.ChannelDispatchers.Count);
480                                 _state.CurrentStage += ", IEndpointBehavior.AddBindingParameters";                              
481                                 bindingParameters.Add (this);
482                         }
483
484                         public void ApplyClientBehavior (ServiceEndpoint endpoint, ClientRuntime clientRuntime) {
485                                 Console.WriteLine ("IEndpointBehavior - ApplyClientBehavior " + _host.ChannelDispatchers.Count);
486                         }
487
488                         public void ApplyDispatchBehavior (ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) {
489                                 Console.WriteLine ("IEndpointBehavior - ApplyDispatchBehavior " + _host.ChannelDispatchers.Count);
490                                 _state.CurrentStage += ", IEndpointBehavior.ApplyDispatchBehavior";                             
491                         }
492
493                         public void Validate (ServiceEndpoint endpoint) {
494                                 Console.WriteLine ("IEndpointBehavior - Validate " + _host.ChannelDispatchers.Count);
495                                 _state.CurrentStage += ", IEndpointBehavior.Validate";
496                                 Assert.AreEqual (_host.ChannelDispatchers.Count, 0);                            
497                         }
498
499                         #endregion
500                 }
501
502                 public class MyContractBehavior : IContractBehavior
503                 {
504                         #region IContractBehavior Members
505                         HostState _state;
506                         ServiceHost _host;
507                         public MyContractBehavior (HostState state, ServiceHost h) {
508                                 _state = state;
509                                 _host = h;
510                         }
511
512                         public void AddBindingParameters (ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) {
513                                 Console.WriteLine ("Contract - AddBindingParameters " + _host.ChannelDispatchers.Count);
514                                 _state.CurrentStage += ", IContractBehavior.AddBindingParameters";                              
515                                 bindingParameters.Add (this);
516                         }
517
518                         public void ApplyClientBehavior (ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime) {
519                                 Console.WriteLine ("Contract - ApplyClientBehavior " + _host.ChannelDispatchers.Count);
520                         }
521
522                         public void ApplyDispatchBehavior (ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime) {
523                                 Console.WriteLine ("Contract - ApplyDispatchBehavior " + _host.ChannelDispatchers.Count);
524                                 _state.CurrentStage += ", IContractBehavior.ApplyDispatchBehavior";                             
525                         }
526
527                         public void Validate (ContractDescription contractDescription, ServiceEndpoint endpoint) {
528                                 Console.WriteLine ("Contract - Validate " + _host.ChannelDispatchers.Count);
529                                 _state.CurrentStage += ", IContractBehavior.Validate";          
530                                 Assert.AreEqual (_host.ChannelDispatchers.Count, 0);                            
531                         }
532
533                         #endregion
534                 }
535
536                 public class MyOperationBehavior : IOperationBehavior
537                 {
538                         #region IOperationBehavior Members
539                         HostState _state;
540                         ServiceHost _host;
541                         public MyOperationBehavior (HostState state, ServiceHost h) {
542                                 _state = state;
543                                 _host = h;
544                         }
545
546                         public void AddBindingParameters (OperationDescription operationDescription, BindingParameterCollection bindingParameters) {
547                                 Console.WriteLine ("IOperationBehavior - AddBindingParameters " + _host.ChannelDispatchers.Count);
548                                 _state.CurrentStage += ", IOperationBehavior.AddBindingParameters";                                     
549                                 bindingParameters.Add (this);
550                         }
551
552                         public void ApplyClientBehavior (OperationDescription operationDescription, ClientOperation clientOperation) {
553                                 Console.WriteLine ("IOperationBehavior - ApplyClientBehavior " + _host.ChannelDispatchers.Count);
554                         }
555
556                         public void ApplyDispatchBehavior (OperationDescription operationDescription, DispatchOperation dispatchOperation) {
557                                 Console.WriteLine ("IOperationBehavior - ApplyDispatchBehavior " + _host.ChannelDispatchers.Count);
558                                 _state.CurrentStage += ", IOperationBehavior.ApplyDispatchBehavior";                            
559                         }
560
561                         public void Validate (OperationDescription operationDescription) {
562                                 Console.WriteLine ("IOperationBehavior - Validate " + _host.ChannelDispatchers.Count);
563                                 _state.CurrentStage += ", IOperationBehavior.ApplyDispatchBehavior";
564                                 Assert.AreEqual (_host.ChannelDispatchers.Count, 0);
565                         }
566
567                         #endregion
568                 }
569
570                 [ServiceContract]
571                 class AllActions
572                 {
573                         [OperationContract (Action = "*", ReplyAction = "*")]
574                         public SMMessage Get (SMMessage req) {
575                                 return null;
576                         }
577                 }
578
579                 [ServiceContract]
580                 interface Action1Interface
581                 {
582                         [OperationContract (Action = "Specific1", ReplyAction = "*")]
583                         SMMessage GetMessage1 (SMMessage req);
584                 }
585
586                 [ServiceContract]
587                 interface Action2Interface
588                 {
589                         [OperationContract (Action = "Specific2", ReplyAction = "*")]
590                         SMMessage GetMessage2 (SMMessage req);
591                 }
592                 
593                 class SpecificAction : Action1Interface, Action2Interface
594                 {                       
595                         public SMMessage GetMessage1 (SMMessage req) {
596                                 return null;
597                         }
598
599                         public SMMessage GetMessage2 (SMMessage req) {
600                                 return null;
601                         }
602                 }
603
604                 [ServiceContract]
605                 class SpecificAction2
606                 {
607                         [OperationContract (Action = "Specific", ReplyAction = "*")]
608                         public SMMessage GetMessage1 (SMMessage req) {
609                                 return null;
610                         }
611                 }
612
613                 class MyChannelDispatcher : ChannelDispatcher
614                 {
615                         public bool Attached = false;
616
617                         public MyChannelDispatcher (IChannelListener l) : base (l) { }
618                         protected override void Attach (ServiceHostBase host) {
619                                 base.Attach (host);
620                                 Attached = true;
621                         }
622                 }
623
624                 class MyChannelListener : IChannelListener
625                 {
626                         #region IChannelListener Members
627
628                         public IAsyncResult BeginWaitForChannel (TimeSpan timeout, AsyncCallback callback, object state) {
629                                 throw new NotImplementedException ();
630                         }
631
632                         public bool EndWaitForChannel (IAsyncResult result) {
633                                 throw new NotImplementedException ();
634                         }
635
636                         public T GetProperty<T> () where T : class {
637                                 throw new NotImplementedException ();
638                         }
639
640                         public Uri Uri {
641                                 get { throw new NotImplementedException (); }
642                         }
643
644                         public bool WaitForChannel (TimeSpan timeout) {
645                                 throw new NotImplementedException ();
646                         }
647
648                         #endregion
649
650                         #region ICommunicationObject Members
651
652                         public void Abort () {
653                                 throw new NotImplementedException ();
654                         }
655
656                         public IAsyncResult BeginClose (TimeSpan timeout, AsyncCallback callback, object state) {
657                                 throw new NotImplementedException ();
658                         }
659
660                         public IAsyncResult BeginClose (AsyncCallback callback, object state) {
661                                 throw new NotImplementedException ();
662                         }
663
664                         public IAsyncResult BeginOpen (TimeSpan timeout, AsyncCallback callback, object state) {
665                                 throw new NotImplementedException ();
666                         }
667
668                         public IAsyncResult BeginOpen (AsyncCallback callback, object state) {
669                                 throw new NotImplementedException ();
670                         }
671
672                         public void Close (TimeSpan timeout) {
673                                 throw new NotImplementedException ();
674                         }
675
676                         public void Close () {
677                                 throw new NotImplementedException ();
678                         }
679
680                         public event EventHandler Closed;
681
682                         public event EventHandler Closing;
683
684                         public void EndClose (IAsyncResult result) {
685                                 throw new NotImplementedException ();
686                         }
687
688                         public void EndOpen (IAsyncResult result) {
689                                 throw new NotImplementedException ();
690                         }
691
692                         public event EventHandler Faulted;
693
694                         public void Open (TimeSpan timeout) {
695                                 throw new NotImplementedException ();
696                         }
697
698                         public void Open () {
699                                 throw new NotImplementedException ();
700                         }
701
702                         public event EventHandler Opened;
703
704                         public event EventHandler Opening;
705
706                         public CommunicationState State {
707                                 get { throw new NotImplementedException (); }
708                         }
709
710                         #endregion
711                 }
712
713                 [ServiceContract]
714                 public interface IDummyService
715                 {
716                         [OperationContract]
717                         void DummyOperation ();
718                 }
719                 public class DummyService : IDummyService
720                 {
721                         public void DummyOperation ()
722                         {
723                                 // Do nothing
724                         }
725                 }
726                 [ServiceContract]
727                 public interface INotImplementedService
728                 {
729                         [OperationContract]
730                         void NotImplementedOperation ();
731                 }
732                 public class DestinationUnreachableClient : ClientBase<INotImplementedService>, INotImplementedService
733                 {
734                         public void NotImplementedOperation ()
735                         {
736                                 Channel.NotImplementedOperation ();
737                         }
738                 
739                         public DestinationUnreachableClient (Binding binding, string address) 
740                                 : base (binding, new EndpointAddress (address))
741                         {
742                         }
743                 }
744
745                 #endregion
746         }
747 }