How to debug WCF programs

54,970

Solution 1

You need to attach the debugger to the process that your wcf service is running in.

If in iis you need to attach to the corresponding w3p.exe process.

If in a stand alone app or windows service, attach to the name of your exe.

In Visual Studio, on the debugger menu, there is an "attach to process". Open the relevant code, set a breakpoint, and call the service causing that code path to execute.

Outside of debugging, using .net tracing with switchable levels is a good way to get insight to what's going on. I typically setup sys internals debugview to color highlight errors and warnings and constantly have it running while running code or tests. Colored lines out of my peripheral vision while working finds issues.

Solution 2

There's a much easier way. Just start multiple processes in Visual Studio. Right click the Solution. Click Properties. Select Startup Project. Click Multiple Startup Projects. Set the WCF and the Client projects to Action=Start. Now you will step through the WCF as well as the client.

Solution 3

If you are looking to trace the WCF activity to see if the traffic is generated in the correct order, then I would recommend one of the following approaches:

1) Use fiddler to watch the WCF traffic.

2) Use the WCF trace listener to monitor the actual WCF calls. This is extremely helpful when trying to determine the causes of serialization failure. You can enable this by adding the following block to your web.config's configuration block:

<system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
              switchValue="Information, ActivityTracing"
              propagateActivity="true">
        <listeners>
          <add name="traceListener"
              type="System.Diagnostics.XmlWriterTraceListener"
              initializeData="c:\log\WebTrace.svclog"  />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>

Double-clicking on the generated file will open up the WCF service log viewer, which will analyze the file for you and allow you to drill into specific calls and see the actual exceptions that occur.

Solution 4

To debug a WCF service in visual studio 2010, go to Debug -> Attach to Process. Check "Show processes from all users", and choose w3p.exe if you are using IIS, or the name of the application if not. Put in a breakpoint, make the call, and then you can then start debugging.

If it's a web application (I'd recommend this) you can right click on the project, go to the Web tab, and under Start Action choose "Don't open a page, wait for a request from an external application". Save and close this setting, then just hit F5 to start debugging.

Solution 5

Attach to the service itself, not the code calling it.

Share:
54,970
Val
Author by

Val

Updated on May 14, 2020

Comments

  • Val
    Val about 4 years

    My code uses lots of WCF calls and when I try to debug it, it doesnt go to the service code itself. Is there a way to debug WCF code somehow?

  • LoBo
    LoBo about 11 years
    This was extremely helpful in my case. Thanks a lot!
  • Sigh-AniDe
    Sigh-AniDe about 7 years
    Web application debugging saved me huge amount of time!
  • Alexander Derck
    Alexander Derck almost 6 years
    Setting the start action to "Don't open a page, wait for a request from an external application" made it possible for me to attach a debugger to my iis process and hit the breakpoints... I'm baffled why it suddenly works, this has nothing to do with eachother afaik
  • Boiethios
    Boiethios over 4 years
    In my computer, that's w3wp.exe