IIS 7 : To Execute function/code for very first request only
//In Following method call to FirstRequestInitialization is done every time beginRequest called for a Application.
protected void Application_BeginRequest(Object source, EventArgs e)
{
HttpApplication app = (HttpApplication)source;
HttpContext context = app.Context;
// Attempt to peform first request initialization
FirstRequestInitialization.Initialize(context);
}
//Following Class will check the Flag(s_InitializedAlready) and according that it will execute Initializer function and check that request comes for first time or not.
class FirstRequestInitialization
{
private static bool s_InitializedAlready = false;
private static Object s_lock = new Object();
// Initialize only on the first request
public static void Initialize(HttpContext context)
{
if (s_InitializedAlready)
{
return;
}
lock (s_lock)
{
if (s_InitializedAlready)
{
return;
}
// write your code here for first-request initialization ...
}
}
}
protected void Application_BeginRequest(Object source, EventArgs e)
{
HttpApplication app = (HttpApplication)source;
HttpContext context = app.Context;
// Attempt to peform first request initialization
FirstRequestInitialization.Initialize(context);
}
//Following Class will check the Flag(s_InitializedAlready) and according that it will execute Initializer function and check that request comes for first time or not.
class FirstRequestInitialization
{
private static bool s_InitializedAlready = false;
private static Object s_lock = new Object();
// Initialize only on the first request
public static void Initialize(HttpContext context)
{
if (s_InitializedAlready)
{
return;
}
lock (s_lock)
{
if (s_InitializedAlready)
{
return;
}
// write your code here for first-request initialization ...
}
}
}
Comments