简体   繁体   中英

Multiple contracts in one service can not be discovered on Client using WCF Discovery

I want to develop a WCF service to support multiple contracts. I managed to make this work by following the instruction from this post on Stackoverflow.

So basically create one service(FooBarService) to implement multiple contracts(IFooService, IBarService).

    string serviceAddress = "net.tcp://localhost:8088/FooBarService";

    ServiceHost selfServiceHost = new ServiceHost(typeof(FooBarService));            

    // The endpoints need to share this binding.
    var binding = new NetTcpBinding();

    selfServiceHost.AddServiceEndpoint(typeof(IFooService), binding, serviceAddress);
    selfServiceHost.AddServiceEndpoint(typeof(IBarService), binding, serviceAddress);

But the challenging thing is that I want to also make this service discoverable using WCF Discovery in order to decouple client and service from endpoint binding.

In the Service side, I did something like this:

    var discoveryBehavior = new ServiceDiscoveryBehavior();
    discoveryBehavior.AnnouncementEndpoints.Add(new AnnouncementEndpoint(new NetTcpBinding(SecurityMode.None), new EndpointAddress("net.tcp://localhost:8001/Announcement")));
    _serviceHost.Description.Behaviors.Add(discoveryBehavior);
    _serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());

It seems that the service can work.

But on one of client that want to only use contract IFooService, I always got the following error when I try to discover it.

Unable to discover the endpoint for contract.IFooService. Either no service exists or it does not support discovery.

Do you think if it is possible to achieve discoverable multiple contracts in one service in this case?

If YES, how can I do that based on the code i have right now?.

Thanks.

I would think it would be possible. Try doing the simplest thing first to see if that works:

string serviceAddress = "net.tcp://localhost:8088/FooBarService";

ServiceHost selfServiceHost = new ServiceHost(typeof(FooBarService));            

// The endpoints need to share this binding.
var binding = new NetTcpBinding();

selfServiceHost.AddServiceEndpoint(typeof(IFooService), binding, serviceAddress);
selfServiceHost.AddServiceEndpoint(typeof(IBarService), binding, serviceAddress);

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

// Add a UdpDiscoveryEndpoint
selfServiceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());

Client:

static EndpointAddress FindCalculatorServiceAddress()
{
    // Create DiscoveryClient
    DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());

    // Find ICalculatorService endpoints            
    FindResponse findResponse = discoveryClient.Find(new FindCriteria(typeof(IFooService)));
    if (findResponse.Endpoints.Count > 0)
    {
        return findResponse.Endpoints[0].Address;
    }
    else
    {
        return null;
    }
}

Also make sure that you can create clients with the two different contracts and call the services manually.

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