【问题标题】:Why is WCF replacing my endpoint name?为什么 WCF 替换我的端点名称?
【发布时间】:2013-05-07 19:55:11
【问题描述】:

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

<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" />

现在,名称为 BasicHttpBinding_ISomeService 的端点永远不会在服务的 Web 配置文件中定义。当我尝试使用以下代码创建新通道工厂时:

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 是从哪里来的,为什么我的原始端点名称被覆盖了,我怎样才能让服务识别我试图命中的端点?

【问题讨论】:

    标签: .net wcf service wcf-configuration


    【解决方案1】:

    当您在服务端定义端点时,您不需要指定名称。但是在客户端,您应该提供唯一的名称,以防您想使用 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);
    

    希望这会有所帮助,

    卢卡斯

    【讨论】:

      【解决方案2】:

      这里的混淆点是 WCF 服务配置中的大多数“名称”值是未在服务之外发布的实现细节。端点名称、绑定名称、服务行为名称仅对服务和客户端可见。

      当您创建服务引用时,客户端会根据已发布的服务信息生成一个端点名称:地址、绑定和合同...并提出名称“BasicHttpBinding_ISomeService”。您会发现所有对使用 BasicHttpBinding 公开 ISomeService 的服务的服务引用都具有相同的名称。

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

      调用服务的最简单方法是使用服务引用生成的客户端。如果您使用该向导选择的默认 ServiceReferece1 命名空间:

      ServiceReference1.SomeServiceClient client = new ServiceReference1.SomeServiceClient("endpointname");
      

      【讨论】:

      • Ernie,我能够得到预期的结果,但只有在删除有问题的 &lt;service&gt; 节点并使用 WCF 配置编辑器重新创建之后。我仍然无法确定该节点发生了什么会导致客户端无法识别它,因为其他所有服务都正常运行。任何人,感谢您的意见。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      • 2010-10-19
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多