WebContentFormat.Raw in your WCF config file

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:




    <binding name="RawReceiveCapable">

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

      <webMessageEncoding webContentTypeMapperType=

          "ebMS3.ServiceModel.RawContentTypeMapper, ebMS3.ServiceModel,

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

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

            transferMode="Streamed" />

      <!-- maxReceivedMessageSize is 500 Mb -->





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

Alternative WCF ServiceHost

I'm writing a test harness that will try out different WCF services and allow us to fire test data at them.  But I also want to be able to override some of the behaviour of the WCF services during the tests.  So I've built some code to look inside the assemblies in a particular folder and determine if they contain any classes that implement the WCF service interfaces.  Then I can create instances of those classes at runtime and host them as WCF services inside my test harness.  This means that I can switch between various new WCF classes from the UI of the test harness.  However, when swapping the service at runtime, I was getting this error: System.InvalidOperationException: Service 'TestHarness.ATestService' has zero application (non-infrastructure) endpoints.  That's because I was now using a differently named WCF service than the one named in the config file.  So, this was my solution:


public class NamedServiceHost : ServiceHost


    public static string configName { get; set; }


    public NamedServiceHost(object singletonInstance, params Uri[] baseAddresses)

        : base(singletonInstance, baseAddresses)

    { }


    protected override void ApplyConfiguration()


        if (!string.IsNullOrEmpty(configName))


            Description.ConfigurationName = configName;






I can use it instead of the normal ServiceHost class, like this:


NamedServiceHost.configName = "MyNamespace.MyService";

ServiceHost = new NamedServiceHost(singletonInstance);



Now I can tell the service host what name I'd like to use from the config file, rather than letting it pick based on the name of the WCF class.  Unfortunately the ApplyConfiguration() method is called from the constructor of the base class, so the easiest way to tell the class what configuration name you'd like to use is by setting a static property.