Start of application

ASP.NET user id is not available in Application.Start, which allows writing to the database. (The website is running on a shared host, so I couldn't configure the permissions as I needed.) So, I implemented the following in global.asax. I am not sure if it is completely stream protected. I want to remove a delegate to eliminate blocking checking to optimize performance.

I wonder if "if (TheFirstReq! = Null)" can fail. Thanks to

delegate void FirstRequest();
static volatile FirstRequest TheFirstReq;
static object ObjLock = new Object();


void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup
    AppStartDateTime = DateTime.Now;
    TheFirstReq = FirstReq;
}

void FirstReq()
{
    lock (ObjLock)
    {
        if (TheFirstReq == null)
            return;
        TheFirstReq = null;
        // Log Application start.
    }
}

protected void Application_BeginRequest(object sender, EventArgs e)
{
    if (TheFirstReq != null)
        TheFirstReq();
}

      

0


a source to share


1 answer


Your code is not thread safe, the double locking pattern, although logically you might think it is safe, it is actually not safe the way you implemented it. You need to add Volatile in the TheFirstReq declaration.



This MSDN article shows that double check locking is properly implemented.

0


a source







All Articles