Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wcf json web service

Tags:

json

c#

.net

wcf

What is the best way to create a JSON web service? We have another team that is using Java and they insist to having all communication done using JSON. I would prefer to use WCF rather than any 3rd party framework.

I found this blog: http://www.west-wind.com/weblog/posts/164419.aspx, and it suggests that the Microsoft implementation is flawed with M$ specific crap.

like image 383
Grzenio Avatar asked May 05 '09 10:05

Grzenio


People also ask

Can WCF use JSON?

WCF supports serializing data in JSON format.

Can we return JSON from WCF service?

WCF has option to send the response in JSON object. This can be configured with WebGet or WebInvoke attribute. In this sample we can create the sample RESTful service to expose the method to read/add/update/delete the employee information.

Is WCF RESTful or SOAP?

Normally, a WCF service will use SOAP, but if you build a REST service, clients will be accessing your service with a different architectural style (calls, serialization like JSON, etc.). Exposing a WCF service with both SOAP and REST endpoints, requires just a few updates to the codebase and configuration.


1 Answers

If you use WCF and the 3.5 Framework, it couldn't be easier. When you mark your OperationContracts with the WebGet attribute, just set the ResponseFormat parameter to WebMessageFormat.Json. When the service is accessed RESTfully, it will return the data using the DataContractJsonSerializer.

It's really helpful to mark the POCOs that you want to JSON serialize as [DataContract] and to mark each serializable member as [DataMember]. Otherwise, you end up with funky JSON, as Rick pointed out in his blog post.

like image 103
W. Kevin Hazzard Avatar answered Oct 25 '22 13:10

W. Kevin Hazzard