views:

2592

answers:

4

I'm building a WCF service based on a W3C specification which defines a RESTful web service endpoint that accepts "application/x-www-form-urlencoded" post data. WCF doesn't support this type of message encoding by default and I have found a number of different examples of creating a contract that looks like this:

XElement Query_Post(Stream postData);

And then within the implementation decoding the postData stream using the HttpUtility.ParseQueryString method.

Does anyone know of a more strongly typed way of supporting "application/x-www-form-urlencoded" in WCF?

I would like my operation contract to be:

XElement Query_Post(string query, string [] params);
A: 

See the WCF REST Starter Kit

John Saunders
Do you have a link to some docs which specifically describe enabling "application/x-www-form-urlencoded" HTTP POST support with WCF REST Starter Kit? I'll take a closer look at WCF REST Starter Kit but any specific links or pointers to what objects I should use would be great.
spoon16
No, sorry, I don't have anything more specific. OTOH, I'm not sure that anything more specific is necessary. application/x-www-form-urlencoded sounds like the default for a POST.
John Saunders
It is the default when posting from a web form. But is not the input format that WCF expects (even when using the WCF REST Starter Kit).
spoon16
May I ask what format is being used by default with the REST Starter Kit?
John Saunders
+3  A: 

The best way is to use Stream like Raw HTTP POST with WCF or what you are saying. The reason is because WCF abstracts all the communication-level physical layout stuff out from the service code. Ideally, you would want to make a service that could turn into SOAP or REST just by flipping the switch.

To support it natively, you probably have to extend WebHttpBinding or make your own binding and implement custom encoder. This is symmetric to the output like the linked post says. You have to twist its arms to get WCF to output non-XML/JSON stuff.

eed3si9n
A: 

The REST STarter Kit allows you to handle form post as a NameValueCollection and has a sample demonstrating that. Does that work for you?

Also, what are the reasons why taking the the input as a Stream and parsing it does not work?

Thanks Vish

+2  A: 

The WCF REST Contrib library enables this functionality:

http://wcfrestcontrib.codeplex.com/

It includes a POX formatter and form url encoded formatter and allows you to easily create your own. Formatters are mapped to mime types and automatically selected to serialize/deserialize the entity body based on the content type and accept headers.

Mike OBrien