Share via


WebContentTypeMapper Sample

Download sample

This sample demonstrates how to map new content types to Windows Communication Foundation (WCF) message body formats.

Note

This sample requires that .NET Framework version 3.5 is installed to build and run. Visual Studio 2008 is required to open the project and solution files.

The <webHttpBinding> element plugs in the Web message encoder, which allows WCF to receive JSON, XML, or raw binary messages at the same endpoint. The encoder determines the body format of the message by looking at the HTTP content-type of the request. This sample introduces the WebContentTypeMapper class, which allows the user to control the mapping between content type and body format.

Note

The setup procedure and build instructions for this sample are located at the end of this topic.

WCF provides a set of default mappings for content types. For example, application/json maps to JSON and text/xml maps to XML. Any content type that is not mapped to JSON or XML is mapped to raw binary format.

In some scenarios (for example, push-style APIs), the service developer does not control the content type returned by the client. For example, clients might return JSON as text/javascript instead of application/json. In this case, the service developer must provide a type that derives from WebContentTypeMapper to handle the given content type correctly, as shown in the following sample code.

public class JsonContentTypeMapper : WebContentTypeMapper
{
    public override WebContentFormat
               GetMessageFormatForContentType(string contentType)
    {
        if (contentType == "text/javascript")
        {
            return WebContentFormat.Json;
        }
        else
        {
            return WebContentFormat.Default;
        }
    }
}

The type must override the GetMessageFormatForContentType method. The method must evaluate the contentType argument and return one of the following values: Json, Xml, Raw, or Default. Returning Default defers to the default Web message encoder mappings. In the previous sample code, the text/javascript content type is mapped to JSON, and all other mappings remain unchanged.

To use the JsonContentTypeMapper class, the endpoint must use a custom binding.

<customBinding>
    <binding name="JsonMapper">
        <webMessageEncoding webContentTypeMapperType=
"Microsoft.Ajax.Samples.JsonContentTypeMapper, JsonContentTypeMapper, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null" />
        <httpTransport manualAddressing="true" />
    </binding>
</customBinding>

To verify the requirement for using the JsonContentTypeMapper, replace the preceding <webMessageEncoding webContentTypeMapperType= with only <webMessageEncoding /> in the configuration file. The client page fails to load when attempting to use text/javascript to send JSON content.

To set up, build, and run the sample

  1. Ensure that you have performed the One-Time Set Up Procedure for the Windows Communication Foundation Samples.

  2. Build the solution WebContentTypeMapperSample.sln as described in Building the Windows Communication Foundation Samples.

  3. Navigate to https://localhost/ServiceModelSamples/JCTMClientPage.htm (do not open JCTMClientPage.htm in the browser from within the project directory).

© 2007 Microsoft Corporation. All rights reserved.