webHttpBinding使用webMessageEncoding:如何配置?

时间:2009-09-12 13:18:18

标签: wcf .net-3.5 rest

我有一个REST WCF服务。它使用webHttpBinding,配置如下:

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

CustomMapper用于应用自定义WebContentTypeMapper,我试图像这样配置:

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

但我无法弄清楚我的web.config中应该插入这些行:

  • 如果我将这些行放在下面,我会收到错误,因为webMessageEncoding不是公认的元素。
  • 如果我将这些行放在自定义绑定标记下面,我会收到一个错误,即wsHttpBinding没有定义CustomMapper!?

有人可以解释如何将自定义类型映射器与webHttpBinding结合使用吗?

1 个答案:

答案 0 :(得分:5)

如果您定义完整的自定义绑定(与此处CustomMapper一样):

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

然后您需要在服务端点中使用该自定义绑定 - 而不是webHttpBinding!此配置部分只定义bindingConfiguration!

在此处尝试此配置:

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

马克