Interview Questions Headline Animator

Thursday, September 18, 2008

Hosting a WCF Service on IIS

Look for this topic on the Internet. The first thing that you will see is the implementation of the service. I really wouldn't fathom why that is usually done. If I am looking for the subject Hosting a service on IIS, I would assume that the service has already been written and ready, and it is the hosting that I really need the help with. But still I will also follow the standard and go and and tell you the details of how to create a WCF Service.

  1. File -> New -> Website
  2. WCF Service

This will create Service1.svc and IService1.cs. The IService1.cs is the Service Contract, that you need to implement on your Service. Prepare the contract, prepare the Service and then build. Once it builds, you are done with the service.

Now when you are writing an application which requires a WCF Service, you don't really start off with a WCF Service. Just like you don't start writing a Windows Service, with a Windows Service, you don't start a WCF Service application with a WCF Service application. Confused...? What I am trying to say is, in case of a windows service, what I usually do is write the entire application in a Windows Application. Once I am done writing the application, I move the code to a Windows Service, simply because it is much easier to debug a windows application than a windows service. Similarly, I would write a normal multi-layered application, and then once I am done (at least up to a logical completion), I would Create a WCF Service and move the Service Layer (which was not a service layer until now) into the new Service. Of course with it goes all the other layers, which were supposed to be on the database side. Like the Data Access Layer or perhaps the Entities Layer, if you happen to use one.

So coming back to the point how do you really host your service? This article is not really about hosting the service. I would like to write more about the glitches and the obstacles that you have to face when you are hosting the service. In theory, the process of hosting the service is quite simple:

  1. Publish the Service
  2. Open Internet Information Services (IIS) Manager
  3. Create a new application and provide the physical path to the directory into which you published the service

That's it.

Now the glitches:

Server Error in Application “Default Web Site/CallService”

HTTP Error 404.3 - Not Found:

Incase of this error the possible reasons are:

  1. The current version of WCF has not been registered
  2. IIS Scriptmaps are not updated at the IIS Metabase Roots
  3. Existing Scriptmaps are of a lower version

To resolve this issue, you need to take the following steps:

1. Run a command prompt window as administrator. Start, cmd as normal then press CTRL+SHIFT+ENTER
2. Navigate to c:\windows\Microsoft.Net\Framework\v3.0\Windows Communication Foundation\
3. Run the command ‘ServiceModelReg -i’ - You should receive the results below.
4. Exit

Another error that you most commonly see is this:

"There is no compatible TransportManager found for URI 'http://<your URI>.svc This may be because that you have used an absolute address which points outside of the virtual application. Please use a relative address instead."

The error message might misguide you. There is a possibility that you might have specified an absolute address in the configuration file. But if you go there and check and find that the address is not absolute, then perhaps the error isn't there. For this what you should do is, check if all your IIS Components are installed or not. In my case I discovered that I didn't have 2 things installed. "IIS Metabase and IIS 6 Configuration Compatibility" under Web Management Tools -> IIS 6 Management Compatibility and "Windows Authentication" under World Wide Web Services -> Security.

Last but not the least. If you see any other issues, which you are not able to figure out just by reading the error message on the IE, where you are trying to browse to the service, publish the website once again, using an http:// URI from your Visual Studio project. If the publish fails, even though you might not see any errors on the error window, go to the output window and check. You will see the source of the problem, which is much more clear and easy to understand. This idea was given to my by my good friend Koti who sits next to me.