Microsoft KB Archive/827163

= FIX: References to STA objects that are stored in session state may become corrupted if they are called from a Session_End event =

Article ID: 827163

Article Last Modified on 10/25/2005

-

APPLIES TO


 * Microsoft .NET Framework 1.0
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft ASP.NET 1.0

-



Notice


For a Microsoft .NET Framework 1.1 version of this article, see 827164.



SYMPTOMS
If you store your references to single-threaded apartment (STA) objects in session state, and you set the AspCompat attribute to true, the references may become corrupted or may become lost when you try to gain access to the objects from the Session_End event.

Note Typically, this problem may occur if you run the application under high loads or if you use a stress tool such as Application Center Test (ACT) when you run the application. When this problem occurs, you receive a &quot;0x8004e005 Error&quot; error message in the event log.



CAUSE
The problem occurs because the Session_End event does not have to run on the same thread that the session was created on. Because the STA object is no longer on the same thread that the session was created on, you must use marshaling to gain access to the object. This behavior causes slow performance.

You can store STA objects in session state. However, Microsoft does not recommend that you do so. For more information, see the &quot;References&quot; section of this article.



RESOLUTION
This hotfix adds a new configuration setting to the Machine.config file. After you install the hotfix, the following application configuration is available. , By default, apartmentThreading is set to false. If you set apartmentThreading to true, the following behavior occurs:
 * All Session_Start events run on STA threads that correspond to the session ID, regardless of the target page or the handler.
 * All Session_End events run on STA threads that correspond to the session ID.
 * AspCompat becomes set to true for all pages, except for the pages that have AspCompat set explicitly to false.

Note You can still run the Session_Start event on the STA thread after you apply this hotfix if apartmentThreading is set to false and AspCompat is set to true for the target page.

This hotfix works for STA objects that are stored directly in session state. This hotfix also works for an STA object that is nested one level deep such as a .NET object that is stored in session state and that has a reference to an STA object. However, this hotfix does not work for STA objects that are stored more than one level deep.

A supported hotfix is now available from Microsoft, but it is only intended to correct the problem that is described in this article. Only apply it to systems that are experiencing this specific problem. This hotfix may receive additional testing. Therefore, if you are not severely affected by this problem, we recommend that you wait for the next Microsoft .NET Framework 1.0 service pack that contains this hotfix.

To resolve this problem immediately, contact Microsoft Product Support Services to obtain the hotfix. For a complete list of Microsoft Product Support Services telephone numbers and information about support costs, visit the following Microsoft Web site:

http://support.microsoft.com/contactus/?ws=support

Note In special cases, charges that are ordinarily incurred for support calls may be canceled if a Microsoft Support Professional determines that a specific update will resolve your problem. The usual support costs will apply to additional support questions and issues that do not qualify for the specific update in question.

The English version of this hotfix has the file attributes (or later file attributes) that are listed in the following table. The dates and times for these files are listed in Coordinated Universal Time (UTC). When you view the file information, it is converted to local time. To find the difference between UTC and local time, use the Time Zone tab in the Date and Time tool in Control Panel.   Date         Time   Version       Size       File name -  22-Sep-2003  19:32  1.0.3705.459    200,704  Aspnet_isapi.dll 22-Sep-2003 19:26                    4,169  Aspnet_perf.h   22-Sep-2003  19:33                  513,302  Aspnet_perf.ini 22-Sep-2003 19:33                  510,714  Aspnet_perf2.ini 22-Sep-2003 19:32  1.0.3705.459     24,576  Aspnet_regiis.exe 22-Sep-2003 19:33  1.0.3705.459     28,672  Aspnet_wp.exe 22-Sep-2003 18:46  1.0.3705.459     69,632  Corperfmonext.dll 17-Jun-2003 22:43                   16,597  Installpersistsqlstate.sql 17-Jun-2003 22:43                   17,331  Installsqlstate.sql 09-Sep-2003 01:28                   17,807  Installsqlstatetemplate.sql 22-Sep-2003 18:44  1.0.3705.459    303,104  Mscorjit.dll 23-Sep-2003 21:34  1.0.3705.459  1,953,792  Mscorlib.dll 22-Sep-2003 18:37                   10,284  Mscorlib.ldo 22-Sep-2003 18:45  1.0.3705.459  2,273,280  Mscorsvr.dll 22-Sep-2003 18:45  1.0.3705.459  2,269,184  Mscorwks.dll 22-Sep-2003 19:30  1.0.3705.459     20,480  Perfcounter.dll 23-Aug-2002 03:23                       15  Smartnav.htm 30-Jul-2003 01:04                    8,728  Smartnav.js   23-Sep-2003  21:33  1.0.3705.459  1,175,552  System.dll 23-Sep-2003 21:32  1.0.3705.459    241,664  System.messaging.dll 23-Sep-2003 21:32  1.0.3705.459    311,296  System.runtime.remoting.dll 23-Sep-2003 21:31  1.0.3705.459    131,072  System.runtime.serialization.formatters.soap.dll 23-Sep-2003 21:34  1.0.3705.459  1,196,032  System.web.dll 23-Sep-2003 21:32  1.0.3705.459    507,904  System.web.services.dll 23-Sep-2003 21:34  1.0.3705.459  1,302,528  System.xml.dll 23-Sep-2003 21:30  1.0.3705.459     32,768  Tlbimp.exe 17-Jun-2003 22:43                    1,419  Uninstallpersistsqlstate.sql 09-Sep-2003 01:28                    2,119  Uninstallsqlstatetemplate.sql 23-Sep-2003 21:30  1.0.3705.459     65,536  Wsdl.exe



STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the &quot;Applies to&quot; section.



MORE INFORMATION
This hotfix package also includes a secondary hotfix. If you set AspCompat to true, and you set an STA object that is stored in session state to null, you may receive a &quot;NullReferenceException&quot; error message when you reload the page. To reproduce the secondary problem, paste the following code inside a Page_Load event. Session[&quot;obj&quot;] = new Object;

Session[&quot;obj&quot;] = null; When you visit this page the first time, the code runs correctly. However, if you reload the page, you receive a &quot;NullReferenceException&quot; error message from Microsoft ASP.NET and you cannot view the page again in the current session.

Stack Trace: [NullReferenceException: Object reference not set to an instance of an object.] System.Web.UnsafeNativeMethods.AspCompatOnPageStart(Object obj) +0 System.Web.Util.AspCompatApplicationStep.OnPageStartSessionObjects +90 System.Web.UI.Page.ProcessRequestMain +44 If you set AspCompat to false, this behavior does not occur.

Steps to Reproduce the Behavior
To reproduce the problem that appears in the &quot;Symptoms&quot; section of this article, follow these steps:  Use Microsoft Visual Basic .NET to create a Visual Basic ActiveX DLL object.

Note If you mark the component as Unattended Execution (UE), the error still occurs. Create two functions in the DLL class:  Create a function that returns a string.</li> Create a function that takes a long parameter and that returns a LONG value. This function does a loop that adds numbers from 1 to the number that is passed to the function.</li></ol> </li> Create a new Web Application project that has two .aspx pages (Page1.aspx and Page2.aspx).</li> In the Global.asax file of the Session_Start event, use CreateObject to create an instance of the object that you created in step 1.</li> In the Page_Load event of Page1.aspx, call either of the functions that you created in step 2 from the object that is stored in session state.</li> Create a hyperlink to Page2.aspx in Page1.aspx.</li> In the Page_Load event of Page2.aspx, call the Session.Abandon method.</li> Use Application Center Test (ACT) to create a test that records the operation of viewing Page1.aspx and then viewing Page2.aspx.</li> Run the test with ten users. See if error 0x8004e005 appears in the Event Viewer.

Note You may have to increase the number of users to reproduce the problem.</li></ol>

<div class="references_section">