Static Constructor in ASP.NET app

D

Dave

Hello,

Suppose you have a class with a static property with only a get (read
only). You also have code in a static constructor that sets these
properties but takes 1 hour to run.

Now suppose the first request comes in at 11:00 am and tries to read
from this property. It will need to wait an hour until the page loads
which is fine and makes sense. If a second (different) request at
11:01 am comes in and tries to read the same property, will it wait
until the static constructor is finished? I'm hoping it does... If it
doesn't then the 2nd request will read dirty data from un-initialized
properties.

In my scenerio, the static constructor runs much quicker, but it's a
high traffic mission critical financial application that must read the
info each time correctly.

Thanks!
Dave
 
C

Cowboy \(Gregory A. Beamer\)

What is it calculating? Can it be done offline (pre-aggregated outside of
the web app) and then run from the work done real time?

I see no reason to run long running processes in page code.
 
G

George

Good question. I was asking that one myself.

According to documentation it will wait.
"A CLR type, such as a class or struct, can have a static constructor, which
can be used to initialize static data members. A static constructor will be
called at most once, and will be called before the first time a static
member of the type is accessed."

But i would check before trust it :)


George.
 
B

bruce barker

yes. the runtime takes a lock, so no code can access the class statics until
the constructor has completed. of course if it took an hour, asp.net would
try to kill the thread, which would not die until the constructor completed,
so a recycle would probably happen.


-- bruce (sqlwork.com)
 
D

Dave

Thanks Bruce,

That's what I figured. My static constructor doesn't really last an
hour, it's just a hypothetical situation (probably a bad choice of an
example). My static constructor really only should take about a second
or so. I'm simply trying to find out if all requests to the same
static property will wait for the static constructor to finish. This
happens to be very important for my app.

Cowboy, sorry I chose an hour; bad example.
George, I think you missed my point. I know the constructor will
finish before reading the static property. What I'm trying to find out
is if a 2nd (different) request for the same static property comes in
before the static constructor is finished executing from the first
request - what happens then? I would guess that it autamically
"locks" like bruce suggested, but if it didn't I'd have some issues.

Or to rephrase - I don't need to put any locking into my constructor
correct?

Big picture, I've always been a big fan of static methods and static
properties in an ASP.NET. Used correctly, it can really improve speed/
effeciency on high traffic sites. However, using static properties
you have to be carefull for multi-threaded locking issues. These
issues only arrise when you try and read and write to the same
memory. Therefore, I've avoided this by just having static properties
that are "get" only and methods with only local variables. The one
exception is in the static constructor where I "write" to these static
properties. This is what led me to this post.

Thanks!
Dave
 
B

bruce barker

you could also implement your own locking (inside the property), and load the
value on first reference. if you have static member that take time to load,
but not refenced a lot this may help.

also .net will call the static contructor on the first reference to the
static object (not at startup).

-- bruce (sqlwork.com)
 

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,997
Messages
2,570,241
Members
46,833
Latest member
BettyeMacf

Latest Threads

Top