简体   繁体   中英

Add Discovery WCF service

i try to add Discovery to my WCF service according this tutorial: http://msdn.microsoft.com/en-us/library/dd456783.aspx

This is My main:

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 method:

    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;
        }
    }

And get an error under:

// Add ServiceDiscoveryBehavior
serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());

The value could not be added to the collection, as the collection already contains an item of the same type: 'System.ServiceModel.Discovery.ServiceDiscoveryBehavior'. This collection only supports one instance of each type.

You can clean Behaviors array, before adding new one.

// Add ServiceDiscoveryBehavior
serviceHost.Description.Behaviors.RemoveAll<ClientCredentials>();
serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM