簡體   English   中英

將wsHttpBinding綁定更改為webHttpBinding

[英]Change wsHttpBinding binding to 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時 ,它將引發錯誤:

由於EndpointDispatcher的ContractFilter不匹配,因此無法在接收方處理帶有動作''的消息。 這可能是由於合同不匹配(發送方和接收方之間的操作不匹配)或發送方和接收方之間的綁定/安全不匹配造成的。 檢查發送方和接收方是否具有相同的合同和相同的綁定(包括安全要求,例如,消息,傳輸,無)。

感謝任何幫助。

要做到這一點,您需要在配置中做一些事情

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方式,另一種方式是在不使用javascript時使用Visual Studio內置的工具等。

注意1:使用json部分時的端點將是例如http://myhost.com/myservice.svc/json/MyMethodName ,您可以通過在服務的相應端點行上修改“ address”屬性來更改此端點(查看基本地址如何為空並且webHttp如何為“ json”)

導致此錯誤的一種可能是:更改配置時,您在服務器或客戶端上進行了更改,但未在兩者上進行更改。 服務器和客戶端上的配置必須匹配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM