public static void EnableDiscovery(this ServiceHost host, bool enableMEX = true) { if (host.Description.Endpoints.Count == 0) { host.AddDefaultEndpoints(); } host.AddServiceEndpoint(new UdpDiscoveryEndpoint()); ServiceDiscoveryBehavior discovery = new ServiceDiscoveryBehavior(); discovery.AnnouncementEndpoints.Add(new UdpAnnouncementEndpoint()); host.Description.Behaviors.Add(discovery); if (enableMEX == true) { host.Description.Behaviors.Add(new ServiceMetadataBehavior()); foreach (Uri baseAddress in host.BaseAddresses) { Binding binding = null; if (baseAddress.Scheme == "net.tcp") { binding = MetadataExchangeBindings.CreateMexTcpBinding(); } if (baseAddress.Scheme == "net.pipe") { binding = MetadataExchangeBindings.CreateMexNamedPipeBinding(); } Debug.Assert(binding != null); if (binding != null) { host.AddServiceEndpoint(typeof(IMetadataExchange), binding, "MEX"); } } } }
public static void EnableDiscovery(this ServiceHost host, Uri scope, bool enableMEX = true) { if (host.Description.Endpoints.Count == 0) { host.AddDefaultEndpoints(); } host.AddServiceEndpoint(new UdpDiscoveryEndpoint()); ServiceDiscoveryBehavior discovery = new ServiceDiscoveryBehavior(); discovery.AnnouncementEndpoints.Add(new UdpAnnouncementEndpoint()); host.Description.Behaviors.Add(discovery); if (enableMEX == true) { host.Description.Behaviors.Add(new ServiceMetadataBehavior()); foreach (Uri baseAddress in host.BaseAddresses) { Binding binding = null; if (baseAddress.Scheme == "net.tcp") { binding = MetadataExchangeBindings.CreateMexTcpBinding(); } if (baseAddress.Scheme == "net.pipe") { binding = MetadataExchangeBindings.CreateMexNamedPipeBinding(); } if (baseAddress.Scheme == "http") { binding = MetadataExchangeBindings.CreateMexHttpBinding(); } if (baseAddress.Scheme == "https") { binding = MetadataExchangeBindings.CreateMexHttpsBinding(); } Debug.Assert(binding != null); if (binding != null) { host.AddServiceEndpoint(typeof(IMetadataExchange), binding, "MEX"); } } } if (scope != null) { EndpointDiscoveryBehavior behavior = new EndpointDiscoveryBehavior(); behavior.Scopes.Add(scope); foreach (ServiceEndpoint endpoint in host.Description.Endpoints) { if (endpoint.IsSystemEndpoint || endpoint is DiscoveryEndpoint || endpoint is AnnouncementEndpoint || endpoint is ServiceMetadataEndpoint) { continue; } endpoint.Behaviors.Add(behavior); } } }