gpt4 book ai didi

c# - 在一个 WCF 服务中托管多个契约(Contract)

转载 作者:可可西里 更新时间:2023-11-01 02:59:28 25 4
gpt4 key购买 nike

<分区>

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

当我运行服务器时,出现以下错误:

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

我的目标是让客户端调用 Foo(或 Bar)并且只看到每个可用的方法。在我的实际应用程序中,我有大约 10 个域实体,每个域实体有大约四个操作。我尽量不要在一个界面中包含 40 种方法。而且我不想托管 10 个不同的 WCF 服务来执行此操作。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com