ASP.NET Web 服务中的静态字段
Web 服务中的静态变量是否在服务器上所有正在运行的 Web 服务调用之间共享?
就我而言,我想要一个服务器范围的同步锁,并且我相信我可以通过单个同步锁来实现这一点,
private static Object syncHandle = new Object();
这是正确的吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,它们都是每个 AppDomain 共享的,这就是为什么一般来说不应该使用它们!
一般来说,不应使用它们,因为它们不太可能被正确使用。 还因为有更安全的替代方案,例如 HttpContext.Cache,甚至会话状态。
尽管如此,如果您封装对这些静态成员的所有访问,并且正确处理锁定,那么您将拥有一个安全的实现,但该实现可能会成为瓶颈,因为所有线程都会争夺共享资源。 确实最好不要这样做。
另外,您似乎指的是 ASMX Web 服务,但您应该指定 ASMX 或 WCF。
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.
我相信只要它们在同一进程中运行,它们就是共享的。 因此,从同一服务器请求的两个人将拥有该对象的相同实例。 但是,您可以在计算机上的 IIS 上的不同进程中运行不同的应用程序,在这种情况下,我非常确定对象的实例不会被共享。
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.
除非您有网络花园,否则它们都是共享的。 网络花园是处理单个应用程序的多个主机进程。 在这种情况下,每个主机都有自己的静态数据。
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.