承载多个合同,在一个WCF服务 [英] Host multiple contracts in one WCF service

查看:132
本文介绍了承载多个合同,在一个WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能举办一个WCF服务的多个服务合同?如果是这样,怎么样?我一直在使用Google,有的帖子说,你能做到这一点(但不知道如何)和其他人说这是不可能的。

Is it possible to host multiple service contracts in one WCF service? If so, how? I've been googling and some posts say you can do it (but not how) and others have said it's just not possible.

当我运行服务器,我得到这个错误:

When I run the server, I get this error:

合同名称'ConsoleAppWcfCommon.IBarService找不到
由实施服务合同的列表
'ConsoleAppWcfServer.FooService

The contract name 'ConsoleAppWcfCommon.IBarService' could not be found in the list of contracts implemented by the service 'ConsoleAppWcfServer.FooService'.

这是我的服务器代码:

    static void Main(string[] args)
    {
        string serviceAddress = "net.tcp://localhost:8088/FooBarService";

        // I'm stuck here as I have to pick *one* service
        ServiceHost selfServiceHost = new ServiceHost(typeof(FooService));            

        // I can add both endpoints here, but this is what gives me the error.
        selfServiceHost.AddServiceEndpoint(typeof(IFooService), new NetTcpBinding(), serviceAddress);
        selfServiceHost.AddServiceEndpoint(typeof(IBarService), new NetTcpBinding(), serviceAddress);

        selfServiceHost.Open();
        Console.ReadLine();
        selfServiceHost.Close();
    }



这是客户端代码:

    static void Main(string[] args)
    {
        NetTcpBinding netTcpBinding = new NetTcpBinding();

        EndpointAddress endpointAddress = new EndpointAddress("net.tcp://localhost:8088/FooBarService");

        // Call IFooService
        var channelFactoryFoo = new ChannelFactory<IFooService>(netTcpBinding, endpointAddress);
        IFooService channelFoo = channelFactoryFoo.CreateChannel();
        Debug.WriteLine(channelFoo.FooMethod1());

        // Call IBarService
        var channelFactoryBar = new ChannelFactory<IBarService>(netTcpBinding, endpointAddress);
        IBarService channelBar = channelFactoryBar.CreateChannel();
        Debug.WriteLine(channelBar.BarMethod1());
    }



我的目标是让客户做出美孚(或酒吧)的调用只看到可用的方法每个。在我的实际应用中,我有大约10个域实体与各约四则运算。我想不会有一个接口,在这40种方法。而且我不希望有举办10个不同的WCF服务做到这一点。

My goal is to let the client make a call to Foo (or Bar) and only see the methods available to each. In my real application, I have about 10 domain entities with about four operations on each. I'm trying not to have one interface with 40 methods in it. And I don't want to have to host 10 different WCF services to do this.

推荐答案

由于marc_s指出,答案是有一个实现这两个接口一个服务实现类。 ,下面是完整的工作代码

As marc_s pointed out, the answer was to have one service implementation class that implements both interfaces. Below is the full working code.

服务器:

    static void Main(string[] args)
    {
        string serviceAddress = "net.tcp://localhost:8088/FooBarService";

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

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

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

        selfServiceHost.Open();

        Console.WriteLine("The service is ready.");
        Console.WriteLine("Press any key to terminate service.");
        Console.WriteLine();
        Console.ReadKey();

        selfServiceHost.Close();
    }



客户:

    static void Main(string[] args)
    {
        NetTcpBinding netTcpBinding = new NetTcpBinding();

        EndpointAddress endpointAddress = new EndpointAddress("net.tcp://localhost:8088/FooBarService");

        // Call IFooService
        var channelFactoryFoo = new ChannelFactory<IFooService>(netTcpBinding, endpointAddress);
        IFooService channelFoo = channelFactoryFoo.CreateChannel();
        Console.WriteLine(channelFoo.FooMethod1());

        // Call IBarService
        var channelFactoryBar = new ChannelFactory<IBarService>(netTcpBinding, endpointAddress);
        IBarService channelBar = channelFactoryBar.CreateChannel();
        Console.WriteLine(channelBar.BarMethod1());

        Console.ReadKey();
    }



美孚合同:

[ServiceContract]
public interface IFooService
{
    [OperationContract]
    string FooMethod1();

    [OperationContract]
    string FooMethod2();
}



酒吧合同:

[ServiceContract]
public interface IBarService
{
    [OperationContract]
    string BarMethod1();

    [OperationContract]
    string BarMethod2();
}



富服务:

public class FooService : IFooService, IBarService
{
    public string FooMethod1()
    {
        return "FooMethod1";
    }

    public string FooMethod2()
    {
        return "FooMethod2";
    }

    public string BarMethod1()
    {
        return "BarMethod1";
    }

    public string BarMethod2()
    {
        return "BarMethod2";
    }
}

这篇关于承载多个合同,在一个WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆