How do I show an error happening in Application_Start ?

  • Thread starter msnews.microsoft.com
  • Start date
M

msnews.microsoft.com

Hi

What can you do with the errors occuring in global.asax in Application_Start
(or Init) ? What is the best practice here ? What's the elegant solution in
this case ?

For now (I'm learning) I can think of caching the error(s) and then when the
start page finally shows, it should check that particular cache and show
whatever is inside. This could maybe be combined with some kind of logging
in a text file.

From my tests, any errors in Application_Start are simply discarded, and the
Start page shows as if nothing happened....

Thank you.
Alex
 
M

msnews.microsoft.com

Me again - I'll try to be more explicit - if the error is in
Application_Start, I understand that the error will occur ONLY ONCE, after
I'm done with editing the global_asax file. When I run the project again, it
won't even pass by there, so in this case if I need to rely on some value
set in Application_Start I will be screwed.

So I thought that this means that, to avoid this possibility, it is actually
not a good thing to have code in "Application_Start", but rather in "Init".
I read that the Init event is raised each time a new instance is created
(already this is *NOT* a very good thing, because I might want to execute
that code only once, and not once for each instance). However, to my grand
surprise, the same thing happens - my code, which is set to provoke an
error, is executed, again, only once - when I "F5" again in VS.Net, the
execution does not even pass by INIT. Is this NOT a new instance ???

Then I thought: Let's imagine that I need to cache a 'Settings' table to be
shared across all instances of my application, so I write code for reading
that table in "Application_Start".

Let's say that this table belongs to the database "A" on server "SA". Let's
say that the rest of my app works with tables in the database "B" on server
"SB". What happens in the case where I start the app normally, and then SA
fails and I need to read that table again because the cache has been cleared
by the system ? Since the app will never pass thru "Application_Start"
again.... what can I do short of restarting SA and asking *ALL* the users to
restart their applications ?
What if one of the many users does not close the application ? That would
mean that all instances but one have been closed, and that would mean that
my application is still not closed i.e. I cannot execute the code in
Application_Start.

Sorry, I'm very confused about this - Application vs. Instances, and error
handling, not to mention that not even INIT is executed on each run. Could
you please be so kind to clarify this for me ?

Thank you for reading this long post.
Alex.
 
G

Guest

Hi,
my suggestion is to create an object, that has method for filling the table,
that you want to be available in the whole app. This object also has method
that gets that table. This method checks if the table is already in an
application variable and if not, gets it from DB and places it in the app
variable(using the method for filling the table). If it is already there, it
simply gets it and returns the result.
In the Application_Start event handler, you just have to call the first
method, I described. Every time in the code, you need data from the table,
call the second method.
Hope that works for you.
Regards,
Kostadin Kostov
 
S

Scott Allen

Let's start with just the Init event questions:

Each HttpApplication instance does fire Init when it instantiates, but
these objects are then pooled and used throughout the life of the
application. It's possible then that you'll only see Init fired a
handful of times.

http://odetocode.com/Articles/89.aspx


Make sense?
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top