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

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

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

在服务“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(netTcpBinding, endpointAddress); IFooService channelFoo = channelFactoryFoo.CreateChannel(); Debug.WriteLine(channelFoo.FooMethod1()); // Call IBarService var channelFactoryBar = new ChannelFactory(netTcpBinding, endpointAddress); IBarService channelBar = channelFactoryBar.CreateChannel(); Debug.WriteLine(channelBar.BarMethod1()); } 

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

正如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(netTcpBinding, endpointAddress); IFooService channelFoo = channelFactoryFoo.CreateChannel(); Console.WriteLine(channelFoo.FooMethod1()); // Call IBarService var channelFactoryBar = new ChannelFactory(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"; } }