What's Wrong With This Code? (#11)

Joe Developer is tasked with displaying the start date of his ASP.NET application. Joe thinks he'll just add a DateTime field to global.asax and initialize the field during the Application_Start event.

<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e)
    {
        StartDateTime =
DateTime.Now;
    }
    
    
public DateTime StartDateTime;
          
</script>

Whenever Joe needs to display the application start date, he accesses this public field.

Joe doesn't know what is wrong, but he is sure of one thing – the date that is displaying on his pages is not the date when the application started. Can you help out Joe one more time?

posted on Tuesday, February 06, 2007 11:08 PM by scott

Comments

Tuesday, February 06, 2007 8:31 PM by Rory Primrose

# re: What's Wrong With This Code? (#11)

My initial thought is that it is an instance/scope problem. Making StartDateTime a static should get around that, but kinda ugly. Why wouldn't you use application state?
Tuesday, February 06, 2007 8:55 PM by Eric W. Bachtal

# re: What's Wrong With This Code? (#11)

Not sure I've ever tried to establish instance data during HttpApplication start, but was still surprised to read this:

"You should set only static data during application start. Do not set any instance data because it will be available only to the first instance of the HttpApplication class that is created."
Tuesday, February 06, 2007 9:15 PM by Tyrone

# re: What's Wrong With This Code? (#11)

A public instance variable in the Global.asax does not translate to mean that you can access this variable globally from anywhere in your application. You would have to add it to the application state bag in order to retreive it from anywhere in your application.
Tuesday, February 06, 2007 9:30 PM by scott

# re: What's Wrong With This Code? (#11)

Rory and Eric:

Making the field static would get the code working - but why? :)

Tyrone:

You can get to fields, properties, and methods in global.asax. In an ASP.NET 2.0 web site you actually get a strongly typed ApplicationInstance property on the page that will reference an instance of the class that represents global.asax, so I could write "ApplicationInstance.StartDateTime" to fetch the value.

Notice I say "an instance" of the class. There is more than one instance alive, but only one of those instances will have StartDateTime initialized to the proper start time.
Tuesday, February 06, 2007 10:15 PM by Eric W. Bachtal

# re: What's Wrong With This Code? (#11)

I assume it's because HttpApplication objects are created when needed, pooled, and reused. On any given request you might get the instance that was created with the first request and populated with instance data during the one call to the start event, but more likely you'll get another instance that never got a start event.
Wednesday, February 07, 2007 6:16 AM by Chris D

# re: What's Wrong With This Code? (#11)

I didn't double check to verify, but i'm pretty sure code inside Application_Start is going to re-run every time the app_pool recycles as well, resetting the date.
Wednesday, February 07, 2007 7:39 AM by scott

# re: What's Wrong With This Code? (#11)

Chris: That's correct. Let's say this is what Joe wants (the date and time when the application was lauched).

Eric: Correct. The code in global.asax ends up inside a class derived from HttpApplication. There will be multiple instances of this class in a pool to serve requests, and this is why using the static keyword with that field is one way to fix the problem.
Wednesday, February 07, 2007 9:05 AM by Haacked

# re: What's Wrong With This Code? (#11)

I want to know when you plan on giving Mr. Joe Developer the pink slip.
Wednesday, February 07, 2007 1:46 PM by Rory Primrose

# re: What's Wrong With This Code? (#11)

Because static variables are stored in the AppDomain and are therefore always in memory for the lifetime of the application regardless of any instance of HttpApplication (or any other object for that matter). There might need to be some kind of locking mechanism though for when static data is altered (threading issues???).
Wednesday, February 07, 2007 7:38 PM by Christopher Steen

# Link Listing - February 7, 2007

Creating Custom Configuration Sections in Web.config [Via: ] ASP.NET AJAX Goodies: Documentation Download,...
Wednesday, February 07, 2007 8:42 PM by scott

# re: What's Wrong With This Code? (#11)

Rory: True, that is something to watch for.

In this case we'll assume Joe only reads the field after the app starts, so it's thread safe (ASP.NET guarantees Application_Start is only called once).
Thursday, February 08, 2007 1:48 PM by Rick Strahl

# re: What's Wrong With This Code? (#11)

So what's the actual value (without looking <s>)...

The first instance gets assigned a value because it's the only one that actually fires Application_Start. All others get what? DateTime.MinValue?

Actually I would argue that putting any properties on HttpApplication is a bad idea precisely because of the confusion about lifetime of HttpApplication, but also even if you use a static you can only reference it directly through this particular class. If you use context.ApplicationInstance the var won't show up (even a public instance variable won't) because the instance is cast to HttpApplication.

For statics that are 'application' level I'd recommend using a separate class - I tend to use App. I hang things like a global configuration object off off that App.Configuration, App.APP_NAME, App.EMPTY_DATE, App.StateLookupTable etc. are then accessible anywhere.

Thursday, February 08, 2007 8:59 PM by scott

# re: What's Wrong With This Code? (#11)

Rick:

Yes, when left untouched it comes out as DateTime.MinValue (1/1/0001). I do agree about staying away from HttpApplication.
Monday, February 12, 2007 9:00 PM by K. Scott Allen

# What's Wrong With This Code (#12)


Joe Developer has been fired. Sacked. Terminated. Dismissed. Booted. &quot;Dis-employed&quot;, if you will. ...
Monday, February 12, 2007 9:15 PM by The estatic reading list

# What's Wrong With This Code (#12)

Joe Developer has been fired. Sacked. Terminated. Dismissed. Booted. &quot;Dis-employed&quot;, if you will. Before
Tuesday, March 06, 2007 9:50 AM by Jeremy Simmons

# re: What's Wrong With This Code? (#11)

Create a singleton object in your application to store the objects you absolutely have to ensure are only there once using the Singleton pattern.
http://msdn2.microsoft.com/en-us/library/ms998558.aspx