c# - 执行 : Manual addressing is enabled on this factory, 时出现 WCF 错误,因此发送的所有消息都必须预先寻址

标签 c# wcf webhttpbinding

我有一个使用 WebHttpBinding 托管的 WCF 服务。该服务非常简单,一个接受多个参数的操作合约。使用“添加服务引用”后自动生成的 WCF 客户端无法直接使用 WCF 服务。该错误仅发生在 WebHttpBinding 而不是其他人。

服务器端

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "Submit2String", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped)]
string Submit2String(string input1, string input2);

客户端

ExpenseServiceClient proxy = new ExpenseServiceClient();
proxy.Submit2String("test1", "test2");

当我测试运行上面的代码时,出现以下错误:

Error: InvalidOperationException was unhandled by user code
Manual addressing is enabled on this factory, so all messages sent must be pre-addressed.

这是使用“添加服务引用”后我自动生成的配置文件的样子:

 <system.serviceModel>
  <bindings>
    <webHttpBinding>
      <binding name="webHttp">
        <security mode="None">
          <transport clientCredentialType="None" />
        </security>
      </binding>
    </webHttpBinding>
  </bindings>
  <client>
    <endpoint binding="webHttpBinding" 
              bindingConfiguration="webHttp"
              contract="ExpenseService.IExpenseService"  
              address="http://localhost/myservices/ExpenseService.svc">
    </endpoint>
  </client>
</system.serviceModel>

最佳答案

我意识到只有 WebHttpBinding 有这个问题。要解决这个问题,只需在客户端配置文件中添加一个行为配置,如下所示:

<behaviors>
    <endpointBehaviors>
        <behavior name="webEndpoint">
            <webHttp defaultBodyStyle="Wrapped" 
                     defaultOutgoingResponseFormat="Xml" 
                     helpEnabled="true"/>
        </behavior>
    </endpointBehaviors>
</behaviors>

然后,更新客户端端点以使用上述端点行为。

<client>
    <endpoint binding="webHttpBinding" 
              bindingConfiguration="webHttp" 
              behaviorConfiguration="webEndpoint"  
              contract="ExpenseService.IExpenseService" 
              address="http://myservices/ExpenseService.svc">
    </endpoint>
</client>

问题应该解决了。

关于c# - 执行 : Manual addressing is enabled on this factory, 时出现 WCF 错误,因此发送的所有消息都必须预先寻址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21204638/

相关文章:

C# Return Generic List/IEnumerable,它是如何工作的?

c# - ASP.NET 工作线程的大部分事件时间是否都处于阻塞状态?

WCF 休息 : remove prefix "ArrayOf" for wcf method response

wcf - 解决方案中的多个 WCF 项目与单个项目

c# - WCF服务停止处理调用15秒钟

c# - 如何编码音频文件并统一保存?

c# - 无法将AWS Lambda中的.net core API连接到RDS

WCF 消息和数据协定、DTO、域模型和共享程序集

c# - 错误 : Cannot obtain Metadata; using WCF Test client, C#,并尝试实现 webhttpbinding 和 json