WCF 执行时出错:此工厂启用了手动寻址,因此所有发送的消息都必须预先寻址

编程入门 行业动态 更新时间:2024-10-26 03:28:06
本文介绍了WCF 执行时出错:此工厂启用了手动寻址,因此所有发送的消息都必须预先寻址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个由 WebHttpBinding 托管的 WCF 服务.服务很简单,一个接受多个参数的操作合约.我的 WCF 客户端在使用添加服务引用"后自动生成,无法直接使用 WCF 服务.错误只发生在 WebHttpBinding 上,其他的不发生.

I have a WCF service hosted with WebHttpBinding. The service is very simple, an operation contract which accept multiple parameters. My WCF client, auto generated after using the "Add Service Reference", is not able to directly consume the WCF service. The error only occur for WebHttpBinding but not the others.

服务端

[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");

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

When I test run my above code, I get the following error:

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

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

Here is how my auto generated configuration file look like after using the "Add Service Reference":

 <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 有这个问题.要解决这个问题,只需在客户端配置文件中添加一个行为配置如下:

I realize that only WebHttpBinding has this problem. To solve this problem, just add a behavior configuration in the client side configuration file like this:

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

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

Then, update the client endpoint to use the above endpoint behavior.

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

问题应该得到解决.

这篇关于WCF 执行时出错:此工厂启用了手动寻址,因此所有发送的消息都必须预先寻址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 20:18:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1142782.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工厂   消息   都必须   WCF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!