Static Fields in ASP.NET Webservice

Is a static variable in a web service shared between all running web service calls on the server?

In my case, I want to have synchronization on the server, and I can do this with a single

private static Object syncHandle = new Object(); 

Is it correct?

+4
source share
3 answers

Yes, they are all divided into AppDomain, therefore, in general, they should not be used!

They should not be used, in general, because they are unlikely to be used properly. In addition, safer alternatives exist, such as HttpContext.Cache or even session state.

However, if you encapsulate all access to these static members, and if you do the locking correctly, then you will have a safe implementation, which may turn out to be a bottleneck, while all threads are fighting for a shared resource. It really is better to get by.

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

+2
source

I believe that they are separated as long as they work in one process. Thus, two users requesting from the same server will have the same instance of the object. However, you can run different applications on a computer in a different process in IIS, in which case I am sure that object instances will not be used.

+1
source

They are all separated if you do not have a web garden. A web garden is a multi-host process that processes a single application. In this case, each host will have its own static data.

+1
source

All Articles