我尝试根据本教程将发现添加到我的WCF服务:http://msdn.microsoft.com/en-us/library/dd456783.aspx
这是我的主要内容:
static void Main(string[] args)
{
// Create a ServiceHost for the CalculatorService type.
using (ServiceHost serviceHost = new ServiceHost(typeof(MySampleWCFService)))
{
// Add a ServiceDiscoveryBehavior
serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
// ...
// Add ServiceDiscoveryBehavior
//serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
// Add a UdpDiscoveryEndpoint
serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());
Uri uri = new Uri("http://" + "10.161.150.250" + ":8733/MySampleWCFService");
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress endpoint = new EndpointAddress(uri);
WCFServiceHostingInWinService.MySampleWCFService service = new MySampleWCFService();
// Send to my service 2 numbers and received their value
Console.WriteLine(service.Add(11, 12));
EndpointAddress eee = FindCalculatorServiceAddress();
Console.ReadLine();
}
}
FindCalculatorServiceAddress方法:
static EndpointAddress FindCalculatorServiceAddress()
{
// Create DiscoveryClient
DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());
// Find ICalculatorService endpoints
FindResponse findResponse = discoveryClient.Find(new FindCriteria(typeof(IMySampleWCFService)));
if (findResponse.Endpoints.Count > 0)
{
return findResponse.Endpoints[0].Address;
}
else
{
return null;
}
}
并在以下情况下收到错误:
// Add ServiceDiscoveryBehavior
serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
无法将值添加到集合中,因为集合已包含相同类型的项:'System.ServiceModel.Discovery.ServiceDiscoveryBehavior'。此集合仅支持每种类型的一个实例。
答案 0 :(得分:0)
您可以在添加新阵列之前清除“行为”阵列。
// Add ServiceDiscoveryBehavior
serviceHost.Description.Behaviors.RemoveAll<ClientCredentials>();
serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());