Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Services / September 2006

Tip: Looking for answers? Try searching our database.

Static variables initialization in web services

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mnowosad - 14 Sep 2006 22:43 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.