473,587 Members | 2,291 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static Constructor in ASP.NET app

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
Oct 16 '08 #1
5 4452
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.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

*************** *************** **************
| Think outside the box! |
*************** *************** **************
"Dave" <ch********@yah oo.comwrote in message
news:10******** *************** ***********@w24 g2000prd.google groups.com...
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
Oct 16 '08 #2
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.
"Dave" <ch********@yah oo.comwrote in message
news:10******** *************** ***********@w24 g2000prd.google groups.com...
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
Oct 16 '08 #3
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)
"Dave" wrote:
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
Oct 16 '08 #4
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

On Oct 16, 12:48*pm, bruce barker
<brucebar...@di scussions.micro soft.comwrote:
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)

"Dave" wrote:
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- Hide quoted text -

- Show quoted text -
Oct 16 '08 #5
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)
"Dave" wrote:
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

On Oct 16, 12:48 pm, bruce barker
<brucebar...@di scussions.micro soft.comwrote:
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)

"Dave" wrote:
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- Hide quoted text -
- Show quoted text -

Oct 17 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
11819
by: Kirk Marple | last post by:
Just want to see if this is 'by design' or a bug... I have a common List<T> defined in a base class, and the base class has a static property to expose this list. I wanted the derived class to add items to this list, but have the base class hold the data and expose the property. Problem is, however, that the derived class' static...
6
1435
by: Marek | last post by:
Hi, I am analyzing Duwamish7 source code boundled with Visual Studio .NET 2003. Could anoybody explain why the Monitor.Enter and Monitor.Exit block is used inside a static constructor? The code can be found in Project SystemFramework within module ApplicationLog.cs. Here comes the sample. namespace Duwamish7.SystemFramework { (some...
11
3814
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of...
5
3078
by: Tina | last post by:
I'm using C# 1.1.............. I add new Class item to my project. It's created as class Math I change it to public static class Math I get an error saying that "The modifier 'static' is not valid for this item. Why? Are static classes not allowed??
12
3420
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this static constructor of the base class would be invoked even if a an object of the derived class is created. Is this correct ? Is there any way to...
7
2140
by: Morgan Cheng | last post by:
In the book *Programming C#* 4th editionby Jesse Liberty, it reads "Actually, the CLR guarantees to start running the static constructor before anything else is done with your class. However, it only guarantees to *start* running the static constructor; it doesn't actually guarantee to *finish* running it." Page 82, Chap 4 Classes and...
12
3563
by: chandu | last post by:
hello, i want to know usage of static methods in a class. is it advantageous or disadvantage to use more static methods in a class. thank u
8
8918
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it. Like, for instance the Objective-C method: +(void)initialize Which has the following characteristics: It is guaranteed to be run
6
7249
by: =?Utf-8?B?TWF0dA==?= | last post by:
I'm having a problem with a static class constructor being called twice. I have the static class MasterTaskList which uses a BackgroundWorker to execute multiple methods on a separate thread. The static constructor calls a reset function which creates a new instance of BackgroundWorker and attaches the appropriate event handlers. There is also...
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2347
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.