简体   繁体   English

在多个绑定上托管WCF服务的选定方法

[英]Hosting selected methods of a wcf service on multiple binding

I have been asked this question on an interview and i dont know the answer yet. 面试中有人问我这个问题,但我不知道答案。

Question is we have 5 operation contracts in a service. 问题是我们在一项服务中有5个运营合同。 We want to host all of them of one binding, lets say http and only two of them on another binding too. 我们希望将它们全部托管在一个绑定中,比如说http,而其中只有两个托管在另一个绑定中。 So those two methods must be hosted on both the bindings and Without any duplication. 因此,这两种方法必须同时托管在绑定上,并且必须没有任何重复。

How do we do this? 我们如何做到这一点? Is it even possible? 可能吗

Yes, it's possible. 是的,有可能。 We can structure the interface contract as follows to separate out the methods we want available on only one endpoint: 我们可以如下构造接口协定,以分离出我们希望仅在一个端点上可用的方法:

[ServiceContract]
interface ILimitedAvailabilityOp
{
    [OperationContract]
    string OpB();
}

[ServiceContract]
interface IAllOfMyOps : ILimitedAvailabilityOp
{
    [OperationContract]
    string OpA();
}

So in total we have two methods which are both available on IAllOfMyOps via inheritance, and ILimitedAvailabilityOp has the operation we will make available on only one binding. 因此,总共我们有两种方法都可以通过继承在IAllOfMyOps使用,而ILimitedAvailabilityOp具有仅在一个绑定上可用的操作。 For our service implementation, we implement all the operations as below: 对于我们的服务实施,我们执行以下所有操作:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class MyService : IAllOfMyOps
{
    public string OpA()
    {
        return "A";
    }

    public string OpB()
    {
        return "B";
    }
}

When we create the service instance, we can do as follows with two different endpoints having two different bindings. 创建服务实例时,可以对具有两个不同绑定的两个不同端点执行以下操作。 On the net tcp endpoint, we make both ops A and B available, on the named pipe binding just B. 在网络tcp端点上,我们使操作A和B都可用,在仅绑定B的命名管道上。

static void CreateService()
{
    var myService = new MyService();
    var serviceHost = new ServiceHost(myService);
    serviceHost.AddServiceEndpoint(typeof(IAllOfMyOps), new NetTcpBinding(), "net.tcp://localhost:61234/MyService/IAllOfMyOps");
    serviceHost.AddServiceEndpoint(typeof(ILimitedAvailabilityOp), new NetNamedPipeBinding(), "net.pipe://localhost/MyService/ILimitedAvailabilityOp");
    serviceHost.Open();
}

The client to connect would look like this: 连接的客户端如下所示:

class MyClient : ClientBase<IAllOfMyOps>, IAllOfMyOps
{
    public MyClient(Binding binding, EndpointAddress endpoint) : base(binding, endpoint) { }

    public string OpA()
    {
        return base.Channel.OpA();
    }

    public string OpB()
    {
        return base.Channel.OpB();
    }
}

And we'd connect to it like this: 我们将像这样连接到它:

static void Connect()
{
    var tcpClient = new MyClient(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:61234/MyService/IAllOfMyOps"));
    Console.WriteLine(tcpClient.OpA());
    Console.WriteLine(tcpClient.OpB());
    tcpClient.Close();

    var namedPipeClient = new MyClient(new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/MyService/ILimitedAvailabilityOp"));
    Console.WriteLine(namedPipeClient.OpB());
    Console.WriteLine(namedPipeClient.OpA()); // this would fail
    namedPipeClient.Close();
}

Thus we haven't repeated any code anywhere, and we have two ops available on one binding, and only one available on another. 因此,我们没有在任何地方重复任何代码,并且在一个绑定上有两个操作可用,而在另一个绑定上只有一个可用。

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

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