views:

786

answers:

3

How can I make a variable (object) available for the whole lifetime of the webservice?

Static variable seem to work but is there an other way to do it?

A: 

probably, but if static variable works then move on to the next problem ! :)

Scott Evernden
+5  A: 

Static variables exist for the lifetime of the App Domain that contains them. In the case of a web service, this is usually the ASP.Net Worker Process. What that means is that when IIS decides to cycle the worker process, your static variable will be gone. This may be what you want, in which case it is probably a good choice. (Putting asside discussions of whether or not static variables are proper in a given context).

Within the scope of a web service you also have access to the HttpApplicationState via the Application property (this would be an asmx service...not sure if WCF is the same or not), so this could also be a good choice for stashing something needed for the lifetime of a service.

ckramer
The HttpApplicationState is something that interest me, thx!
Daok
A: 

A singleton utilizing HttpApplicationState should work a treat

c00ke