views:

369

answers:

2

Hey guys, I apologize for this basic question, but for the life of me I can't seem to find an answer.

I previously asked a question here about how to dynamically store temporary images and handle their cleanup on a web server's file system. (using C# in .NET 3.5)

It was suggested that I use a global.asax file to handle this.

I just can't figure out how this thing works.

I have two separate applications... So far, I have figured out that the global.asax is supposed to be in the root directory of the website.

Questions:

1) How to I get the global.asax to fire for only these two specific applications.

2) both applications need to create a list of strings (the file locations) then delete them on the application termination. Do I instantiate this array in the app, or in the global.asax?

my code will look like this

List<string> fileLocations = new List<string>();  //I don't know where to put this.

//The next line of code will be in both applications (this could be called many times in one application session.  The names of the files are generated from the clock (milliseconds and seconds combined, I might change this to use the actual random class combined with sessionID)
fileLocations.Add(file);

void Application_End(object sender, EventArgs e) 
{
    //  Code that runs on application shutdown
    foreach(string file in fileLocations)
    {
        if(File.Exists(file))
            File.Delete(file);
    }    
}

I am confused about how the global.asax actually works. Is it used like an interface?

+1  A: 

MSDN Link and Wikipedia Link are places to start.

It isn't used like an interface, more like a filter that has a bunch of different hooks for things like starting the Application object or a Session or a Request. The Wikipedia link has a list of calls that the Global.asax can make though I would suggest stepping back for a moment to understand how a web application ends compared to a desktop application. IIS can recycle an AppPool as one way to end the application though there are others so setting a cleanup on application end may not be the best idea if that event never fires.

JB King
I read both of those, but these lead me to more confusion.It would seem to me that these suggest that the global.asax is solely used store variables that can be accessed from ANY session or ANY application. Which is useful, but not for what I am doing right now. I was under the impression that the global.asax could create threads for handling an individual application start up and an individual application end.
Dave
I think you may have a misunderstanding on the use of the Session and Application objects in ASP.Net. A Session exists for each user connected so that if user A has a variable stored in the Session object, it isn't accessible to anyone but user A. In contrast, the Application object is truly global in that there is only one of it when the server is running, thus there has to be care in using it as if you want to store user A's information in the Application object, you have to use some way of marking the key so that it is unique to that user. You could create a thread anywhere in ASP.Net
JB King
+2  A: 

A good place to look at how to use Global.asax is to read the ASP.NET Quickstart on it's usage. You have one per web application / site. It's kind of like Global level events.

Be aware, the Application_End event will not fire often, on most servers. It will only fire if the IIS app pool is unloaded/recyclyed, web.config modified , assemblies changed in /Bin,or someother situation where the webserver stops. On a popular site it could be weeks, months before your event ever fires.

Dan Diplo
So there is no simple way to just detect when someone is done using an application and run a clean up script? I feel like this should be a lot easier than it is :/
Dave
Well, if the user is logged in then you could use the Session_End event also in Global.asax By default it's 20 mins, but you can set your own limit. See http://stackoverflow.com/questions/621744/how-to-handle-session-end-in-global-asax and http://justgeeks.blogspot.com/2008/07/aspnet-session-timeouts.html
Dan Diplo
Ah, ok. So Then if I declare a List in the global.asax file, will it instantiate a new list for every user's session, or do I declare the list in my program and somehow the global.asax has access to it.Like I said, I apologize for the basic questions... I am usually pretty good at figuring this stuff out on my own but for some reason the global.asax is not very intuitive to me.
Dave
Create your fileLocations List in your program, as normal. Perhaps use a SessionID to create a unique folder for each user and place files in that folder. Then in Session_End you can delete the folder that relates to that SessionID. Does that make sense? You may also want to have another job in Application_End that deletes ALL files, just to clean-up any that get missed (this will only fire occasionally when web-server restarts etc).
Dan Diplo
ohhhhhhhh, that makes perfect sense now. Wow, I was looking at it from a completely wrong perspective. I kept thinking I had to implement it like some sort of abstract class or interface. Thank you so much for your help!
Dave