WCF:无法调用自己的WCF服务

Bud*_*dda 4 .net wcf wcfserviceclient

我有我的WCF服务,我已经从MSTest项目创建了它的引用.以下是我如何调用服务方法的示例:

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

PublishResult res = serviceClient.PublishEnrollmentProfile(...);
Run Code Online (Sandbox Code Playgroud)

而不是执行我有以下错误:

内容类型application/xml; 响应消息的charset = utf-8与绑定的内容类型不匹配(text/xml; charset = utf-8).如果使用自定义编码器,请确保正确实现IsContentTypeSupported方法.响应的前710个字节是:__CODE__由于EndpointDispatcher上的ContractFilter不匹配,无法在接收方处理"带有Action的消息".这可能是由于合同不匹配(发送方与接收方之间的操作不匹配)或发送方与接收方之间的绑定/安全性不匹配.检查发件人和收件人是否具有相同的合同和相同的绑定(包括安全要求,例如消息,传输,无).---> System.Net.WebException:远程服务器返回错误:(500)内部服务器错误..

据我所知,ContractFilter和EndpointDispatcher之间存在一些问题.我曾试图改善,但发现没什么可以理解的......

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

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

PublishResult res = serviceClient.PublishEnrollmentProfile(...);
Run Code Online (Sandbox Code Playgroud)

这又回来了另一个错误:

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

问题1:

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

Questions2:

我的情况有什么问题?

非常感谢.

PS有些问题我可以使用WcfTestClient连接到服务,更多细节在这里: WCF服务:不能通过'WebHttpBinding'端点调用方法

PPS这是服务器端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>
Run Code Online (Sandbox Code Playgroud)

mar*_*c_s 8

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

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

您需要更改此设置以确保服务端提供的服务端点能够使客户端连接到该服务端点.

所以:

  • 使用basicHttpBinding和连接到该端点,将另一个端点添加到服务配置中

要么:

  • 改变你的客户端使用 webHttpBinding