加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – WCF执行错误:此工厂启用了手动寻址,因此必须预先发送所有

发布时间:2020-12-16 01:59:36 所属栏目:百科 来源:网络整理
导读:我有一个使用WebHttpBinding托管的WCF服务.服务非常简单,是一个接受多个参数的操作合同.使用“添加服务引用”后自动生成的我的WCF客户端无法直接使用WCF服务.该错误仅发生在WebHttpBinding而不是其他错误. 服务器端 [OperationContract][WebInvoke(Method =
我有一个使用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>

问题应该解决.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读