Monday, April 14, 2008

Creating a quick sample IIS hosted WCF Service

Today I came across an MSDN article that describes how to create a sample IIS hosted WCF Service. The steps outline were manually and unnecessary if you have access to Visual Studio 2008 so here is a quick guide on creating your first sample IIS hosted service using a VS 2008 project template.

Creating the Sample

First open up Visual Studio 2008 and go into Create New Project.

image

Navigate to the Web portion of the Project Types tree in the language of your choice (C# in my example above) and select WCF Service Application from the Templates side.

image

Create your sample service and you will get a project that should look like the screenshot below with a Contract interface (IService1.cs) and Service1.svc file to represent the actual service endpoint.

image

Congratulations you now have a sample (or starting point) WCF Service that is hosted by IIS.

Moving to the real IIS

As a final note I want to remind everyone that by following these steps you now have a WCF Service that is hosted by the Visual Studio Development Server (by default).

I recommend switching to IIS Web Server in the project properties tab if you have it available on your machine as that will give you a more realistic hosting experience (which the Development Server is not)

image

8 comments:

Feng said...

Hi Dmitry ,

Thanks for the nice intro to WCF directly via VS 2008.

I have a question regarding WCF. I was wondering whether you would like to give the help. What is a easy way to host a WCF on a home PC which is supposed to be reachable via internet worldwide?

Thanks in advance. I will come to check your blog everyday.:)

Steven,Wang

Dmitry said...

Hi Feng,

Thank you for stopping by my blog, I am always glad to help if I can.
To answer your question it really "depends". Here are some (and trust me there is more) challenges you will face:

1. You will need to build a working WCF service and install it on IIS of your server machine, but this is a given :)

2. You will need to make the server computer hosting the service routable to. So for example lets say you have a normal home setup with multiple machines behind some wireless router then youll need to configure the correct ports in the router to point to the exact server with your WCF service

3. If on the server you don’t have the right port rules and services turned on, then of course nothing will work once again

4. If you don’t have a static IP (which most people don’t) you will need to get a service that can monitor your dynamic IP and update a DNS entry that others can reference. One such service is www.dyndns.com/ but look at other options by using Live search and keywords “dynamic DNS”

Another option you have is to get some sort of hosting service, maybe even with a virtual dedicated server for the most control. This would allow you to probably get a static IP for your hosted server and let the provider worry about routing and security, while youd only have to configure the windows server itself for WCF/Ports. I use GoDaddy for my virtual server but please feel free to find the best deal you can through your own research.

Best of luck!

BrianB said...

hey. thanks for posting this and the link to it from the MSDN article.

Do you have a link that describes the files generated by the 2008 WCF project template?

I'm a bit confused because it looks like, from the default template files, that the Service1.svc.vb and IService1.vb are very similar and somewhat redundant.

Jeremy - Developer said...

That's because IService1.vb is an interface for Service1.svc.vb.

Search google and read up on Interfaces.

Rod said...

Hi Dmitry,

Thank you for posting this article. I need to ask a clarifying question, though. This blog entry suggests that all I have to do, in order to host a WCF service in IIS is to create the service using VS 2008 project template of WCF Service Application under the Web portions of the Project Types. What confuses me is I've been watching Michele Leroux Bustamante's webcasts on MSDN from I think October 2007 (perhaps earlier). Anyway, in Michele's webcast on hosting, when she discussed hosting a WCF service in IIS it seemed to me that she had you write the service in one project, and then the host application (which actually instantiated the service) in another project. Your demo here seems to suggest that one only needs to use one project?!

I am very confused! Which is it? (I am using VS 2008 Pro.)

Dmitry said...

Michele Leroux Bustamante is my hero in this area, so whatever she tells you to do you should do :).

In terms of your confusion, perhaps what she said and what I am demoing here are related in terminology but try to represent two different things. I am not sure what recording you are talking about exactly either and dont have time to view it but i can tell you what this sample is trying to accomplish:

This post merely shows you how to create a quick WCF Service hosted in IIS using the Visual Studio 2008 Project Template. It works, try it :).

Now if you look at what this project template generates all it does is:

1. Creates an ASP.NET website
2. Adds a a contract (IService1.cs)
3. Adds a Service (Service1.svc)
4. Adds the required configuration information into the web.config

Thats it, this is enough to created a hosted service once you run this.

There is more then one way to do this, so work with whatever your scenario requires.

GMS0012 said...

Hi all, can you help me..
i created the template project and run it on local IIS.. everything is working..
then i publish it with the VS 2008 to my remote IIS and it is not working...
do i have to change something somewhere?? please help me...

thanks

Dmitry said...

There are many reasons why a deployed WCF service could not work. I suggest gathering as much details about your environment as possible and posting it at the following forum to seek help:

http://forums.microsoft.com/msdn/ShowForum.aspx?siteid=1&ForumID=118

MSDN Official forums are typically full of MVP's willing to help solve even complex problems.