每个请求数据缓存在Owin应用 [英] Data caching per request in Owin application
问题描述
在传统的ASP.NET应用程序(即使用的System.Web),我能够缓存数据
In traditional ASP.NET applications (that use System.Web), I'm able to cache data in
HttpContext.Current.Items
现在在Owin HttpContext的是不可用了。有没有办法做类似的事情在Owin - 一个静态方法/属性,通过它我可以设置/每个请求的数据得到的
Now in Owin the HttpContext is not available anymore. Is there a way to do the similar thing in Owin - a static method/property through which I can set/get per request data?
这<一个href=\"http://stackoverflow.com/questions/27368130/should-i-use-owincontexts-environment-to-hold-application-specific-data-per-req?rq=1\">question给了一些线索,但在我的情况不准确的解决方案。
This question gave some hints but not exact a solution in my case.
推荐答案
终于让我找到 OwinRequestScopeContext 。使用方法很简单:
Finally I found OwinRequestScopeContext. Very simple to use:
在启动:
app.UseRequestScopeContext();
然后我可以每个请求缓存添加这样的:
then I can add per request cache like this:
OwinRequestScopeContext.Current.Items["myclient"] = new Client();
然后在任何地方我的code我可以做(就像HttpContext.Current):
Then anywhere in my code I can do (just like HttpContext.Current):
var currentClient = OwinRequestScopeContext.Current.Items["myclient"] as Client;
<一个href=\"https://github.com/neuecc/OwinRequestScopeContext/blob/master/Source/OwinRequestScopeContext/OwinRequestScopeContext.cs\">Source这里code 的,如果your're好奇。它采用CallContext.LogicalGetData和LogicalSetData。是否有任何人看到任何问题缓存请求数据的这种做法?
Source code here if your're curious. It uses CallContext.LogicalGetData and LogicalSetData. Does any one see any problem with this approach of caching request data?
这篇关于每个请求数据缓存在Owin应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!