WebContentFormat.Raw in your WCF config file

by david 2. August 2010 18:42

If you want to specify WebContentFormat.Raw in your WCF service - so that it can process any content-type, but want that to happen from the config file and not programatically, here is one approach.

 

1) Add an overridden WebContentTypeMapper in your code, like this:

 

using System;

using System.ServiceModel.Channels;

 

namespace ebMS3.ServiceModel

{

    public class RawContentTypeMapper : WebContentTypeMapper

    {

        public override WebContentFormat

                GetMessageFormatForContentType(string contentType)

        {

            return WebContentFormat.Raw;

        }

    }

}

 

2) Reference this new WebContentTypeMapper in your config file in a customBinding:

 

<bindings>

  <customBinding>

    <binding name="RawReceiveCapable">

      <!-- Provide the fully qualified name of the WebContentTypeMapper  -->

      <webMessageEncoding webContentTypeMapperType=

          "ebMS3.ServiceModel.RawContentTypeMapper, ebMS3.ServiceModel,

                  Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />

      <httpTransport manualAddressing="true" maxReceivedMessageSize="524288000"

            transferMode="Streamed" />

      <!-- maxReceivedMessageSize is 500 Mb -->

    </binding>

  </customBinding>

</bindings>

 

3) Finally, use the new customBinding on your endpoint, also in your config file:

 

<endpoint contract="MyContract" behaviorConfiguration="MyEndpointBehavior"

binding="customBinding" bindingConfiguration="RawReceiveCapable" />

That's it, another job done...

About the author

David

I'm a C# developer having worked with .Net since it was in beta.  Before that I mainly worked in C and C++.  I have been developing commercial software for more than 20 years.  I also mess around with microprocesors, but that's just for fun.  I live near Cambridge, England and at the moment I'm contracted to one of the departments at Cambridge University.

Related Links

Tag cloud