Pregunta

Recently we started seeing a problem where the Application_Error event handler (for HttpApplication.Error) is being invoked on a different thread from where the request was handled.

Things we have changed recently:

  • 32 bit to 64 bit
  • Classic to Integrated Pipeline mode

For what its worth, here's some code that might help explain this:
In one representative test, the thread shown on the page is 7, where the one in the email is 10.

//The application
public class MyApplication : HttpApplication
{
    protected virtual void Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    {
        var threadId = System.Threading.Thread.Current.ManagedThreadId;
        SendEmail("There was an error on threadId " + threadId.ToString());
    }

    private void SendEmail(string message)  
    { 
        //snip 
    }
}

//Some aspx page
<%
var threadId = System.Threading.Thread.Current.ManagedThreadId;
throw new Exception("This is a test.  ThreadId = " + threadID.ToString());
%>

This causes issues for us as we are storing authentication information in Thread.CurrentPrincipal, and we need to log that information with the exception.

How can I either keep it on the same thread, or make IIS give me the CurrentPrincipal from the original thread?

¿Fue útil?

Solución

I'm assuming you're seeing this behavior because ASP.Net uses different threads for executing an ASP.Net page and handling an error.

You should be able to solve your problem by using HttpContext.Current.User instead. However, there can be subtle differences between these. You can check this article that explains that they can point to different objects (but basically only do if you make it that way)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top