【问题标题】:WCF: Can't call own WCF serviceWCF:无法调用自己的 WCF 服务
【发布时间】:2011-01-10 16:51:56
【问题描述】:

我有我的 WCF 服务,我从 MSTest 项目创建了对它的引用。这是我如何调用服务方法的示例:

IEnrollmentService serviceClient = ChannelFactory<IEnrollmentService>
    .CreateChannel(new BasicHttpBinding(),
                   new EndpointAddress("http://localhost/EnrollmentService.svc"));

PublishResult res = serviceClient.PublishEnrollmentProfile(...);

我得到了以下错误,而不是执行:

内容类型application/xml; 响应消息的charset=utf-8 与内容类型不匹配 绑定(文本/xml;字符集=utf-8)。如果 使用自定义编码器,请确保 IsContentTypeSupported 方法是 正确实施。第一个710 响应的字节为:'Sendera:ActionNotSupported消息 操作 '' 无法在 接收者,由于 ContractFilter EndpointDispatcher 不匹配。 这可能是因为 合同不匹配(不匹配的操作 发送者和接收者之间)或 之间的绑定/安全不匹配 发送者和接收者。检查 发送者和接收者具有相同的 合同和相同的约束力 (包括安全要求,例如 消息,传输, 没有)。'。 ---> System.Net.WebException:远程 服务器返回错误:(500) 内部服务器错误..

据我了解,ContractFilter 和 EndpointDispatcher 之间存在一些问题。我尝试过goodgle,但没有发现任何可以理解的......

我也尝试过以另一种方式调用 wcf 服务方法:

EnrollmentServiceClient serviceClient = new EnrollmentServiceClient("http://localhost/EnrollmentService.svc");

PublishResult res = serviceClient.PublishEnrollmentProfile(...);

这又返回了一个错误:

找不到端点元素 姓名 'http://localhost/McActivation/EnrollmentService.svc' 和合同 'EnrollmentServiceReference.IEnrollmentService' 在 ServiceModel 客户端 配置部分。这可能是 因为没有配置文件 为您的应用程序找到了,或者因为 没有与此名称匹配的端点元素 可以在客户端元素中找到..

问题1:

实例化 wcf 服务客户端的正确方法是什么?

问题2:

我的情况有什么问题?

非常感谢。

附:对于一些问题,我可以使用 WcfTestClient 连接到服务,这里有更多详细信息: WCF service: Can't call methods through the 'WebHttpBinding' endpoint

附言这是服务器端 WCF 服务配置:

<system.serviceModel>
<services>
  <service name="McActivationApp.EnrollmentService" behaviorConfiguration="McActivationApp.EnrollmentServicBehavior">
    <endpoint address="" binding="webHttpBinding" contract="McActivationApp.IEnrollmentService"/>
    <endpoint address="mex" binding="mexHttpBinding" contract="McActivationApp.IEnrollmentService" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="McActivationApp.EnrollmentServicBehavior">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="False" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>

【问题讨论】:

标签: .net wcf wcfserviceclient


【解决方案1】:

您的问题是:您的 服务配置 定义了一个 webHttpBinding 端点 - 这是一个 REST ("Xml-over-HTTP") 端点....

然而,您的客户端使用 basicHttpBinding,这是一个 SOAP 绑定 - 它们不兼容!

您需要更改此设置以确保服务端提供的服务端点是客户端可以连接到它的。

所以:

  • 使用basicHttpBinding 将另一个端点添加到您的服务配置并连接到该端点

或:

  • 将您的客户端更改为使用webHttpBinding

【讨论】:

  • 我为双方的basicHttpBinding做了那个......非常感谢!但是,如果服务器配置为使用“webHttpBinding”并且我将客户端切换为使用“webHttpBinding”,我仍然会收到“由于 EndpointDispatcher 的 ContractFilter 不匹配...”错误消息,接收器无法处理带有 Action 的消息.
  • 另外,第一种实例化服务的方法是正确的,第二种仍然行不通
  • @Budda:如果您将客户端切换到 webHttpBinding,您也会从 SOAP 更改为 REST,这意味着:完全重新创建您的客户端代理。这两个标准完全不同,仅仅改变客户端配置是不够的.....
  • @Budda:您尝试创建客户端代理的第二种方式期望获得配置文件中端点配置部分的 name (&lt;endpoint name="XYZ"&gt; ) - 不是您提供的 URL……没有构造函数可以只接收服务 URL。
  • 是的,第二种方式(当端点名称用作构造函数参数时)也可以使用...之前它表明 URI 有问题...非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多