Current date in a static property?

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?
 
C

cowznofsky

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.
 
H

Hans Kesting

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
 
C

cowznofsky

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top