-
Notifications
You must be signed in to change notification settings - Fork 18
De serialization Overview
The Web Dispatch Formatter enables you to register “formatters” that will automatically serialize and deserailze an entity body based on the content-type and accept headers. A formatter can be assigned to handle one or more mime types. Request and response entity bodies can differ in format; for example a request could have an xml entity body and return a json entity body. The following formatters ship with WCF REST Contrib:
- Form url encoded. See more under the Url Form Encoded Formatter Overview.
- POX (Via DataContractSerializer, no namespaces/attributes, element order not enforced). See more under the POX Formatter Overview.
- Xml (Via DataContractSerializer). See more under the Xml Formatter Overview.
- Json (Via DataContractJsonSerializer). See more under the Json Formatter Overview.
Custom formatters can be created to handle custom formats. See more under Custom Formatters Overview.
To use the web dispatch formatter you will need to register formatters on the service declaratively with the WcfRestContrib.ServiceModel.Description.WebDispatchFormatterConfigurationAttribute (Where you can set the default mime type) and WebDispatchFormatterMimeTypeAttribute (Where you map mime types to a formatter):
[WebDispatchFormatterConfiguration("application/xml")]
[WebDispatchFormatterMimeType(
typeof(WcfRestContrib.ServiceModel.Dispatcher.Formatters.PoxDataContract),
"application/xml",
"text/xml")]
[WebDispatchFormatterMimeType(
typeof(WcfRestContrib.ServiceModel.Dispatcher.Formatters.DataContractJson),
"application/json")]
[WebDispatchFormatterMimeType(
typeof(WcfRestContrib.ServiceModel.Dispatcher.Formatters.FormUrlEncoded),
"application/x-www-form-urlencoded")]
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"/>
<formatter mimeTypes="application/json"
type="WcfRestContrib.ServiceModel.Dispatcher.Formatters.DataContractJson, WcfRestContrib"/>
<formatter mimeTypes="application/x-www-form-urlencoded"
type="WcfRestContrib.ServiceModel.Dispatcher.Formatters.FormUrlEncoded, WcfRestContrib"/>
</formatters>
</webFormatter>
</behavior>
</serviceBehaviors>
</system.serviceModel>
NOTE: Multiple mime types can be specified by separating them with a comma.
Once the formatters have been configured on the service the WcfRestContrib.ServiceModel.Description.WebDispatchFormatterAttribute can be applied to operations where automatic formatting is desired:
[ServiceContract]
public interface IBooksService
{
[WebInvoke(UriTemplate = "/{isbn}", Method=Verbs.Put)]
[WebDispatchFormatter]
[OperationContract]
void AddOrModifyBook(string isbn, Book book);
....
}
In the example above the entity body will be deserialized into a Book object. The web dispatch formatter will examine the content type and choose the appropriate formatter. If no appropriate formatter is found, the formatter that handles the default mime type will be used.
In the following example the web dispatch formatter will examine the accept header and serialize the Book object using the appropriate formatter:
[ServiceContract]
public interface IBooksService
{
[WebGet(UriTemplate = "/{isbn}")]
[WebDispatchFormatter]
[OperationContract]
Book GetBook(string isbn);
....
}
In some instances you may only want the automatic de/serialization to occur in a particular direction. The following example demonstrates how to set the direction the formatter will de/serialize in.
[ServiceContract]
public interface IContentService
{
[WebInvoke(UriTemplate = "/{isbn}", Method=Verbs.Post)]
[WebDispatchFormatter(WebDispatchFormatter.FormatterDirection.Outgoing)]
[OperationContract]
UploadId Upload(Stream content, string isbn);
}
NOTE: A feature request has been submitted to Microsoft to include this feature in the WCF REST API. See more here.
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 the Declarative Binding & Behavior Overview.