I have a WCF service that needs to support HTTP request with JSON, but the default serializer (DataContractSerializer) just doesn't do the job very well. I read about Web API is the way to go these days, but all the examples I come across requires using models and only work with simple CRUD operations. My WCF service performs more than CRUD operations. It also accepts/returns data types like list of dictionary. Any suggestion on how should I approach this?
-
WCF is really a pain with json dictionaries. I have moved over to Web Api and it handles serialization (with newtonsoft) and deserialization really well and very intuitively. – brando Aug 12 '15 at 21:39
1 Answers
...the default serializer (DataContractSerializer) just doesn't do the job very well
What job does the DataContractJsonSerializer not do very well? I've not had any problems with it. Regardless, if you want to swap it out for JSON.net, instructions for doing that here and here.
I read about Web API is the way to go these days
Well that is debateable. If you just want an HTTP api then platforms like Nancy are much easier to work with than WebAPI. But you are generally correct, WebApi is preferred to WCF webHttpBinding.
...only work with simple CRUD operations. My WCF service performs more than CRUD operations
This is incorrect, WebAPI can be used in a more RPC style manner. For example, you can do something like this:
[AcceptVerbs("POST")]
public HttpResponseMessage DoSomeAction(Something actionParams)
{
...
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("Action done")
};
}
It's just that this kind of goes against REST principals, which is why the samples you have seen will not generally include this kind of service operation.
As for a recommended approach my order of preference would be:
- Nancy as the preferred option, hosted in a windows service
- WebApi if there is a IIS hosting requirement
- WebHttpBinding if no IIS requirement and you are already heavily invested in WCF

- 1
- 1

- 30,562
- 14
- 91
- 126