The Visual Studio .NET or Visual Studio 2005 debugger stops responding when it calls an XML Web Service in asynchronous mode (315624)



The information in this article applies to:

  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2002), Academic Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2003), Academic Edition
  • Microsoft Visual Studio 2005 Standard Edition
  • Microsoft Visual Studio 2005 Professional Edition

This article was previously published under Q315624

SYMPTOMS

When you run a single-threaded apartment console Web client application by using the Visual Studio .NET or Visual Studio 2005 debugger, the client application cannot complete an asynchronous call to an XML Web service. However, if the Web client is started without the debugger, the call works as expected.

CAUSE

This behavior occurs because the client application makes an asynchronous call to the XML Web service, and then waits for the XML Web service to fire an event. While the client is waiting, the main thread of the client application automatically enters a sleep mode.

If you are running the client application in debug mode, the Csm.dll debugger component file is called to handle the debugger event. However, because the main thread is in sleep mode, the event callback cannot be completed and the client application stops responding.

RESOLUTION

A console application that blocks the primary thread can receive the asynchronous response from the Web service during a debugging session. However, in this case, the application was launched in a single-threaded apartment.

Resolution 1

By default, .NET-connected console applications run in a multithreaded apartment (MTA). Therefore, you can resolve this problem by removing the STAThread attribute that appears before the Main function (see the sample code in step 10 of the "Steps to Reproduce the Behavior" section). This permits the application to retrieve the result from another thread in the MTA while the debugger is executing.

Resolution 2

A Microsoft Windows-based application should run its primary thread in a single-threaded apartment (the STAThread attribute). However, because a Windows application is event-driven, the blocking loop does not have to prevent shutdown. Therefore, you can also use an event-driven Windows client application to resolve this problem.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to reproduce the behavior

To reproduce the behavior, follow these steps:
  1. In Visual Studio .NET or in Visual Studio 2005, create a new Visual C# ASP.NET Web service project named MyWebService1.
  2. In Solution Explorer, right-click Service1.asmx, and then click View Code to edit the code.
  3. Remove the comments to turn on the HelloWorld sample method.
  4. Save and then build the project.
  5. Add a new Visual C# console application named MyClient to the solution that you created in step 1.
  6. In Solution Explorer, right-click MyClient, and then click Add Web Reference.
  7. Type the URL for the .asmx file of the MyWebService1 project that you created in step 1 (for example, http://localhost/MyWebService1/Service1.asmx), and then click Add Reference.
  8. In Solution Explorer, right-click MyClient, and then click Add Reference.
  9. In the Add Reference dialog box, click the .NET tab, and then click System.Windows.Forms.dll in the list. This reference permits the console application to display a message box. Click Select, and then click OK.

    Note In Visual Studio 2005, you do not have to click Select.
  10. Open Class1.cs in Visual Studio .NET or Program.cs in Visual Studio 2005, and then replace all of the code with the following sample code:
    using System;
    using System.Windows.Forms;
    using System.Threading;
    
    namespace myconsoleapp
    {
    	// Summary description for Class1.
    	class Class1
    	{
    		// The main entry point for the application.
    		// public static void Handler(IAsyncResult ar)
    		public static void Handler(IAsyncResult ar)
    		{
    		// Retrieve the proxy object reference that is passed
    		// to BeginSubmit().
    		localhost.Service1 proxy = (localhost.Service1)ar.AsyncState; 
    			
    		// Use the proxy to retrieve the result that is returned
    		// from the call. Pass in 'ar' so that the specific 
    		// call can be identified.
    		string ret = proxy.EndHelloWorld(ar);
    
    		// Show the XML receipt to the user.
    		MessageBox.Show(ret);
    		}
    
    		[STAThread]
    		static void Main(string[] args)
    		{
    			localhost.Service1 obj = new localhost.Service1();
    			obj.HelloWorld();
    			System.IAsyncResult ar;
    			ar = obj.BeginHelloWorld(new AsyncCallback(Handler),obj);
    			while(ar.IsCompleted != true)
    			{
    				System.Threading.Thread.Sleep(1000);
    			} 
    		}
    	}
    }
    					
  11. In Solution Explorer, right-click MyClient, and then click Set As Startup Project. Build the project in release mode, and then press CTRL+F5 to run the project. A command window appears, and then a message box appears that contains the following message:

    Hello World

  12. Build the project in debug mode, and then run the project.
    The application stops responding.

Modification Type:MajorLast Reviewed:3/3/2006
Keywords:kbvs2005applies kbvs2005swept kbvs2002sp1sweep kbBug kbDebug kbide kbprb KB315624 kbAudDeveloper