在一个WCF服务中托管多个合同

时间:2013-05-11 20:59:28

标签: c# wcf

是否可以在一个WCF服务中托管多个服务合同?如果是这样,怎么样?我一直在谷歌搜索,一些帖子说你可以做(​​但不是如何),其他人说这是不可能的。

当我运行服务器时,我收到此错误:

  

找不到合约名称“ConsoleAppWcfCommon.IBarService”   在服务实施的合同列表中   '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());
    }

我的目标是让客户调用Foo(或Bar),只看到每个可用的方法。在我的实际应用程序中,我有大约10个域实体,每个实体上有四个操作。我试图在其中没有一个包含40个方法的接口。我不想要托管10个不同的WCF服务来实现这一目标。

1 个答案:

答案 0 :(得分:25)

正如marc_s指出的那样,答案是有一个实现两个接口的服务实现类。以下是完整的工作代码。

服务器

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

Foo合同:

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

    [OperationContract]
    string FooMethod2();
}

律师合约:

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

    [OperationContract]
    string BarMethod2();
}

Foo服务:

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