michielpost.nl

Creating a JSON REST Webservice with WCF in .Net 4

07-12-2010

First, create a normal WCF Service, with service contracts, datacontracts etc.

To make it JSON / REST, follow these steps:

Modify the Web.Config:
Application_Start:
RouteTable.Routes.Add(new ServiceRoute("test", new WebServiceHostFactory(), typeof(MyService)));
(Add System.ServiceModel.Activation.dll as reference)
 
This will make the webservice available at the following URL:
http://mysite/test
 
Add this on the service implementation class:
[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
 
This puts the webservice in ASP.Net compatibility mode.
 
Add this attribute above methods that should be available using GET:
[WebGet(ResponseFormat = WebMessageFormat.Json)]
 
There's lots of outdated info online (.Net 3.5 and older).
This is a nice tutorial for JSON, WCF and .Net 4
http://christopherdeweese.com/blog2/post/drop-the-soap-wcf-rest-and-pretty-uris-in-net-4

Comments


Gg3vGFomR 17-07-2014
"??? = ????????"?????????? INI ??????? ????? ????????:XML allows arirtraibly complex levels and nesting, and has standard mechanisms for encoding binary data. INI files are typically limited to two levels (sections and parameters) and do not handle binary data well. ???? nesting ?????????? ????????? ?????? ??? ?????????? ?? ????? ??????.?????????.????????='INI rulez'???? ?????? ??? ????????? xml ????????? ?? ? ?????????????? (? ?????? ???????) ????? ??????.


SOAwR6Apdk 22-07-2014
I can't wait for that video! I'm currently<a href="http://rqnvxce.com"> hnavig</a> to write a Web API that requires some kind of Identity and Access Control. Also tying it into our AD system here.


lHfX0Gtvvl9 25-07-2014
I've watched all your PluralSight coseurs. Great job, thanks.When are you going have a course on Identity and Access Control for Web APIs?- Mike http://wjcjpqnnk.com [url=http://mreefzgilud.com]mreefzgilud[/url] [link=http://uhxggtkzc.com]uhxggtkzc[/link]


6hQkRBhiLC 29-07-2014
I doubt that all Windows 7 users will convert to Windows 8. I know that I am reedimcnomng that casual users (like my wife and some neighbors) stick with Windows 7. The reason is the silly Start screen in Windows 8 gives them nothing of value and a lot of confusion.I have gone to Windows 8 and most developers will follow suit. I think Windows 8 is a better OS but just too confusing for the those who just use their machine for simple stuff like email, web browsing, and a few Office apps. http://upgakldkx.com [url=http://gvrjszoxwr.com]gvrjszoxwr[/url] [link=http://uglkmownbxv.com]uglkmownbxv[/link]


For the love of God, keep writing these arcleits. 12-11-2015
For the love of God, keep writing these arcleits.


New Comment

Name
E-mail (not published)
Comment
Enter the code shown: