Current date in a static property?

  • Thread starter Thread starter cowznofsky
  • Start date Start date
C

cowznofsky

We have an app load balanced across two servers, and just ran into a
date-related problem where we get different results on each server.

There is a static property that I think may be the cause, which looks
like this:

public static readonly string ZERO_TO_2_DAYS = DateTime.Now.AddDays
(2).ToShortDateString();

When trying this on my machine today I got a value of 3/11. Then when
I recompiled I got the correct value of 3/12. So I'm thinking this
might get cached.

What do you think?
 
This is just a static field initialized when the class is first accessed....
What if you use a property ?

--
Patrice

"cowznofsky" <[email protected]> a écrit dans le message de groupe de
discussion :
(e-mail address removed)...








- Show quoted text -

I'm just wondering if the existing code would be considered an
incorrect technique. If it's initialized when the class is first
accessed, should this happen at the beginning of each user's session?
My experimenting makes it look like it persists beyond each session.
 
cowznofsky used his keyboard to write :
I'm just wondering if the existing code would be considered an
incorrect technique. If it's initialized when the class is first
accessed, should this happen at the beginning of each user's session?
My experimenting makes it look like it persists beyond each session.

That is correct: the lifetime of a static variable is the lifetime of
the *application*. A WebApplication is a single application that serves
multiple users concurrently.

So your datestring gets initialized once in the lifetime of the entire
webapplication and then never changes until the webapp is restarted.

Hans Kesting
 
A static variable is shared by all users (basically it is initialized once
for the whole application an ASP.NET is a single application used by
multiple users).

Thanks to you both, that's what I was wondering. It definitely looks
like a bug in the way this was coded.
 
Back
Top