public void Use2() { // This time with ServiceDiscoveryBehavior. var b = new EndpointDiscoveryBehavior(); IEndpointBehavior eb = b; var host = new ServiceHost(typeof(TestService)); var se = host.AddServiceEndpoint(typeof(ITestService), new BasicHttpBinding(), new Uri("http://localhost:37564")); var sdb = new ServiceDiscoveryBehavior(); sdb.AnnouncementEndpoints.Add(new UdpAnnouncementEndpoint()); IServiceBehavior sb = sdb; se.Behaviors.Add(b); var bc = new BindingParameterCollection(); sb.AddBindingParameters(host.Description, host, host.Description.Endpoints, bc); eb.AddBindingParameters(se, bc); Assert.AreEqual(0, bc.Count, "#1"); Assert.AreEqual(0, host.Extensions.Count, "#1-2"); sb.Validate(host.Description, host); eb.Validate(se); // ... should "validate" not "apply dispatch behavior" do "add host extension" job? I doubt that. Assert.AreEqual(1, host.Extensions.Count, "#2-2"); var dse = host.Extensions.Find <DiscoveryServiceExtension> (); Assert.IsNotNull(dse, "#2-3"); Assert.AreEqual(0, dse.PublishedEndpoints.Count, "#2-4"); Assert.AreEqual(2, se.Behaviors.Count, "#2-5"); // EndpointDiscoveryBehavior + discovery initializer. Assert.AreEqual(0, host.ChannelDispatchers.Count, "#3-1"); Assert.AreEqual(1, host.Description.Endpoints.Count, "#3-2"); Assert.AreEqual(0, dse.PublishedEndpoints.Count, "#3-4"); // The IEndpointBehavior from EndpointDiscoveryBehavior, when ApplyDispatchBehavior() is invoked, publishes an endpoint. sb.ApplyDispatchBehavior(host.Description, host); Assert.AreEqual(0, dse.PublishedEndpoints.Count, "#3-5"); // not yet published eb.ApplyDispatchBehavior(se, new EndpointDispatcher(new EndpointAddress("http://localhost:37564"), "ITestService", "http://tempuri.org/")); Assert.AreEqual(2, host.ChannelDispatchers.Count, "#3-6-1"); // for online and offline announcements Assert.AreEqual(0, dse.PublishedEndpoints.Count, "#3-6-2"); // still not published. host.Open(); try { Assert.AreEqual(3, host.ChannelDispatchers.Count, "#4-1"); // for online and offline announcements Assert.AreEqual(1, dse.PublishedEndpoints.Count, "#4-2"); // The endpoint is published again. (Not sure if it's worthy of testing.) } finally { host.Close(); } }
public void Use() { // Without ServiceDiscoveryBehavior. var b = new EndpointDiscoveryBehavior(); IEndpointBehavior eb = b; var host = new ServiceHost(typeof(TestService)); var se = host.AddServiceEndpoint(typeof(ITestService), new BasicHttpBinding(), new Uri("http://localhost:37564")); se.Behaviors.Add(b); var bc = new BindingParameterCollection(); eb.AddBindingParameters(se, bc); Assert.AreEqual(0, bc.Count, "#1"); Assert.AreEqual(0, host.Extensions.Count, "#1-2"); eb.Validate(se); // This behavior itself does not populate discovery extension. It just configures the extension. Assert.AreEqual(0, host.Extensions.Count, "#2-2"); }
/// <summary> /// Implement to pass data at runtime to bindings to support custom behavior. /// </summary> /// <param name="endpoint">The endpoint to modify.</param> /// <param name="bindingParameters">The objects that binding elements require to support the behavior.</param> public void AddBindingParameters( ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) { endpointBehavior.AddBindingParameters(endpoint, bindingParameters); }