Web 服务变量在 Web 服务的生命周期内共享吗?
如何使变量(对象)在 Web 服务的整个生命周期内可用?
静态变量似乎可以工作,但是还有其他方法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
静态变量在包含它们的应用程序域的生命周期内存在。 对于 Web 服务,这通常是 ASP.Net Worker Process。 这意味着当 IIS 决定循环工作进程时,您的静态变量将消失。 这可能就是您想要的,在这种情况下,它可能是一个不错的选择。 (抛开静态变量在给定上下文中是否合适的讨论)。
在 Web 服务范围内,您还可以访问 HttpApplicationState< /a> 通过 Application 属性(这将是一个 asmx 服务...不确定 WCF 是否相同),因此这也可能是存储服务生命周期所需内容的不错选择。
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.
使用 HttpApplicationState 的单例应该会很不错
A singleton utilizing HttpApplicationState should work a treat
可能,但如果静态变量有效,那么继续下一个问题! :)
probably, but if static variable works then move on to the next problem ! :)