How to debug WCF programs

后端 未结 7 1168
执笔经年
执笔经年 2020-12-31 01:21

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?

相关标签:
7条回答
  • 2020-12-31 01:34

    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
    0 讨论(0)
  • 2020-12-31 01:44

    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.

    0 讨论(0)
  • 2020-12-31 01:45

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

    0 讨论(0)
  • 2020-12-31 01:50

    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.

    0 讨论(0)
  • 2020-12-31 01:52

    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.

    0 讨论(0)
  • 2020-12-31 01:53

    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.

    0 讨论(0)
提交回复
热议问题