Search This Blog

Thursday, December 17, 2009

My experience with RESTful in Windows Comunication Foundation (WCF) and .NET 3.5

I always thought that it would be nice to share my programming experience with others. I finally made myself to write a blog. So here it is...

After working with Python and CherryPy I had a pleasure to write a RESTfull webservice using WCF in Visual Studio 2008 and .NET 3.5.
At the start I have switched off sessions and wrote a custom request authentication.
I liked XML processing with DataContract and CollectionDataContract. Unfortunately it appeared that they work nice only one way (at least if you are not a fan of using schema).
I have written deserialization manually.

Routing in WCF is done based on UriTemplate, a bit oldish comparing to the way CherryPy works, but C# is not a dynamic language as Python is, so we have to turn a blind eye.

Now one more "funny" think. Please look at the example below, it works perfectly.
[OperationContract]
[WebGet(UriTemplate = "/users")]
public string GetAllUsers()
{
return "GET";
}
[WebInvoke(UriTemplate = "/users", Method = "POST")]
[OperationContract]
public string AddNewUser(Stream stream)
{
return "POST";
}

but what happens if we add QueryString paramaters in GET, like below:

[OperationContract]
[WebGet(UriTemplate = "/users?test={test}")]
public string GetAllUsers(string test)
{
return "GET";
}
[WebInvoke(UriTemplate = "/users", Method = "POST")]
[OperationContract]
public string AddNewUser(Stream stream)
{
return "POST";
}

a GET method of our WebService doesn't work any more, we get 405 StatusCode. Looks like a dispatcher is trying to call POST method instead.
A workaround is to use /users/NEW but in such case is it really a REST support?. The interesting thing is that on msdn there is no similar example of using GET and POST on the same URI.

Personally I am going to avoid WCF for writing RESTful web services in the future. There are freeware solutions much better for this purpose and when we also consider a fact that e.g Python + CherryPy don't need Windows server and IIS a choice is even easier.




No comments:

Post a Comment