如何将我的 WCF webHttpBinding 与 customBinding 结合起来以更改 webMessageEncoding?
How do I combine my WCF webHttpBinding with a customBinding to change webMessageEncoding?
我已经实现了一个 IIS 托管的 WCF Restful 服务,它是 运行 以下配置(经过简化,这里只是相关配置):
<system.serviceModel>
<bindings>
<!-- My old working webHttpBinding -->
<webHttpBinding>
<binding name="RestBinding" maxBufferPoolSize="524288" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</webHttpBinding>
<!-- My new custom binding which should replace my webHttpBinding-->
<customBinding>
<binding name="CustomMapper">
<webMessageEncoding webContentTypeMapperType="CustomTypeMapper, MyService.Lib" />
<httpTransport authenticationScheme="None" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
manualAddressing="true" />
</binding>
</customBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="restbehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<!-- My endpoint where I have changed binding from webHttpBinding to customBinding
and bindingConfiguration from RestBinding to CustomMapper -->
<service behaviorConfiguration="servicebehavior" name="MyService">
<endpoint address="rest" binding="customBinding" name="RESTEndPoint" bindingConfiguration="CustomMapper"
behaviorConfiguration="restbehavior" contract="IMyService" />
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
只要我 运行 使用我的旧 webHttpBinding 的服务,一切都很好,但是一旦我切换到自定义配置,我就会在调用该服务时收到 404 状态代码。
我需要 customBinding,因为我必须为服务使用传入流,我确实需要内容类型 "application/JSON" 而不是仅支持 "text/plain"(非常类似于 this问题)。
但是如何正确配置我的绑定?我尝试遵循 this 方法,但没有成功。
我必须以某种方式将我的 webHttpBinding 与我的 customBinding 结合/转换。我需要做什么?
我看到您在旧绑定中使用它作为 https 绑定,因为您添加了安全模式。
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
要在自定义绑定中具有相同的行为,您需要按以下方式将其更改为 httpsTransport 而不是 httpTransport:
<customBinding>
<binding name="CustomMapper">
<webMessageEncoding webContentTypeMapperType="CustomTypeMapper, MyService.Lib" />
<httpsTransport authenticationScheme="None" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
manualAddressing="true" />
</binding>
</customBinding>
我已经实现了一个 IIS 托管的 WCF Restful 服务,它是 运行 以下配置(经过简化,这里只是相关配置):
<system.serviceModel>
<bindings>
<!-- My old working webHttpBinding -->
<webHttpBinding>
<binding name="RestBinding" maxBufferPoolSize="524288" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</webHttpBinding>
<!-- My new custom binding which should replace my webHttpBinding-->
<customBinding>
<binding name="CustomMapper">
<webMessageEncoding webContentTypeMapperType="CustomTypeMapper, MyService.Lib" />
<httpTransport authenticationScheme="None" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
manualAddressing="true" />
</binding>
</customBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="restbehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<!-- My endpoint where I have changed binding from webHttpBinding to customBinding
and bindingConfiguration from RestBinding to CustomMapper -->
<service behaviorConfiguration="servicebehavior" name="MyService">
<endpoint address="rest" binding="customBinding" name="RESTEndPoint" bindingConfiguration="CustomMapper"
behaviorConfiguration="restbehavior" contract="IMyService" />
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
只要我 运行 使用我的旧 webHttpBinding 的服务,一切都很好,但是一旦我切换到自定义配置,我就会在调用该服务时收到 404 状态代码。
我需要 customBinding,因为我必须为服务使用传入流,我确实需要内容类型 "application/JSON" 而不是仅支持 "text/plain"(非常类似于 this问题)。
但是如何正确配置我的绑定?我尝试遵循 this 方法,但没有成功。
我必须以某种方式将我的 webHttpBinding 与我的 customBinding 结合/转换。我需要做什么?
我看到您在旧绑定中使用它作为 https 绑定,因为您添加了安全模式。
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
要在自定义绑定中具有相同的行为,您需要按以下方式将其更改为 httpsTransport 而不是 httpTransport:
<customBinding>
<binding name="CustomMapper">
<webMessageEncoding webContentTypeMapperType="CustomTypeMapper, MyService.Lib" />
<httpsTransport authenticationScheme="None" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
manualAddressing="true" />
</binding>
</customBinding>