473,387 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Static variables initialization in web services

As far I know, static variables are tied to AppDomain scopes. So, every time
an executing code within an AppDomain references a class for the the first
time since the AppDomain was created/loaded, the .NET executes the
assignments done in the class static variables declarations and runs the
static constructor of that class.

So, I expected that, as in ASP.NET web site, for a given Web Service site,
the AppDomain would be initialized upon the first request for a web method
and, after that, the following web method requests would execute under the
same AppDomain. That would mean that static variables defined in a Web
Service class could share theri values across web method requests.

However, what I am noticing in my code is that the static variables
initialization and the static constructor are being executed in every single
web method request! What that means? Is the AppDomain under which web service
runs being created/loaded upon each web method request and being unloaded
after the web method processing is done?? That sounds like a huge overhead.
Hope I am wrong.
Sep 14 '06 #1
4 3084
"mnowosad" <mn******@discussions.microsoft.comwrote in message
news:B8**********************************@microsof t.com...
As far I know, static variables are tied to AppDomain scopes. So, every
time
an executing code within an AppDomain references a class for the the first
time since the AppDomain was created/loaded, the .NET executes the
assignments done in the class static variables declarations and runs the
static constructor of that class.

So, I expected that, as in ASP.NET web site, for a given Web Service site,
the AppDomain would be initialized upon the first request for a web method
and, after that, the following web method requests would execute under the
same AppDomain. That would mean that static variables defined in a Web
Service class could share theri values across web method requests.

However, what I am noticing in my code is that the static variables
initialization and the static constructor are being executed in every
single
web method request! What that means? Is the AppDomain under which web
service
runs being created/loaded upon each web method request and being unloaded
after the web method processing is done?? That sounds like a huge
overhead.
First of all, they should not be happening on each web method request,
unless that request is forcing the creation of a new AppDomain.

Second, I hope you know that you have to synchronize write access to these
statics, as they can be referenced by multiple threads (requests) at the
same time.

John
Sep 15 '06 #2

The static variables I am using are declared as readonly. So multi-threading
is not an issue here. But they DO have an expensive initialization process
(placed in the static constructor), which is the reason why I do not want
this initialization to happen upon each web method request.

Notice that this issue (static constructor of the web service class being
executed for every web method request) has already been reported in a C-Sharp
Corner article. The guy even created a helper class (Pool) to go around this
problem (a solution that did not please me). See link below:

http://www.c-sharpcorner.com/UploadF...LDatabase.aspx

Is there anything in the web service declaration that could be causing the
AppDomain to be created in each web method request? I created the web service
class using Visual Studio 2005 wizard and did not modify the generated
template (only added my web methods).

Thanks,
Marcos

"John Saunders" wrote:
"mnowosad" <mn******@discussions.microsoft.comwrote in message
news:B8**********************************@microsof t.com...
As far I know, static variables are tied to AppDomain scopes. So, every
time
an executing code within an AppDomain references a class for the the first
time since the AppDomain was created/loaded, the .NET executes the
assignments done in the class static variables declarations and runs the
static constructor of that class.

So, I expected that, as in ASP.NET web site, for a given Web Service site,
the AppDomain would be initialized upon the first request for a web method
and, after that, the following web method requests would execute under the
same AppDomain. That would mean that static variables defined in a Web
Service class could share theri values across web method requests.

However, what I am noticing in my code is that the static variables
initialization and the static constructor are being executed in every
single
web method request! What that means? Is the AppDomain under which web
service
runs being created/loaded upon each web method request and being unloaded
after the web method processing is done?? That sounds like a huge
overhead.

First of all, they should not be happening on each web method request,
unless that request is forcing the creation of a new AppDomain.

Second, I hope you know that you have to synchronize write access to these
statics, as they can be referenced by multiple threads (requests) at the
same time.

John
Sep 15 '06 #3
"mnowosad" <mn******@discussions.microsoft.comwrote in message
news:0D**********************************@microsof t.com...
>
The static variables I am using are declared as readonly. So
multi-threading
is not an issue here. But they DO have an expensive initialization process
(placed in the static constructor), which is the reason why I do not want
this initialization to happen upon each web method request.
Are these statics in the webservice class itself? Try moving them out.

John
Sep 16 '06 #4
Are these statics in the webservice class itself? Try moving them out.
Yes. They are declared in the web service class.

That's what I have been trying to understand (from a technical point of
view). Why declaring the static variables in the web service class is not a
good idea and why the .NET framework re-initialize them upon every web method
request?

Thanks,
Marcos

"John Saunders" wrote:
"mnowosad" <mn******@discussions.microsoft.comwrote in message
news:0D**********************************@microsof t.com...

The static variables I am using are declared as readonly. So
multi-threading
is not an issue here. But they DO have an expensive initialization process
(placed in the static constructor), which is the reason why I do not want
this initialization to happen upon each web method request.

Are these statics in the webservice class itself? Try moving them out.

John
Sep 16 '06 #5

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

Similar topics

5
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that...
9
by: Allen | last post by:
In a static library, there is a static variable definition. static CLogger::mapFile; In both EXE and DLL, I use the same code. CLogReader uses CLogger::mapFile to do some work. CLogReader...
8
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....
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
3
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
5
by: parag_paul | last post by:
Can a class with out any instances present have it static members updated. If yes, on which object does the update happen. -Parag
17
by: copx | last post by:
I don't know what to think of the following.. (from the dietlibc FAQ) Q: I see lots of uninitialized variables, like "static int foo;". What gives? A: "static" global variables are initialized...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...

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.