【问题标题】:Change wsHttpBinding binding to webHttpBinding将 wsHttpBinding 绑定更改为 webHttpBinding
【发布时间】:2012-09-21 13:55:02
【问题描述】:

我正在努力解决 WCF 服务配置的问题。 我有以下配置:

<behaviors>
  <serviceBehaviors>
    <behavior name="SampleWebBehavior">
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
    <behavior name="MyServiceTypeBehaviors">
      <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<protocolMapping>
  <add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false"/>

它运行良好,我可以尝试使用 WcfTestClient.exe 的方法并获得正确的响应。但我需要绑定是 webHttpBinding,这样我才能在浏览器中看到结果并创建 JSON 请求和响应。 但是当我将绑定更改为 webHttpBinding 时,它会抛出一个错误:

由于 EndpointDispatcher 的 ContractFilter 不匹配,接收方无法处理带有 Action '' 的消息。这可能是因为合约不匹配(发送方和接收方之间的操作不匹配)或发送方和接收方之间的绑定/安全不匹配。检查发送方和接收方是否具有相同的合同和相同的绑定(包括安全要求,例如消息、传输、无)。

感谢您的帮助。

【问题讨论】:

  • 我已经尝试将 [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)] 添加到类

标签: c# wcf


【解决方案1】:

要实现这一点,您需要在配置中做一些事情

1) 为您的服务添加另一个端点,您可以在下面看到我同时拥有 basicHttp 和 webHttp

    <system.serviceModel>
        <services>
          <service name="<serivceclass>" behaviorConfiguration="DefaultBehavior">
            <endpoint binding="basicHttpBinding" contract="<serviceinterface>" bindingConfiguration="soapBinding"/>
            <endpoint address="json" binding="webHttpBinding" behaviorConfiguration="jsonBehavior" contract="<serviceinterface>" bindingConfiguration="jsonBinding"/>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
          </service>
 </system.serviceModel>

2) 添加您的绑定配置(您将再次看到 web 和 basichttp)

<system.serviceModel>
<bindings>
      <basicHttpBinding>
        <binding name="soapBinding" maxBufferPoolSize="9000000" maxBufferSize="9000000" maxReceivedMessageSize="9000000">
          <readerQuotas maxArrayLength="9000000" maxBytesPerRead="9000000" maxDepth="9000000" maxNameTableCharCount="9000000" maxStringContentLength="9000000"/>
          <security mode="None">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </basicHttpBinding>
      <webHttpBinding>
        <binding name="jsonBinding">
          <security mode="None">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </webHttpBinding>
    </bindings>   
  </system.serviceModel>

3) 设置您的行为 - 注意名称以及它们如何与步骤 1 中列出的端点相关

<system.serviceModel>    
<behaviors>
          <endpointBehaviors>
            <behavior name="jsonBehavior">
              <webHttp defaultOutgoingResponseFormat="Json" />
            </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="DefaultBehavior">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
              <dataContractSerializer maxItemsInObjectGraph="9000000" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
</system.serviceModel>

虽然我启用和配置的一些内容是可选的,但这允许您以两种方式访问​​服务,一种方式使用 web 和 json 内容,另一种方式使用 Visual Studio 内置的工具,当您不使用 javascript 等时。

注意 1:当您使用 json 部分时,端点将是例如 http://myhost.com/myservice.svc/json/MyMethodName,您可以通过修改服务的相应端点行上的“地址”属性来更改此设置(查看基本地址如何为空和webHttp 是 'json')

【讨论】:

    【解决方案2】:

    此错误来源的一种可能性是:当您更改配置时,您在服务器或客户端上进行了更改,但不是在两者上都进行了更改。服务端和客户端的配置必须匹配。

    【讨论】:

      猜你喜欢
      • 2011-02-08
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-28
      • 2017-05-15
      相关资源
      最近更新 更多