views:

925

answers:

3

Is a static variable in a webservice shared between all running invocations of the webservice on a server?

In my case I want a server-wide sync-lock, and I beleive I can accomplish that with a single

private static Object syncHandle = new Object();

Is that correct?

+1  A: 

Yes, they are all shared per AppDomain which is why, in general, they should not be used!

They should not be used, in general, because they are so unlikely to be used properly. Also because there are safer alternatives, like HttpContext.Cache, or even Session state.

Still, if you encapsulate all access to these static members, and if you handle locking correctly, then you'll have a safe implementation that may then turn out to be a bottleneck, with all threads contending for the shared resource. It's really better to do without.

Also, you seem to mean ASMX web services, but you should specify ASMX or WCF.

John Saunders
why shouldn't they be used? I would say that static objects have a very good use in web applications, especially for the purpose of caching shared data.
Kibbee
If you want to cache shared data with the Application cache
JoshBerke
+1  A: 

I believe they are shared as long as they are running in the same process. So two people requesting from the same server would have the same instance of the object. However, you can run different applications in a computer different process on IIS, in which case I'm pretty sure that instances to objects wouldn't be shared.

Kibbee
+1  A: 

They are all shared unless you have a Web Garden. A web garden is multiple host process handling a single application. In this case each host will have its own static data.

JoshBerke