webHttpBinding using webMessageEncoding: how to co

2019-05-15 06:19发布

问题:

I have a REST WCF service. Its using a webHttpBinding and the configuration looks like this:

<service name="IndexingService.RestService" behaviorConfiguration="IndexingService.Service1Behavior">
    <endpoint
      address=""
      binding="webHttpBinding"
      bindingConfiguration="CustomMapper"
      contract="IndexingService.IIndexingService"
      behaviorConfiguration="webby"/>
</service>

The CustomMapper is used to apply a custom WebContentTypeMapper, which I tried to configure like this:

<binding name="CustomMapper">
        <webMessageEncoding webContentTypeMapperType="IndexingService.CustomContentTypeMapper, IndexingService" />
        <httpTransport manualAddressing="true" />
</binding>

But I cannot figure out where in my web.config I should insert these lines:

  • If I put these lines below I get an error, because webMessageEncoding is not a recognized element.
  • If I put the lines below a custom binding tag, I get an error that wsHttpBinding does not have a CustomMapper defined!?

Can somebody explain how to use a custom type mapper together with webHttpBinding?

回答1:

If you define a complete custom binding (as you do here with CustomMapper):

<binding name="CustomMapper">
   <webMessageEncoding webContentTypeMapperType=
             "IndexingService.CustomContentTypeMapper, IndexingService" />
   <httpTransport manualAddressing="true" />
</binding>

then you need to use that custom binding in your service endpoint - not webHttpBinding! This config section does not define just a bindingConfiguration!

Try this config here:

<system.serviceModel>
  <bindings>
    <customBinding>
       <binding name="CustomMapper">
          <webMessageEncoding webContentTypeMapperType=
                 "IndexingService.CustomContentTypeMapper, IndexingService" />
          <httpTransport manualAddressing="true" />
       </binding>
    </customBinding>
  </bindings>
  <services>
    <service name="IndexingService.RestService"   
             behaviorConfiguration="IndexingService.Service1Behavior">
        <endpoint
           address=""
            binding="customBinding"
            bindingConfiguration="CustomMapper"
            contract="IndexingService.IIndexingService"
            behaviorConfiguration="webby"/>
     </service>
  </services>
</system.serviceModel>

Marc