In my last post i wrote about a new Visual Studio Extension Template which i had developed for jump starting WCF REST Service based development targeting Visual Web Developer 2010 Express edition. I did receive a 4 star rating from 1 reviewer and he said “Great Work”. That really pumped me up and then wanted to do some more. So here i am with another release of a new extension template. Read on to see what it is.
The extension template i wrote for VWD gives you a jump start in developing a WCF REST Service which is hosted by a Web Server i.e. using IIS of course. What if you wanted to try out a self hosting WCF REST service. That necessity lead me to create one more Visual Studio extension template. I am calling this template as “VC# WCF REST Service Template”. At this moment as i write this blog post, the extension is live on Visual Studio Gallery site. Here is the link to that : http://visualstudiogallery.msdn.microsoft.com/3efc4aa9-4683-48a1-b167-5bfe757fc575. As said previously i wanted to support the community which uses the Express edition so this extension is targeted for Visual C# 2010 Express edition only.
So lets see how to use this template step by step:
Getting the template:
Fire up the Visual C# 2010 Express. Go to extension manager and search for “VC# WCF”. You will see the search results show the extension. Click on Download and the extension will be installed.
After installation, you can click on Installed Extension on the left hand panel and you should see this extension listed there. See the screen shot below:
Using the Template:
Once you have finished the above step its now time to use the template. Select File > New Project. In the templates section you will see the new project template option. See the screen shot below:
Give the new project a name and location and click Ok. At this moment you have a new Self Hosted WCF Rest Service project.
Description of the Solution:
Now lets see the solution that just got created because of the above step. Here is a screen shot of the solution:
The solution is broken down into 4 Parts:
WCF REST Service Contract
This project contains the contracts for the WCF REST Service we will be building. Its always a good practice to bundle your contracts into its own assemblies.
WCF REST Service
This project is the actual WCF REST Service implementation. This has a reference to WCF REST Service Contract.
WCF REST Service Host
This project is the console host which will host our WCF REST Service. This is a simple console project which reference WCF REST Service and WCF REST Service Contract.
WCF REST Service Client
This is a test client for our WCF REST Service. This client has a reference to WCF REST Service Contract. Using ChannelFactory this establishes communication to our WCF REST Service and executes the operation.
Couple of gotchas:
The service is hosted on port 9117. I have picked some random port number. If you want to change the port number of your choice then look in WCF REST Service Host and WCF REST Service Client.
Since we are running on a random port, you will face namespace reservation problem. So before running the application please do the following:
Open a command prompt as “Administrator”.
Run the following command. Note this is for WIN 7.
netsh http add urlacl url=http://+:9117/ user=<your domain\your user name>
For more information of how to do this on other OS check this article : http://msdn.microsoft.com/en-us/library/ms733768.aspx
If everything goes well i.e. build succeeded and you have done the namespace reservation – then the Server console should look like the following:
Then if you start the client, you will see the following result:
One thing to note here is – the client uses the Service Contract and ChannelFactory to communicate to the server. If you look at the service contract itself all the operation have UriTemplate defined. Meaning you can access them in a RESTful way. So once the server is up, open a browser for e.g. Internet Explorer and hit the URL http://localhost:9117. This address has been mapped to GetCollection method on the contract using the UriTemplate and this method can server HTTP GET method.
In this template – the client communicating with the server using Contract & Channel factory and also ability to access the same endpoints using the Browser showcases 2 facets of the WCF REST Support i.e. if you have your methods designed RESTfully without any specific HTTP parameters – it can support both the non browser consumption meaning a WCF client or a non WCF client PLUS any browser based consumption too.
So we learned something new today. I am super excited about this subject. Will continue to explore more on this grounds. So stay tuned.
Till next time, Happy Coding. Code with Passion. Decode with Patience.