C# · 12月 23, 2021

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

这个问题在这里已经有一个答案:> Run WCF ServiceHost with multiple contracts9个
可以在一个WCF服务中托管多个服务合同吗?如果是这样,怎么样?我一直在谷歌搜索,一些帖子说你可以做(​​但不是如何)和其他人说这是不可能的.

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

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),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服务.

解决方法 正如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.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”; }}