Forum Moderators: open

Message Too Old, No Replies

Global.asax On_Start Variables resetting

         

ordonezi

1:18 am on Nov 18, 2003 (gmt 0)



Hi everybody,

I am having a problem with an ASP.NET application i am working on. I new to ASP.NET so I'm not sure if I'm doing this right.

I am working on a website that has an oil price listed throught several pages. In the global.asax file i have the following code.

Sub Application_Start(ByVal sender As Object, ByVal e
As EventArgs)
' Fires when the application is started
Application("OilPrice") = "1.14"
Application("PrePay") = "1.14"
End Sub

I reference these variables on other pages. Everything works correctly up to this point. I have another page were the price can be changed. Once a new price is changed it reflects on all pages. The problem is after a few hours or the next day. The price always resets to the "1.14" in the global.asax file. It was my understanding that the Application_Start event only occurs once, the first time the application is accessed.

Is my assumption correct? If so what could be causing the variables to be reset? Thanks!

plumsauce

3:26 am on Nov 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




I am *GUESSING* that this might have something
to do with the httphandler.

IIS6 has a new default behaviour where the
httphandler is restarted periodically.

If that is happening, then it would not be
surprising that the global.asax is being
fired again.

If this is happening then you need to have
an online source for this info that global.asax
can grab the correct number from without waiting
for a manual update.

garann

11:47 pm on Nov 18, 2003 (gmt 0)

10+ Year Member



From what I understand of IIS's handling of Applications, it's sort of like "last one out turns off the lights". The Application object is automatically destroyed once the last Session accessing it expires.

If that's correct, you should be able to use the Application_OnEnd event to save the current values of those variables to a database or XML file before the Application shuts down. Then in Application_OnStart, you can read those variables back, rather than use static values. That may even work if, as plumsauce says, the problem is that the httphandler is restarting.

Good luck!
g.

TheNige

8:13 pm on Nov 19, 2003 (gmt 0)

10+ Year Member



Yes, if you really need to store those values long term...and they are dynamic you would be better off putting them into the database, then use the Cache object to store them so you don't have to hit the DB all the time.

Then you would only hit the DB when your application is restarted for the first time, the values are changed, or your cache entry expires. This way your data will persist over application_restarts, reboots, and other things.

Don't hard code any values unless they are going to be the same for a very long time.

RossWal

9:23 pm on Nov 19, 2003 (gmt 0)

10+ Year Member



Another idea, if you don't have/need a database is to store the stuff in XML. I find XML works great as a scratch pad for up to a couple hundred rows. Actually, I don't know where the trade-off of having index efficiencies of a RDBMS would off-set the cost of connecting, etc. Probably well over 200 rows.

.Net has very simple XML functions built in. Look at the ReadXML & WriteXML methods on the dataset object.

Ross