This has been on my mind for a while now. I posted a thread a couple of weeks ago about my Session variables being lost across requests from the client to the server, and we came to the conclusion that it was because of the server having multiple asp.net worker processes, and that all requests from one IP were being distributed across these processes. So the session was being stored on the initial process, and all ensuing requests were along other processes where the session was not stored.
I also tried adding a variable to the application state to see if that worked, but this variable was also lost across requests.
At the moment I have one instance of a class (containing my logic and objects' info) that is being used by all users of the web app. I globally declare this instance in a module, and this module is being initialized in the Application_Start event in the Global.asax file.
What I cannot understand now is how this class maintains its state across requests. How does it keep all the objects contained in it if I do not even add the instance to the application or session state ?
Confused !ok I just read these 2 posts:
http://www.dev-archive.com/vb2themax/Tip/18845
http://weblogs.asp.net/wallen/archive/2003/10/21/32854.aspx
Is is true that a variable declared in a vb.net module like so:
Public mClass As ClassName
...is static and will it keep its value across requests, similar to the Application Object?
Yes, but unlike the Session every user hits the same variable. But, that is the same as anything stored in application.
What are you using the application variable for?
It's just a counter that I use to increment everytime an event occurs.
I'm adding it to the application state from a web service I made.
Session, Application, Cache, and even using a static member in a module are powerful things, but if you don't know how they work, you will be left with odd things happening. I suggest you take some time to read about them on the http://msdn.microsoft.com site.
Just because you moved a variable to a module, doesn't mean it won't get lost. ASP.NET is designed to shut down if there are no current requests to it (within a window of time). Your static variable in the module will go away if this happens.
The only sure way to persist a value is on disk, or in a database. Period. You should be looking at these options if you continue to have trouble.
ASP.NET can use SQL Server as it's session state store. This is pretty powerful when your servers are in the mode they are (web garden or web farm).
Yeah I've been trying to read up on them as much as I can. State is quite a big issue when it comes to large web apps. Thanks for the input. Do you know how long this 'window of time' is approximately? It has to be fairly long time doesn't it ?
What I would perhaps like to do then is to check if this instance (that I declare in the module on Application_Start) still exists whenever a client requests a page from the server and then initialize the module again if it doesn't. I do not want to put this in the form load event of the first form as the variable will only last as long as that form does (that it what was happening last time I think!). Is there a general event that is called when a request is made to the application? I could put my code in there...
Ok nevermind, found it :)
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires at the beginning of each request
End Sub
Yep, the Global.asax file contains those events.
Also, you can configure how the aspnet worker process behaves and session timeouts behave by adjusting settings in your web.config, or the machine.config. Be careful when messing with the machine.config, I have hosed my system before playing with that...lol. I go real slow with it now and read all I can about the setting I am about to change.
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment