Patrick Desjardins
Patrick Desjardins

Reputation: 141023

Web service variable shared for the lifetime of the webservice?

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?

Upvotes: 4

Views: 4546

Answers (3)

c00ke
c00ke

Reputation: 2325

A singleton utilizing HttpApplicationState should work a treat

Upvotes: 0

ckramer
ckramer

Reputation: 9443

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.

Upvotes: 12

Scott Evernden
Scott Evernden

Reputation: 39986

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

Upvotes: -1

Related Questions