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.
John Saunders - 15 Sep 2006 18:29 GMT
> As far I know, static variables are tied to AppDomain scopes. So, every
> time
[quoted text clipped - 17 lines]
> 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
mnowosad - 15 Sep 2006 22:51 GMT
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/UploadFile/jbailo/MultiuserXMLDatabase1123200502322
1AM/MultiuserXMLDatabase.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
> > As far I know, static variables are tied to AppDomain scopes. So, every
> > time
[quoted text clipped - 26 lines]
>
> John
John Saunders - 16 Sep 2006 02:27 GMT
> 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
mnowosad - 16 Sep 2006 03:00 GMT
> 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
> > The static variables I am using are declared as readonly. So
> > multi-threading
[quoted text clipped - 5 lines]
>
> John