Software Architecture – RESTful WCF Services for .Net Framework 4.0/Visual Studio 2010

According to Wiki, Restful Webs services are described as:

A RESTful web service (also called a RESTful web API) is a simple web service implemented using HTTP and the principles of REST. It is a collection of resources, with three defined aspects:
•the base URI for the web service, such as http://example.com/resources/
•the Internet media type of the data supported by the web service. This is often JSON, XML or YAML but can be any other valid Internet media type.
•the set of operations supported by the web service using HTTP methods (e.g., POST, GET, PUT or DELETE).

For programming Restful services in .Net Framework, till visual studio 2008 sp1, there was a separate kit for named “WCF REST Starter Kit Preview 2” at:
http://aspnet.codeplex.com/releases/view/24644

But it was not upgraded for Visual studio 2010. So for Visual studio 2010/.Net 4.0, search WCF+REST related items in Visual studio gallery.

http://visualstudiogallery.msdn.microsoft.com/site/search?f%5B1%5D.Type=SearchText&f%5B1%5D.Value=WCF%20REST%20Service&f%5B0%5D.Value=templates&f%5B0%5D.Type=RootCategory

One of the best template by WCF Team in Microsoft is as below:

http://visualstudiogallery.msdn.microsoft.com/fbc7e5c1-a0d2-41bd-9d7b-e54c845394cd

Download file named “WcfRestService40(CS).vsix”, and double click to install the template.

Now a new project type named “WCF REST Service application” is added.

I created a new project based on new project type named “WcfRestServiceApp”.

Added a console project to solution for running service named “ServiceServerApp”, with following code

WcfRestServiceApp.Service1 serv = new WcfRestServiceApp.Service1();
            WebServiceHost host = new WebServiceHost(serv, new Uri("http://localhost/WCFRESTService/"));
            host.Open();
            Console.WriteLine("Service is running....Press enter to stop the service...");
            Console.Read();
            host.Close();

When you try to run service, you will get following error:

In order to use one of the ServiceHost constructors that takes a service instance, the InstanceContextMode of the service must be set to InstanceContextMode.Single.  This can be configured via the ServiceBehaviorAttribute.  Otherwise, please consider using the ServiceHost constructors that take a Type argument.

As error explains, I have to change InstanceContextMode from InstanceContextMode.PerCall
to InstanceContextMode.Single

After that I got following error:

HTTP could not register URL http://+:80/WCFRESTService/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).

So solution is to run following command incase of windows 7.

netsh http add urlacl url=http://+:80/WCFRESTService/  user=juliet\shah

For theory details see the link above.

So I am ready with service running .

While analyzing code of template, two things are important to note.
•Template uses the WebGetAttribute and WebInvokeAttribute to associate service operations with both GET and other HTTP verbs like PUT, POST, and DELETE.
•Template uses the UriTemplate and UriTemplateTable classes to provide URI processing capabilities.
For details about theory visit following
http://msdn.microsoft.com/en-us/library/bb412172.aspx

That all from my side for RestFull web Services in .Net framework 4.0/Visual studio 2010

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: