Skip to content

POX Formatter Overview

mikeobrien edited this page Sep 14, 2010 · 2 revisions

The POX formatter uses the System.Runtime.Serialization.DataContractSerializer, but unlike the WCF REST Contrib xml formatter ([Discussed under Xml Formatter Overview), does not serialize data contract namespaces or xml schema attributes, it does not require namespaces to be specified in xml to be deserialized and it does not require elements to be in a specific order. This enables you to serve and accept very simple xml. It also allows you to reuse data contracts from your SOAP services (that have a namespace specified) in your RESTful services (where you will not use namespaces).

The POX formatter can be configured declaratively on the service with the WcfRestContrib.ServiceModel.Description.WebDispatchFormatterConfigurationAttribute and WebDispatchFormatterMimeTypeAttribute:

[WebDispatchFormatterConfiguration("application/xml")]
[WebDispatchFormatterMimeType(
    typeof(WcfRestContrib.ServiceModel.Dispatcher.Formatters.PoxDataContract),
    "application/xml", 
    "text/xml")]
public class Books : IBooksService {...}

Or in configuration:

<system.serviceModel>
    <extensions>
        <behaviorExtensions>
            <add name="webFormatter" 
                 type="WcfRestContrib.ServiceModel.Configuration.WebDispatchFormatter.ConfigurationBehaviorElement, WcfRestContrib, 
                       Version=x.x.x.x, Culture=neutral, PublicKeyToken=89183999a8dc93b5"/>
        </behaviorExtensions>
    </extensions>
    <serviceBehaviors>
        <behavior name="Rest">
          <webFormatter>
            <formatters defaultMimeType="application/xml">
              <formatter mimeTypes="application/xml,text/xml" 
                         type="WcfRestContrib.ServiceModel.Dispatcher.Formatters.PoxDataContract, WcfRestContrib"/>
            </formatters>
          </webFormatter>
        </behavior>
    </serviceBehaviors>
</system.serviceModel>

One or more formatters can be defined.

NOTE: I use the name “POX” (For lack of a better term) relative to the stock DataContractSerializer, not that POX cant have attributes or namespaces. The namespaces and schema attributes that the DataContractSerializer adds/requires are usually unnecessary and unwanted by some people implementing RESTful services. The POX formatter gets even “plainer” by omitting them altogether as well as not enforcing a particular element order.

NOTE: Internally the POX formatter sorts elements alphabetically before deserialization, which is how the DataContractSerializer reads xml by default. If the data contract is part of an inheritance hierarchy and/or data members are explicitly ordered, this formatter will not work properly (See more here). You will need to instead use the Xml Formatter (See more under Xml Formatter Overview) and make sure elements are in the proper order.

NOTE: The WcfRestContrib.ServiceModel.Web.WebServiceHost allows you to specify configuration based behaviors if you do not want to specify this declaratively. See more about it under Declarative Binding & Behavior Overview.