Web service variable shared for the lifetime of the webservice?

 ̄綄美尐妖づ 提交于 2019-12-07 08:24:03

问题


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?


回答1:


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.




回答2:


A singleton utilizing HttpApplicationState should work a treat




回答3:


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



来源:https://stackoverflow.com/questions/292831/web-service-variable-shared-for-the-lifetime-of-the-webservice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!