Check environment in application_start

  • Thread starter Thread starter MattB
  • Start date Start date
M

MattB

I'm trying to put some code in my asp.net 1.1 application that checks
some dependencies. I want to alert the user if something is missing or a
component version is out of sync.
I've been able to do the checking logic, but can't do what I want from
the application_start event. What I fist intended to do was set a
session variable, Session("lasterror") and redirect to my internal error
page that would tell the user what happened.
But I can't seem to be able to set a session variable OR redirect from
this event ("...not available in this context."). So what do you people
do in situations like this? I had considered the session_start event but
that would fire too often and I'd be afraid of performance issues if a
site was busy.

Thanks,
Matt
 
I'm trying to put some code in my asp.net 1.1 application that checks some
dependencies. I want to alert the user if something is missing or a
component version is out of sync.
I've been able to do the checking logic, but can't do what I want from the
application_start event. What I fist intended to do was set a session
variable, Session("lasterror") and redirect to my internal error page that
would tell the user what happened.
But I can't seem to be able to set a session variable OR redirect from
this event ("...not available in this context.").

That's right - Application_OnStart occurs *before* the first session is
created i.e. before the Session_Start event fires for the user whose
accessing the site caused it to start up... This means that, although the
the Application and Server built-in objects are available within
Application_OnStart, very little else of that nature is... As you've
discovered, trying to reference the Session, Request, or Response objects in
Application_OnStart event causes an error....
So what do you people do in situations like this?

In similar situations, I've set a flag in the app's backend database...
I had considered the session_start event but that would fire too often and
I'd be afraid of performance issues if a site was busy.

The Session_Start event would fire no more often than normal, since it fires
every time a new Session is created... If your app has to query the backend
database in Session_Start, this would seem a fairly suitable place to check
for your working environment...
 
Sometimes simplistic is best. Thanks for the idea!

How about...?

protected void Application_Start(Object sender, EventArgs e)
{
Application["OkToRun"] = <code to check viable environment>;
}

protected void Session_Start(Object sender, EventArgs e)
{
if ((bool)Application["OkToRun"])
{
Response.Redirect("homepage.aspx", false);
}
else
{
Response.Redirect("errorpage.aspx", false);
}
}
 
Perfect. Thanks!

Matt
Sometimes simplistic is best. Thanks for the idea!

How about...?

protected void Application_Start(Object sender, EventArgs e)
{
Application["OkToRun"] = <code to check viable environment>;
}

protected void Session_Start(Object sender, EventArgs e)
{
if ((bool)Application["OkToRun"])
{
Response.Redirect("homepage.aspx", false);
}
else
{
Response.Redirect("errorpage.aspx", false);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top