Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to support "application/x-www-form-urlencoded" post data with WCF?

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);
like image 832
Eric Schoonover Avatar asked Mar 02 '09 23:03

Eric Schoonover


2 Answers

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.

like image 106
Eugene Yokota Avatar answered Nov 15 '22 14:11

Eugene Yokota


The WCF REST Contrib library enables this functionality:

https://github.com/mikeobrien/WcfRestContrib

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.

like image 2
hcoverlambda Avatar answered Nov 15 '22 16:11

hcoverlambda