Exposing an REST Endpoint (POST) which processes Xml

For my project I needed to expose an Endpoint, which accepts an HTTP Post with Xml as input/output. As I like to start small I’ve tried to start with a small test project. In this project, I wanted to learn how to expose an endpoint, how to set everything up and what options are there.

This, because there are a lot of resources on all sort of specific issues, but the ones I faced were scattered in several blogposts.

So for my project I developed a WCF Service, which was created using a WCF Webrole so that I can also publish it in Azure.

·         Create a new WCF Webrole

o   Create a new Cloud service

clip_image002

o   Create a new web role

clip_image004

o   Which should result in a similar structure

clip_image006

·         Configure the endpoint to use WCF-WebHTTP and allow a help page

At this point, you have a WCF Service, which is not yet exposed a WCF-WebHTTP endpoint. We can do this by:

o   simply changing the Web.Config

clip_image008

clip_image010

 

o   Add a method which accepts Xml and decorate it with the ‘WebInvoke’ attribute

clip_image012

 

·         View the help page

After deploying the webservice, we can navigate to the help page

clip_image014

·         Now we can view specific methods, and for each method the parameters are shown

I want to look into each specific option…

DataContract with CompositeType

This allows specifying a custom type, this is serialized into Xml and can be used and tested using the WCF Test client and is quite straightforward

clip_image016

We can use Fiddler to test this. Set the following:   Content-Type: text/xml

clip_image018

clip_image020

Primitive types

Primitive types, can be processed using supported formats out of the box, which are JSON / XML.

clip_image022

Format JSON supports sending plain data, we can do this using Fiddler, by setting Content-type: application/json

clip_image024

clip_image026

We see however, that the response is of the type XML format, so when using JSON, we need to explicitly set the response format.

We can also see that (although I’m not the single point of truth), the Format XML does not support sending plain data, format Xml uses a serializer, thus expects the input wrapped in an Xml string.

So a request must be submitted (according to the help page) as

clip_image028

A test using the xml format, using Fiddler, can be done by setting Content-type:  text/xml.

clip_image030

There is one option you can apply on the ServiceContract level, and that is, setting the XmlSerializerFormat.

clip_image032

This makes life a little bit easier….

clip_image034

However, this does not work….and I gave up…..for a day. My conclusions so far were:

·         Working with primitive types is not that simple L

·         Formats are limited (for my purpose) and I need to write a custom format

·         If such a simple approach already leads to this type of issues, how will I ever manage to process Xml

Xml Documents

The next day…i thought about my plan...All I wanted, was to start with the basics and build my way up, to send/receive Xml. I did this trying to start with primitive types and then use an XmlDocument etc. However, it didn’t worked as I wanted, so I thought…..let’s start from scratch, using XmlDocuments.

clip_image036

Using Fiddler to submit a request, using Content-Type: text/xml..

clip_image038

Response…bad request?

clip_image040

No….wrong code…you should always set the XmlSerializerFormat! (Unless using the Composite type!)

clip_image042

Victory!

clip_image043

 

So I learned 5 things:

·         The WebHTTP Help (<webHttp helpEnabled="true" />) is incredibly useful

·         WCF-WebHTTP works with primitive types using the XML format by default,this  implies the XmlSerializer, when you don’t want this, you need to implement your custom format

http://msdn.microsoft.com/en-us/library/ee476510.aspx

http://blogs.msdn.com/b/endpoint/archive/2010/02/01/returning-custom-formats-from-wcf-webhttp-services.aspx

·         Processing Xml can be done by creating a custom CompositeType, or by using an XmlDocument

·         The Content-Type is something you need to set right

·         As always, Fiddler is a great tool

 

HTH,

 

Sander

 

Comments

Popular posts from this blog

Azure implementation guidelines

UK Connected Systems User Group – BizTalk Services questions

Setting up a build server with the BizTalk Deployment Framework