简体   繁体   中英

Change wsHttpBinding binding to webHttpBinding

I am struggeling with a problem with WCF Service configuration. I have following config:

<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"/>

It works perfectly, I can try the methods with WcfTestClient.exe and get correct responses. But I need the binding to be webHttpBinding, so I can see the results in browser and create JSON requests and responses. But when I change the binding to webHttpBinding , it throws an error:

The message with Action '' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, eg Message, Transport, None).

Thaks for any help.

To pull this off you need to do a few things in your config

1) add another endpoint to your service, you can see below I have both basicHttp and 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) add your binding config (again you'll see both web and 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) Setup your behaviors - notice the names and how they correlate tot the endpoints listed in step 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>

While some of the stuff I enable and config are optional this allows you to access the service both ways, one way using web and json stuff and another using the tooling built into visual studio when you are not working in javascript,etc.

Note1: the endpoint when you are using the json part will be for example http://myhost.com/myservice.svc/json/MyMethodName , You can change this by modifying the "address" attribute on the appropriate endpoint line for your service (see how basic address is empty and the webHttp is 'json')

one possibility of the source of this error is: When you changed the configuration you made the change on the server or the client but not on both. The configuration on the server and client must match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM