18

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

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

在服务“ConsoleAppWcfServer.FooService”实施的合同列表中找不到合同名称“ConsoleAppWcfCommon.IBarService”。

这是我的服务器代码:

    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 服务来执行此操作。

4

1 回答 1

27

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

福服务:

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";
    }
}
于 2013-05-11T21:17:05.937 回答