将 wsHttpBinding 绑定更改为 webHttpBinding



我正在努力解决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时,它会抛出一个错误:

带有操作"的消息无法在接收方处理,因为端点调度程序的合同过滤器不匹配。这可能是由于协定不匹配(发送方和接收方之间的操作不匹配)或发送方和接收方之间的绑定/安全不匹配。 检查发送方和接收方是否具有相同的协定和相同的绑定(包括安全要求,例如消息、传输、无)。

感谢任何帮助。

要做到这一点,你需要在你的配置中做一些事情

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,您可以通过修改服务的相应端点行上的"address"属性来更改此设置(请参阅基本地址如何为空,webHttp为"json")

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

最新更新