开发者

.Net & WCF - How to create REST instead of SOAP?

I created a couple of services, but they are all running as soap web services and return 开发者_如何学运维soap messages.

1- How can I make my wcf project to be restful instead of soap?

2- How can i make all services to return and expect json instead of xml?


You need to do at least two things:

  • decorate your operation contracts (the service methods) with a WebGet or WebInvoke attribtue and define a URI template

  • define a service endpoint that uses the webHttpBinding and a specific webHttp endpoint behavior

Other than that - you really don't need to do anything to get your REST service up and running.

Mind you: this is just "exposing" your current SOAP methods as REST - this is not the "proper" REST style of programming. To do that, you'd have to design your service from the beginning to be RESTful.

SOAP typically uses messages and methods - stuff like GetCustomer, GetBalance and so forth.

REST on the other hand thinks in resources - you have a Customer resource, and issuing an HTTP GET on that resource URI retrieves the customer, a PUT would insert a new customer, POST would update an existing customer, and DELETE (all HTTP verbs) will delete your customer. So in proper REST, you're not thinking in methods and procedures - you're thinking about how to expose resources and make those available to the consumer of your service.


Another example of a WCF REST/Json service and client can be found in my answer to the question "Client configuration to consume WCF JSON web service".


There is an excellent section in the MSDN docs devoted to a detailed analysis of how to "JSON-enable" your WCF services. I recommend it as a starting point.

But before you read that, I recommend skimming through the section on the WCF Web Http Programming Model, since it builds up some foundational concepts that would be useful in understanding the JSON-enablement / AJAX-enablement topics of WCF described later.

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜