Introduction

There are various examples of ReST available on the net like Web-Services for .NET/C#, but none of them provide a simple .NETish framework. For example, how easy is it to create a Web-Service by just deriving from a class and annotating methods as exposed for the web? This article provides a small framework for ReST based Web-Services primarily targeted for .NET versions 3.5 and older.

Using the code

Like any other article on the web about ReST and .NET, this implementation involves handling specific resources request through an IHttpHandler; in this case, it is PoC.Web.Services.ReSTServiceHandler. This IHttpHandler would handle the ReST calls (Get, Put, Post, and Delete) and dispatch them appropriately. In order to add the handler, the following lines must be added to the web.config:

In this example, we have a Calculator class which implements IReSTService and exposes three methods by annotating them with the ReSTMethod attribute. The ReSTMethod attribute requires an allowed verb on that method. That's it. If built, the CalculatorService can now be accessed by these URLs:

In this example, we are exposing an array of POCO, an XmlNode, and an XmlDocument. We also see a new attribute in this example, ReSTXmlResponse. This attribute makes the engine (IHttpHandler) return an XML representation of the object by serializing the object. If the return type of the method is typeof XmlNode, then the engine would return the OuterXml of the node and set the content-type to "xml". And yes, there is a special type converter for XmlDocument/XmlNode from string .

"...simple .NETish framework..."
if ur sample code spans more than couple of files, don't tag it simple. someone might just be starting to explore something new, and have to go through SIX different classes/interfaces, including an interface that implements another interface, and you tag it simple!! wasted 10 valuable minutes!!

running the solution in VStudio 2010 (with the built in cassini web server) works great and just as I have hoped.
However when I deploy the project into IIS7 (create Web-Site and point to folder where the default.aspx and the bin directory are located) it will not work.
More precisely the RestServiceHandler is never being called. I have added some debug prints and all I get is nothing.
So what do I need to do in order for IIS 7 to really start using the code?

By the way - I have modified the web.config accordingly. So it currently has two entries with "add verb path="RestService/*" and the other with "add verb path="RestService/*/*".

Again, everything works fine locally in VStudio only IIS7 does not want to jump into the code.

Instead of returning the number "C" in Sum (A+B) Function. I try to send

c= Session.SessionID. It gives the error

Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the <configuration>\<system.web>\<httpModules> section in the application configuration.

Parag, thanks for the code example. To run this with IIS7 the Application Pool needs to be changed to Classic .NET AppPool, however, there's a 404 when trying to deploy and run this from IIS rather than via the ASP.NET Development Server (Cassini). So the next thing to do then is to add Handler Mappings.

Hello All,
I did add the handlers, but when I type the URL in the webbrowser, it returns nothing.. I am using IIS7... But when I run in VS2010, I get the results back... Is there anything extra I need to do to work in IIS7