2 // Author: Atsushi Enomoto <atsushi@ximian.com>
4 // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 using System.Collections.Generic;
27 using System.Collections.ObjectModel;
29 using System.Net.Sockets;
30 using System.ServiceModel;
31 using System.ServiceModel.Channels;
32 using System.ServiceModel.Description;
33 using System.ServiceModel.Discovery;
34 using System.ServiceModel.Dispatcher;
35 using System.Threading;
36 using NUnit.Framework;
38 namespace MonoTests.System.ServiceModel.Discovery
41 public class IntegratedDiscoveryTest
43 void AssertTcpPortOpen (int port)
46 var l = new TcpListener (port);
49 } catch (Exception ex) {
50 Assert.Fail (String.Format ("TCP port {0} is not freed", port));
55 [Category ("NotWorking")]
56 public void UseCase1 ()
58 RunCodeUnderDiscoveryHost1 (new Uri ("http://localhost:37564"), new Uri ("http://localhost:4949"), new Uri ("http://localhost:4989"), UseCase1Core);
59 AssertTcpPortOpen (4949);
60 AssertTcpPortOpen (4989);
61 AssertTcpPortOpen (37564);
64 void UseCase1Core (Uri serviceUri, AnnouncementEndpoint aEndpoint, DiscoveryEndpoint dEndpoint)
66 // actual service, announcing to 4989
67 var host = new ServiceHost (typeof (TestService));
68 var sdb = new ServiceDiscoveryBehavior ();
69 sdb.AnnouncementEndpoints.Add (aEndpoint);
70 host.Description.Behaviors.Add (sdb);
71 host.AddServiceEndpoint (typeof (ITestService), new BasicHttpBinding (), serviceUri);
73 // It does not start announcement very soon, so wait for a while.
76 // actual client, with DiscoveryClientBindingElement
77 var be = new DiscoveryClientBindingElement () { DiscoveryEndpointProvider = new SimpleDiscoveryEndpointProvider (dEndpoint) };
78 var clientBinding = new CustomBinding (new BasicHttpBinding ());
79 clientBinding.Elements.Insert (0, be);
80 var cf = new ChannelFactory<ITestService> (clientBinding, DiscoveryClientBindingElement.DiscoveryEndpointAddress);
81 var ch = cf.CreateChannel ();
82 Assert.AreEqual ("TEST", ch.Echo ("TEST"), "#1");
89 void RunCodeUnderDiscoveryHost1 (Uri serviceUri, Uri dHostUri, Uri aHostUri, Action<Uri,AnnouncementEndpoint,DiscoveryEndpoint> action)
91 // announcement service
92 var abinding = new CustomBinding (new HttpTransportBindingElement ());
93 var aAddress = new EndpointAddress (aHostUri);
94 var aEndpoint = new AnnouncementEndpoint (abinding, aAddress);
97 var dbinding = new CustomBinding (new HttpTransportBindingElement ());
98 var dAddress = new EndpointAddress (dHostUri);
99 var dEndpoint = new DiscoveryEndpoint (dbinding, dAddress);
100 // Without this, .NET rejects the host as if it had no service.
101 dEndpoint.IsSystemEndpoint = false;
103 // it internally hosts an AnnouncementService
104 using (var inst = new AnnouncementBoundDiscoveryService (aEndpoint)) {
105 var host = new ServiceHost (inst);
106 host.AddServiceEndpoint (dEndpoint);
109 action (serviceUri, aEndpoint, dEndpoint);
116 [Test] // Announcement: UDP, Discovery: HTTP
117 [Category ("NotWorking")]
118 public void UseCase2 ()
120 RunCodeUnderDiscoveryHost2 (new Uri ("http://localhost:37564"), new Uri ("http://localhost:4949"), UseCase2Core);
121 AssertTcpPortOpen (4949);
122 AssertTcpPortOpen (37564);
125 void UseCase2Core (Uri serviceUri, AnnouncementEndpoint aEndpoint, DiscoveryEndpoint dEndpoint)
127 // actual service, announcing to UDP
128 var host = new ServiceHost (typeof (TestService));
129 var sdb = new ServiceDiscoveryBehavior ();
130 sdb.AnnouncementEndpoints.Add (aEndpoint);
131 host.Description.Behaviors.Add (sdb);
132 host.AddServiceEndpoint (typeof (ITestService), new BasicHttpBinding (), serviceUri);
134 // It does not start announcement very soon, so wait for a while.
137 // actual client, with DiscoveryClientBindingElement
138 var be = new DiscoveryClientBindingElement () { DiscoveryEndpointProvider = new SimpleDiscoveryEndpointProvider (dEndpoint) };
139 var clientBinding = new CustomBinding (new BasicHttpBinding ());
140 clientBinding.Elements.Insert (0, be);
141 var cf = new ChannelFactory<ITestService> (clientBinding, DiscoveryClientBindingElement.DiscoveryEndpointAddress);
142 var ch = cf.CreateChannel ();
143 Assert.AreEqual ("TEST", ch.Echo ("TEST"), "#1");
149 void RunCodeUnderDiscoveryHost2 (Uri serviceUri, Uri dHostUri, Action<Uri,AnnouncementEndpoint,DiscoveryEndpoint> action)
151 // announcement service
152 var aEndpoint = new UdpAnnouncementEndpoint (new Uri ("soap.udp://239.255.255.250:3802/"));
155 var dbinding = new CustomBinding (new HttpTransportBindingElement ());
156 var dAddress = new EndpointAddress (dHostUri);
157 var dEndpoint = new DiscoveryEndpoint (dbinding, dAddress);
158 // Without this, .NET rejects the host as if it had no service.
159 dEndpoint.IsSystemEndpoint = false;
161 // it internally hosts an AnnouncementService
162 using (var inst = new AnnouncementBoundDiscoveryService (aEndpoint)) {
163 var host = new ServiceHost (inst);
164 host.AddServiceEndpoint (dEndpoint);
167 action (serviceUri, aEndpoint, dEndpoint);
175 [Category ("NotWorking")]
176 public void UseCase3 ()
178 RunCodeUnderDiscoveryHost3 (new Uri ("http://localhost:37564"), new Uri ("http://localhost:4989"), UseCase3Core);
179 AssertTcpPortOpen (4989);
180 AssertTcpPortOpen (37564);
183 void UseCase3Core (Uri serviceUri, AnnouncementEndpoint aEndpoint, DiscoveryEndpoint dEndpoint)
185 // actual service, announcing to 4989
186 var host = new ServiceHost (typeof (TestService));
187 var sdb = new ServiceDiscoveryBehavior ();
188 sdb.AnnouncementEndpoints.Add (aEndpoint);
189 host.Description.Behaviors.Add (sdb);
190 host.AddServiceEndpoint (typeof (ITestService), new BasicHttpBinding (), serviceUri);
192 // It does not start announcement very soon, so wait for a while.
195 // actual client, with DiscoveryClientBindingElement
196 var be = new DiscoveryClientBindingElement () { DiscoveryEndpointProvider = new SimpleDiscoveryEndpointProvider (dEndpoint) };
197 var clientBinding = new CustomBinding (new BasicHttpBinding ());
198 clientBinding.Elements.Insert (0, be);
199 var cf = new ChannelFactory<ITestService> (clientBinding, DiscoveryClientBindingElement.DiscoveryEndpointAddress);
200 var ch = cf.CreateChannel ();
201 Assert.AreEqual ("TEST", ch.Echo ("TEST"), "#1");
208 void RunCodeUnderDiscoveryHost3 (Uri serviceUri, Uri aHostUri, Action<Uri,AnnouncementEndpoint,DiscoveryEndpoint> action)
210 // announcement service
211 var abinding = new CustomBinding (new HttpTransportBindingElement ());
212 var aAddress = new EndpointAddress (aHostUri);
213 var aEndpoint = new AnnouncementEndpoint (abinding, aAddress);
216 var dEndpoint = new UdpDiscoveryEndpoint (DiscoveryVersion.WSDiscovery11, new Uri ("soap.udp://239.255.255.250:3802/"));
217 // Without this, .NET rejects the host as if it had no service.
218 dEndpoint.IsSystemEndpoint = false;
220 // it internally hosts an AnnouncementService
221 using (var inst = new AnnouncementBoundDiscoveryService (aEndpoint)) {
222 var host = new ServiceHost (inst);
223 host.AddServiceEndpoint (dEndpoint);
226 action (serviceUri, aEndpoint, dEndpoint);
233 class SimpleDiscoveryEndpointProvider : DiscoveryEndpointProvider
235 public SimpleDiscoveryEndpointProvider (DiscoveryEndpoint endpoint)
237 this.endpoint = endpoint;
240 DiscoveryEndpoint endpoint;
242 public override DiscoveryEndpoint GetDiscoveryEndpoint ()