472,125 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,125 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 2984
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Per Bull Holmen | last post: by
5 posts views Thread by parag_paul | last post: by

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.