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;
28 using System.ServiceModel;
29 using System.ServiceModel.Channels;
30 using System.ServiceModel.Description;
31 using System.ServiceModel.Discovery;
32 using System.ServiceModel.Dispatcher;
33 using NUnit.Framework;
35 namespace MonoTests.System.ServiceModel.Discovery
38 public class EndpointDiscoveryBehaviorTest
43 // Without ServiceDiscoveryBehavior.
44 var b = new EndpointDiscoveryBehavior ();
45 IEndpointBehavior eb = b;
46 var host = new ServiceHost (typeof (TestService));
47 var se = host.AddServiceEndpoint (typeof (ITestService), new BasicHttpBinding (), new Uri ("http://localhost:37564"));
50 var bc = new BindingParameterCollection ();
51 eb.AddBindingParameters (se, bc);
52 Assert.AreEqual (0, bc.Count, "#1");
53 Assert.AreEqual (0, host.Extensions.Count, "#1-2");
56 // This behavior itself does not populate discovery extension. It just configures the extension.
57 Assert.AreEqual (0, host.Extensions.Count, "#2-2");
61 [Category ("NotWorking")]
64 // This time with ServiceDiscoveryBehavior.
65 var b = new EndpointDiscoveryBehavior ();
66 IEndpointBehavior eb = b;
67 var host = new ServiceHost (typeof (TestService));
68 var se = host.AddServiceEndpoint (typeof (ITestService), new BasicHttpBinding (), new Uri ("http://localhost:37564"));
69 var sdb = new ServiceDiscoveryBehavior ();
70 sdb.AnnouncementEndpoints.Add (new UdpAnnouncementEndpoint ());
71 IServiceBehavior sb = sdb;
74 var bc = new BindingParameterCollection ();
75 sb.AddBindingParameters (host.Description, host, host.Description.Endpoints, bc);
76 eb.AddBindingParameters (se, bc);
77 Assert.AreEqual (0, bc.Count, "#1");
78 Assert.AreEqual (0, host.Extensions.Count, "#1-2");
80 sb.Validate (host.Description, host);
82 // ... should "validate" not "apply dispatch behavior" do "add host extension" job? I doubt that.
83 Assert.AreEqual (1, host.Extensions.Count, "#2-2");
84 var dse = host.Extensions.Find<DiscoveryServiceExtension> ();
86 Assert.IsNotNull (dse, "#2-3");
87 Assert.AreEqual (0, dse.PublishedEndpoints.Count, "#2-4");
88 Assert.AreEqual (2, se.Behaviors.Count, "#2-5"); // EndpointDiscoveryBehavior + discovery initializer.
90 Assert.AreEqual (0, host.ChannelDispatchers.Count, "#3-1");
91 Assert.AreEqual (1, host.Description.Endpoints.Count, "#3-2");
92 Assert.AreEqual (0, dse.PublishedEndpoints.Count, "#3-4");
94 // The IEndpointBehavior from EndpointDiscoveryBehavior, when ApplyDispatchBehavior() is invoked, publishes an endpoint.
95 sb.ApplyDispatchBehavior (host.Description, host);
96 Assert.AreEqual (0, dse.PublishedEndpoints.Count, "#3-5"); // not yet published
97 eb.ApplyDispatchBehavior (se, new EndpointDispatcher (new EndpointAddress ("http://localhost:37564"), "ITestService", "http://tempuri.org/"));
98 Assert.AreEqual (2, host.ChannelDispatchers.Count, "#3-6-1"); // for online and offline announcements
99 Assert.AreEqual (0, dse.PublishedEndpoints.Count, "#3-6-2"); // still not published.
103 Assert.AreEqual (3, host.ChannelDispatchers.Count, "#4-1"); // for online and offline announcements
104 Assert.AreEqual (1, dse.PublishedEndpoints.Count, "#4-2"); // The endpoint is published again. (Not sure if it's worthy of testing.)