Tuesday, June 21, 2011

How to Host a WCF Class Library Project ?

When we create a WCF class library project, it is not ready to be hosted on IIS directly.
It needs web.config and .svc file.
Notice that the WCF class library project has a app.config file, but no web.config or .svc file.

There are two steps :
  Create web.cofig
  Create .svc file

1. To create web.config, add a blank text file to the class library project. Rename it to web.config.
Add the following to it  and save it:
 <?xml version="1.0" encoding="UTF-8"?>

Then copy the entire <system.serviceModel>  tag from the existing app.config file of the class library peoject.

2. Next add a blank file to the wcf class library project and name it as a .svc file, say xyz.svc.
In the svc file add two statements :
  <%@ ServiceHost Service="BusinessServiceLayer.LoginService" %>
  <%@ Assembly Name="BusinessServiceLayer" %>

 Note that the service value in the ServiceHost directive  is the name appearing in the <service> tag in app.config/web.cofig

Assembly name is the name of the dll. By default this dll is created in bin\Debug of bin\Release folder of the
 class library project. This must be copied out of the \Debug or \Release folder into bin folder for it to
 be accessible to the IIS.

The last step is to set .svc as the default page in the IIS web site settings.

No comments:

Post a Comment