[英]How do I implement shared state in an ASP.NET MVC 3 application?
I'm writing a basic RESTful service, and decided I'd use ASP.NET MVC 3 for the task. 我正在编写一个基本的RESTful服务,并决定将ASP.NET MVC 3用于该任务。 My application is going to be responsible for maintaining a persistent connection to a server per user (for now). 我的应用程序将负责维护每个用户与服务器的持久连接(目前)。 I had assumed that Application_Start
is the place to register static/shared state (like persistent connections), but after reading the documentation for Unity.MVC3, it appears that each request/response cycle will trigger the creation of services (by calling Application_Start). 我以为Application_Start
是注册静态/共享状态(如持久连接)的地方,但是在阅读Unity.MVC3的文档之后,似乎每个请求/响应周期都会触发服务的创建(通过调用Application_Start)。
The documentation I refer to says: 我引用的文档说:
On every request, one UpperCaseService, one LowerCaseService and one ExampleContext are instantiated by DependencyResolver via Unity. 对于每个请求,DependencyResolver会通过Unity实例化一个UpperCaseService,一个LowerCaseService和一个ExampleContext。 At the end of the request, the ExampleContext is automatically disposed 在请求结束时,ExampleContext被自动释放
After reading other documentation, and from what I already assumed, Application_Start would be called per AppDomain spawned (again assumed that this would be in the vicinity of how many cores there are on the server). 在阅读了其他文档之后,并且按照我已经假设的那样,将按每个AppDomain生成的名称调用Application_Start(再次假定这将在服务器上有多少个内核附近)。
So, what would be an effective way of maintaining a set of persistent connections to a server, that survive the request/response phase, and if possible, are shared between all AppDomains that the IIS server has created? 那么,什么是维护与服务器的一组持久连接,在请求/响应阶段中生存并在IIS服务器已创建的所有AppDomain之间共享的有效方法?
It might help to mention that this web service is only going to be consumed by another web site. 可能需要提及的是,该Web服务仅将由另一个网站使用。 It is essentially an Authentication Proxy server, however, in the future, it is going to do a lot more. 它本质上是一个身份验证代理服务器,但是在将来,它将做更多的事情。 Therefore, I can't just cache the response, as future requests will be required, and reauthenticating is not an option. 因此,我不能仅仅缓存响应,因为将来的请求将是必需的,并且重新认证不是一种选择。
If you want to survive AppDomain restarts and share state between multiple ASP.NET applications you will have to go out of the IIS process and store this in a central location that is accessible from all applications. 如果要生存下来,AppDomain将重新启动并在多个ASP.NET应用程序之间共享状态,则必须退出IIS进程,并将其存储在所有应用程序都可以访问的中央位置。 A database is a good candidate. 数据库是一个不错的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.