How to Debug a WCF Service

G_arora
Posted by in WCF category on for Intermediate level | Views : 53297 red flag

There are three different ways through one can Debug WCF service.
In the article all 3-ways are described.
Introduction
Following are the 3-different ways to start debugging a WCF service:
  • When you are debugging a client process that calls a service. Here debugger steps into the service also service does not have to be in the same solution as your client application.
  • When you are debugging a client process that requests to a service and here service must be a part of your solution.
  • When you are using Attach to Process to attach to currently running service. Here debugging begins inside the service.

  • Debugging with Visual Studio 2008
    Using Visual Studio 2008, one can step into a WCF service. One can hit break points inside the WCF service, if the WCF service is in the same solution as the client.

    For stepping to work, you must have debugging enabled in the app.config or Web.config file we will discuss later on the limitation and how to enable debugging.
     

    To step into a WCF Service
  • First you need to create a Visual Studio solution that contains both the WCF client and WCF service projects.
  • Now from Solution Explorer, right-click the WCF Client project and then click Set as Startup Project.
  • Then enable debugging in the app.config or web.config file
  • Mark a breakpoint at the location in the client project where you want to start stepping (just before the WCF service call).
  • Now, Run to the breakpoint, then begin stepping. The debugger will step into the service automatically.

  • Limitations of Debugging a WCF Service
    1.Stepping Into a Service 
  • The following conditions should be met when you start To step into a service from a client applications:
  • Service must be called bys using a synchronous client object.
  • One-way contract operation is not allowed.
  • Debugging must be enabled

  • 2.Stepping Out of a Service
    The limitations are the same as described the above, in addition, here the debugger must be attached to the client. 

    This is because while you are debugging a client and step into a service, the debugger remains attached to the service whether you started the client by using Start Debugging or attached to the client by using Attach to Process.
    But while you are debugging by attaching to the service, the debugger is not yet attached to the client. In that case, you must first use Attach to Process to attach to the client manually.

    Debugging must be enabled

    3.Automatic Attach to a Service
  • Automatically attaching to a service has the following limitations:

  • In this case the service must be part of the solution you are debugging.
  • The service must be hosted. Which means it may be part of a Web Site Project (File System and HTTP), Web Application Project (File System and HTTP), or WCF Service Library project. WCF Service Library projects can be either Service Libraries or Workflow Service Libraries.
  • WCF client must be invoked the service.
  • Debugging must be enabled

  • Enabling Debugging a WCF Service

    Use following piece of lines in web.config or asp.config file(s):

    <system.web&rt;
      <compilation debug="true" />
    <system.web>
    Note: In case when you use Attach to Process on a service, the debug code is automatically added to the .config file
    How to Debug a Self-Hosted WCF Service?
  • In simple words WCF service which does not run inside IIS, WCF service Host or ASP.NET Development Server is know as Self-Hosted Service.

    To debug such services we need to configure Visual Studio to launch both client and server when you choose Start Debugging on the Debug menu.

    How to start both client and host from Visual Studio?

  • Need to create a Visual Studio solution pertaining both the client and server projects.
  • Now configure the solution to start both client and server :
    a. From Solution Explorer, right-click the solution name.
    b. Click Set Startup Projects.
    c. In Solution from Properties dialog box, select Multiple Startup Projects.
    d. Click Action and choose Start from the Multiple Startup Projects grid, on the line that corresponds to the server project also on the line that corresponds to the client project, click Action and choose Start.
    e. Finally click OK

  • Page copy protected against web site content infringement by Copyscape

    About the Author

    G_arora
    Full Name: Gaurav Kumar Arora
    Member Level: Starter
    Member Status: Member,Microsoft_MVP
    Member Since: 9/7/2009 2:53:47 AM
    Country: India
    Gaurav Kumar Arora
    http://www.gaurav-arora.com
    MCA, M.Phill(Comp.Sc.),MCP, MCTS, Certified Scrum Master. Life time member of CSI (Computer Society of India)

    Login to vote for this post.

    Comments or Responses

    Login to post response

    Comment using Facebook(Author doesn't get notification)