简体   繁体   中英

How to debug WCF programs

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?

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.

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.

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.

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.

附加到服务本身,而不是调用它的代码。

In my case, I set the WCF Site as StartUp Project in Visual Studio and directly run the WCF in the Debug Mode,

the WCF item in the Visual Studio Solution Explorer is like:

在此输入图像描述

after WCF start, there'll be a new web page show in the browser, and its url will like http://xxxx:xxport/Service.svc , copy this uri and use it in other program who call this WCF,

then set the break point at the method that program call, the breakpoint will be successfully entered when the program executes.

When running an application that accesses WCF services there are often two processes involved

  • The client process which is accessing the WCF service
  • The server process which is hosting the WCF service

It sounds like you are debugging the client process. In order to step through the actual WCF service code you need to attach the Visual Studio Debugger to the process which is hosting the service and set a break point in the code.

Note: Visual Studio can attach to multiple processes simultaneously so you can debug both your client and server code in the same session. Use

  • Tools -> Attach to Process

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM