Wednesday, June 20, 2012

Hosting WCF Service Library in IIS

A service file (.svc) and web.config files are required for the WCF service to be hosted in IIS. Since WCF Service Library do not have these files by default we may not be able to host it in IIS directly. WCF Service Library project is basically to be able to host the service in Console, Windows Service, Windows application or IIS. So, we need to follow these steps to host the WCF Service Library in IIS:

1. Create a virtual directory in the IIS for the service.
2. Create two files in the virtual directory folder. One is the web.config and the other one somename.svc.
3. Now copy the content of <system.ServiceModel> in the app.config file of the WCF Service Library into the web.config.
4. Add the following content in the .svc file:
<% @ServiceHost Service="Namespace.MyServiceClass" language="C#" debug="false" %>
<%@ Assembly Name="Assembly Name" %>

From our previous examples in Part 1 and Part 2, it should be as follows:
<%@ ServiceHost Service="HelloWorld.HelloWorld"%>
<%@ Assembly Name="HelloWorld" %>
5. Create a bin folder in the virtual directory and place the HelloWorld.dll generated in the bin\debug folder of the WCF Service Library project.
6. Make sure that the IIS has got sufficient permissions on the folders and files created in the virtual directory.

In case if your config file has localhost anywhere, change it to the machine name. Otherwise, when you generate proxy using visual studio, your WSDL will have the "localhost" instead of the machine name and if you are trying to access it from another computer it will not work.

Hope this helps... Happy coding guys!!!

No comments:

Post a Comment