Tuesday, April 19, 2011

How to: Host a WCF Service in IIS 7.5 (Visual Studio 2010)

Here is a quick list of tasks that we'll need to do for hosting a WCF Service in IIS:
  • Open VS 2010 and create a new project of type WCF Service Library under Visual C#=>WCF.
  • Give a name to your project say: MyFirstWcfServiceLibrary and click OK. This will create a sample working WCF service which you can run by simply pressing F5. Solution that is generated automatically is shown in figure below. We are not done as yet, are we? We have to host this service in IIS.


  • Now, build the solution in Release mode. This will create the MyFirstWcfServiceLibrary.dll assembly.
  • Create a new folder where you want to host your WCF service say: we want to host our service in folder named D:\MyFirstWcfServiceLibrary. Create another folder named bin copy the MyFirstWcfServiceLibrary.dll into D:\MyFirstWcfServiceLibrary\bin
  • Create two files in folder: D:\MyFirstWcfServiceLibrary
  • Create a .svc file named: IService1.svc (IService is the name of service created by VS) and copy the following text into it:

<% @ServiceHost Service="MyFirstWcfServiceLibrary.Service1" %>

· Create a new file named: web.config and type in the following contents into it:

     <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service behaviorConfiguration="MyFirstWcfServiceLibrary.Service1Behavior" name="MyFirstWcfServiceLibrary.Service1">
<endpoint address="" binding="wsHttpBinding" contract="MyFirstWcfServiceLibrary.IService1" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyFirstWcfServiceLibrary.Service1Behavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

  • Now, ensure that ASP.NET and WCF are correctly installed and registered.
  • Register WCF by executing the following utility in console:
CD C:\WINDOWS\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\

ServiceModelReg.exe -i -x
  • Register ASP.Net by executing the following command in console: aspnet_regiis -i
  • Now open Run and type inetmgr. This will open the IIS.
  • Create a new Application Pool and set the setting as shown in the following figure:

  • Create a new Application and set the setting as shown in the following figure (D:\MyFirstWcfServiceLibrary is where your virtual directory will be created. Remember we have MyFirstWcfServiceLibrary.dll in folder: D:\MyFirstWcfServiceLibrary\bin and web.config and service1.svc files in folder: D:\MyFirstWcfServiceLibrary):

  • WCF Service has been deployed successfully!!! Now, you are ready to access your service from here:

http://localhost/MyFirstWCFService/IService1.svc

You’ll see the following screen:

Running the svcutil.exe generates two files: A code file (Service1.cs) and a configuration file (output.config)

Please feel free to post any comments.