Thursday, March 15, 2012

Visual Studio hosting WCF Services using WCF Service Host

In this post we are going to be creating a simple WCF Service library project. We are going to be hosting this using Visual Studio Service host. We are going to see what various options are available to launch a WCF Service using Visual Studio including how we can test it using WCF Service Client. We are also going to be using Process Explorer to see how these supporting tools are used by Visual Studio.

When we create a WCF class library project, then Visual Studio creates a default WCF service for us with default template. It creates the following in the project.
  1. Adds the relevant assembly reference to the project. This includes System.Runtime.Serialization.dll and System.ServiceModel.dll.
  2. Adds a WCF Service containing a simple method. The class implements a contract added separately. The method (defined as operation contract)
  3. Adds a interface decorated with [ServiceContract] attribute. This also has a method decorated with [operationContract] attribute. This is the same interface implemented by the above class.
  4. Adds a class decorated with [DataContract] attribute. This type is used as a parameter in the operation contract method of above service contract.
  5. Adds app.config to the project. The configuration has the details of the exposed WCF service.

  6. See the properties of a WCF Service library project. There is an additional WCF options tab added.



    There is only one configurable option for the WCF service. When checked, Visual Studio automatically launches WCF Service Host utility hosting the service even when there is another start up project in the solution. This is a Visual Studio supporting tool and gets installed with Visual Studio installation in the following path in your Windows 7 computer.

    C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\

    This is available as follows:


    Let's download Process Explorer to see the details about WCF Service Host process from the following technet link:

    http://technet.microsoft.com/en-us/sysinternals/bb896653

    When launched, WCF Service host is available in system tray as follows:


    The tray icon can be used to launch the tool's interface. The interface has the details about hosted service.


    • If the WCF class library project is the start-up project then the service host is launched with relevant start-up arguments to use WCF Test Client as the client tool for the hosted service. The service host takes care of launching the client tool. We can use Process explorer to see the start-up argument of service host process.

      It is basically launched with the following start-up arguments.
      /service:C:\Users\shujaat\documents\visual studio 2010\Projects\InstituteApp\Institute.Data\bin\Debug\Institute.Data.dll
      /config:C:\Users\shujaat\documents\visual studio 2010\Projects\InstituteApp\Institute.Data\bin\Debug\Institute.Data.dll.config
      /event:dc8e4802-046c-4134-97b3-0d940ab3ee82 
      /client:"WcfTestClient.exe
      
      Specifying the switch for client launches the WCF Test Client after the service is hosted in WCF Service Host tool. The WCF Test Client is launched with the start-up arguments specifying the service details.


    • When the service project is not selected as a start-up project then Visual Studio just launches the WCF Service host with the details of the service being hosted. The start-up arguments to launch WCF Service host are as follows:
      /service:C:\Users\shujaat\documents\visual studio 2010\Projects\InstituteApp\Institute.Data\bin\Debug\Institute.Data.dll
      /config:C:\Users\shujaat\documents\visual studio 2010\Projects\InstituteApp\Institute.Data\bin\Debug\Institute.Data.dll.config
      /event:56023077-b3b8-486d-8e62-ffc2ccf7cad7 
      We can see that all the parameters are same except the client is not expected to be launched so the client switch hasn't been specified.

Basically the additional start-up argument is the default start-up argument of WCF Service library project. It can be seen in the Debug tab of project properties. If we don't want the WCFTestClient to be launched when WCF Service runs as a start-up project then just remove this extra switch.


Download Code:

1 comment:

Anonymous said...

If it is a web site project(VS2010) right click project. Select Properties Window(F4). You should see a property grid of the properties window. There choose False for Always start when debugging. This solved the problem for me.