1

我尝试根据本教程将发现添加到我的 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”。此集合仅支持每种类型的一个实例。

4

1 回答 1

0

您可以在添加新数组之前清理 Behaviors 数组。

// Add ServiceDiscoveryBehavior
serviceHost.Description.Behaviors.RemoveAll<ClientCredentials>();
serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
于 2013-10-23T15:10:49.917 回答