简体   繁体   English

使用WCF发现无法在客户端上发现一项服务中的多个合同

[英]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. 我想开发一个WCF服务来支持多个合同。 I managed to make this work by following the instruction from this post on Stackoverflow. 我设法按照Stackoverflow上这篇文章中的说明进行了这项工作。

So basically create one service(FooBarService) to implement multiple contracts(IFooService, IBarService). 因此,基本上创建一个服务(FooBarService)来实现多个合同(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. 但是具有挑战性的事情是,我还想使用WCF Discovery使该服务可发现,以便使客户端和服务与终结点绑定脱钩。

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. 但是,在只希望使用合同IFooService的客户端之一上,当我尝试发现它时总是收到以下错误。

Unable to discover the endpoint for contract.IFooService. 无法发现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. 另外,请确保您可以使用两个不同的合同创建客户端并手动调用服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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