为什么WCF要替换我的端点名称?



我有一个具有以下端点的服务:

<endpoint address="" binding="basicHttpBinding" bindingConfiguration=""
          name="SomeServiceDotNetEndpoint" contract="Contracts.ISomeService" />

当我将此服务引用添加到另一个项目时,应用程序配置文件显示以下客户端端点:

<endpoint address="http://test.example.com/Services/SomeService.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISomeService"
                contract="SomeService.ISomeService" name="BasicHttpBinding_ISomeService" />

现在,在服务的web配置文件中从未定义名称为BasicHttpBinding_ISomeService的端点。当我尝试用以下代码创建一个新的通道工厂时:

var someServiceChannelFactory = new ChannelFactory<ISomeService>("SomeServiceDotNetEndPoint", endpoint);

它失败了,告诉我在那个地址没有匹配的合约/端点。我也试过使用"BasicHttpBinding_ISomeService",我得到同样的错误。

Could not find endpoint element with name 'SomeServiceDotNetEndPoint' and
contract 'SomeService.ISomeService' in the ServiceModel client configuration
section. This might be because no configuration file was found for your
application, or because no endpoint element matching this name could be found
in the client element.

那么,BasicHttpBinding_ISomeService来自哪里,为什么我的原始端点名称被覆盖,以及我如何让服务识别我试图命中的端点?

在服务端定义端点时,不需要指定名称。但是在客户端,您应该提供唯一的名称,以防您想使用channelFactory来创建代理。

在简介:

服务web . config

<endpoint address="" 
          binding="basicHttpBinding" 
          contract="Contracts.ISomeService" />
客户机app.config

<endpoint address="http://test.com/SomeService"
          binding="basicHttpBinding"
          name="someServiceEndpoint"
          contract="Contracts.ISomeService" />

代码
var someServiceChannelFactory = new ChannelFactory<ISomeService>("someServiceEndpoint", endpoint);

希望有帮助,

卢卡斯

这里令人困惑的一点是,WCF服务配置中的大多数"name"值都是没有在服务外部发布的实现细节。端点名称、绑定名称、服务行为名称只对服务和客户端可见。

当您创建服务引用时,客户端根据发布的服务信息生成端点名称:地址、绑定和契约…并提出了名称"BasicHttpBinding_ISomeService"。你会发现,使用BasicHttpBinding公开ISomeService的服务的所有服务引用都获得相同的名称。

所以您的端点名称没有被覆盖。客户根本不知道它是什么。

调用服务的最简单方法是使用生成的服务引用客户机。如果您使用该向导选择的默认ServiceReferece1名称空间:
ServiceReference1.SomeServiceClient client = new ServiceReference1.SomeServiceClient("endpointname");

最新更新