Check environment in application_start

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
 
P

Peter Bromberg [C# MVP]

Correct. I know this sounds simplistic, but how about we save the info to a
text file from Application_Start and have the default page read and display
it? If your default page detects there's nothing in the file, it continues,
otherwise we can redirect to an error info page that displays the information
with instructions.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com
 
M

Mark Rae [MVP]

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...
 
M

Mark Rae [MVP]

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);
}
}
 
M

MattB

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

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top