Philipp Stauss
Philipp Stauss

Reputation: 731

How do I combine my WCF webHttpBinding with a customBinding to change webMessageEncoding?

I have implemented an IIS hosted WCF Restful service which is running with the following configuration (simplified, just the relevant configuration here):

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

As long as I am running the service with my old webHttpBinding everything is fine, but as soon as I switch to the custom configuration I get a 404 status code when I invoke the service.

I need the customBinding because I have to use an incoming Stream for the service and I do need the content type "application/JSON" instead of only "text/plain" to be supported (Quite similar to this problem).

But how do I configure my binding correctly? I tried to follow this approach but I do not succeed.

Somehow I have to combine / convert my webHttpBinding with my customBinding. What do I need to do?

Upvotes: 1

Views: 1368

Answers (1)

Jacob Hansen
Jacob Hansen

Reputation: 106

I can see that you in the old binding used it as an https binding because of the security mode that you have added.

            <security mode="Transport">
                <transport clientCredentialType="None"/>
            </security>

To have this same behavior in custom bindings you need to change it to httpsTransport instead of httpTransport in the following way:

    <customBinding>
        <binding name="CustomMapper">          
            <webMessageEncoding webContentTypeMapperType="CustomTypeMapper, MyService.Lib" />
            <httpsTransport authenticationScheme="None" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" 
                manualAddressing="true" />          
        </binding>
    </customBinding>

Upvotes: 2

Related Questions