Debug XML Web services in VS.NET

Visual Studio .NET makes developing Web services applications relatively easy, but debugging them is a different story. Learn how to debug your Web service apps using VS.NET.

XML Web services are touted by Microsoft as the next big thing. But until recently, developers were hampered in their efforts to effectively develop them, placing the future of the new protocol in some doubt. Fortunately, Visual Studio .NET has made the development and consumption of Web services almost trivial, while making it possible—if not always simple—to debug them.

The concept behind debugging Web services
Web service development under VS.NET has a number of possible scenarios:
  • Development of the service itself, using ASP.NET and Visual Studio .NET
  • Utilization of an outside service as part of your ASP.NET or Windows Forms application
  • Development of both the Web service and the application

If you're developing the Web service yourself, you can debug it using the default page you see when displaying the ASMX file with a browser. If you're developing an application using an external service, your options are limited. Since it's unlikely that you can get debugging information from the remote service, most problems can’t be debugged out.

If you're writing both the service and the application, your debugging options widen—and grow more complex. You need to test and possibly debug the service itself, as well as the interface between the application and the Web service. This is best served by stepping into the service from the application. That's the example we'll consider in this article, because it covers the widest range of possible issues.

We're going to build a simple Web service and a Windows Forms application to consume it. Before we begin, let's address a few housecleaning issues. If you're running this exercise on a remote server, as many XML Web services will be, you'll need to load Remote Debugging on that server. The user will need to have Administrator rights on the server. Since we'll be consuming the Web service in a Windows Forms application, you should attach to the ASP.NET Worker Process on the server. This requires installing aspnet_wp.exe on Windows 2000 or w3wp.exe on Windows 2003. Now we can begin the debugging test.

To start off, generate a simple service called Debug Services with a class TestDebug and a single method, GetTestResult:
public string GetTestResult()
   string TestResult = "";
   TestResult = System.AppDomain.CurrentDomain.ToString();
   return TestResult;

If you set a breakpoint at the string declaration and press [F5] (after setting the new file as the start page), Visual Studio will load the sample page for the service in a browser. After you select the GetTestResult service and click the Invoke button (there are no parameters), you'll be sent to debugging mode, just as with ASP.NET Web Forms.

Next, add a Windows Forms project to your solution in Visual Studio .NET. Put a label (resultTest) on the form for your return values, and a button (CallService) to click to call the service. Now, you need a little simple code in the CallService event handler:
private void CallService_Click(object sender, System.EventArgs e)
   TestDebug remoteDebug = new TestDebug();
   resultTest.Text = remoteDebug.GetTestResult();

Leave the breakpoint on the string declaration of the XML Web service. Then, set the startup project as the new Windows Forms project by right-clicking on the project name in Solution Explorer. Go into debug mode on the Windows Forms application by pressing [F5] and then click back on Visual Studio .NET.

Choose Processes from the Debug menu and select the machine running the Web service from the Name drop-down list, as shown in Figure A. Then, click on the ASP.NET Worker Process (aspnet_wp.exe, in this case) from the Available Processes list box.

Figure A

As you can see, I'm running the Web service on a machine called Gryphon, while my Windows Forms project is on Pegasus. After selecting Worker Process and clicking Attach, make sure the Common Language Runtime is selected in the Attach dialog box. Then, click OK and Close.

Finally, return to the running debug version of the Windows Forms application and click the CallService button we created. The Web service code will appear at the breakpoint, just as if you were running from the sample page. Note that if the Windows Forms project and the XML Web service project are not in the same solution, the debugger will ask you for the location of the source file.

Editor's Picks