WCF Web API
There is a new API for doing REST with WCF: WCF Web API http://wcf.codeplex.com
Here is a quick getting started blog:
1] Create a new empty ASP.NET application
2] Add WebApi.All from NuGet
3] Add new class
using System.ServiceModel;
using System.ServiceModel.Web;
namespace WcfWebApi
{
public class Contact
{
public int Id { get; set; }
public string Name { get; set; }
}
[ServiceContract]
public class ContactService
{
[WebGet(UriTemplate="")]
public Contact Get()
{
return new Contact() { Name = "Bert", Id = 999 };
}
}
}
4] Add global.asax file and add a route ‘contact’
using System;
using System.Web.Routing;
using Microsoft.ApplicationServer.Http.Activation;
namespace WcfWebApi
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapServiceRoute<ContactService>("contact");
}
}
}
To get this code running I needed to add a reference to Microsoft.ApplicationServer.HttpEnhancements.dll and namespace ‘Microsoft.ApplicationServer.Http.Activation’
5] Run in browser ‘/contact’ to get XML back
6] Run in Fiddler with ‘accept:application/json’ in the header to get JSON back
Conclusion: with just a bit of code we can quickly create REST services that return XML/JSON format. No hassle with cryptic XML config files anymore ![]()
-
Archives
- November 2011 (1)
- October 2011 (1)
- September 2011 (1)
- May 2011 (3)
- March 2008 (4)
-
Categories
-
RSS
Entries RSS
Comments RSS