PDA

Click to See Complete Forum and Search --> : Having problems with the server hosting my site [Resolved]


Patch21
Jan 1st, 2005, 10:23 AM
Hey all.

I've just developed a website in vb.net / asp.net and i'm testing it on the host's server, and i'm getting one annoying problem and I do not know why it is happening.

I have a module where I instantiate a class in the Sub Main. This class contains all the logic. On startup of the website I call this Sub Main of the module in the Page_Load of a blank form, and then straight afterwards response.redirect to another page.

On my machine, this works perfectly and the instance of the class remains in memory. However, on the server all memory seems to be cleared after I response.redirect to the new form. Why would this happen? The instance of the class just dissappears from memory on that new form. This doesn't happen on my machine.

Any help will be great.
Thanks and happy new year :P

pvb
Jan 1st, 2005, 11:48 PM
Hmm, well the lifecycle of a page in ASP.NET dictates how long something is in memory. A class instantiated in the page load event, will last as long as the request is processing. Once the page is done and content delivered to the requesting client, consider anything instantiated during the page load, or any event for that matter, gone. When Response.Redirect is called, that sends out a message to the browser to request whatever page is passed to Response.Redirect. Once that happens, anything previously instantiated and not persisted somewhere that exists between client requests is essentially gone. It might appear that classes you instantiate on your local machine maintain their state from request to request but unless you've implemented some sort of mechanism to make that seem to happen, they don't. If you want whatever class you've instantiated to "live" from request to request, consider saving the instance to cache or application/session state.

Patch21
Jan 2nd, 2005, 11:47 AM
Thanks for the reply.

What I am ideally looking for is that this one instance exists for the entire time that the server is up and running. In other words, from the time the server is initially started up and the web site running for the first time, until the server crashes or goes down for maintenance.

I never realised that this instance would get lost after the request is processed, this is very new to me :). What I thought would happen was that the instance would exist in memory on the server, and that all users would be using this instance. Obviously I am wrong here. I think my knowledge is a bit limited when it comes to application / session states.

Would it be possible to have this one instance running in the background for all users to use ? I will look at your option of storing it in the cache or application / session state.

Thanks

Patch21
Jan 2nd, 2005, 12:07 PM
I'm guessing I should be looking at the Application state, as this exists for the entire time the application is running.

I'm looking at the global.asax file, containing the sub :


Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
End Sub


Would I be correct in saying that the instance should be declared in that sub ?

pvb
Jan 2nd, 2005, 01:19 PM
You could put the code there to start things off, sure. You'll need some way of referencing whatever you stick in application state or cache. As an example, if I'm going to persist a DataTable across requests and across users I'll do something like this:
private DataTable getData()
{
string cacheKey = "MyCachedData";
DataTable dt = null;
if( HttpContext.Current != null && HttpContext.Current.Cache[ cacheKey ] != null )
{
//Get the datatable from cache.
dt = HttpContext.Current.Cache[ cacheKey ] as DataTable;
}
else
{
//Load the datatable from the database.
string connString = "db connection string here";
string cmdText = "select * from sometable";
using( SqlConnection cn = new SqlConnection( connString ) )
using( SqlDataAdapter da = new SqlDataAdapter( cmdText, cn ) )
{
dt = new DataTable();
da.Fill( dt );
}
//Now cache the data for all other requests
if( HttpContext.Current != null )
{
HttpContext.Current.Cache[ cacheKey ] = dt;
}
}
return dt;
}(sorry, it's in c#,I write faster that way) The idea is that whenever i need the data, if it's in cache grab it from there, if for some reason it's not in cache, get it again and cache it so it's there next time. You could if you wanted to stick the code that initially loads the object you're persisting in the Application_Start event handler. Since that event is only going to fire when the application starts, you might wanna make sure there's a way to reload that class instance if for some reason it gets "lost"(ie some of your code sets it to null on accident), and restarting the app is not an option.

Patch21
Jan 2nd, 2005, 01:53 PM
I've been messing around with the application object and it seems to be working good so far. Thanks for the help and the code! I'll probably be back if I run into any more problems :)