Data caching per request in Owin application
Data caching per request in Owin application
解答1
Finally I found OwinRequestScopeContext. Very simple to use.
In the Startup class:
app.UseRequestScopeContext(); //依赖于NuGet上的这个package https://www.nuget.org/packages/OwinRequestScopeContext/
Then I can add per request cache like this:
OwinRequestScopeContext.Current.Items["myclient"] = new Client();
Then anywhere in my code I can do (just like HttpContext.Current):
var currentClient = OwinRequestScopeContext.Current.Items["myclient"] as Client;
Here is the source code if you're curious. It uses CallContext.LogicalGetData and LogicalSetData. Does any one see any problem with this approach of caching request data?
源码https://github.com/neuecc/OwinRequestScopeContext/
需要注意app.Use的时机,需要放在webapi之前,否则current会是空
app.UseAutofacMiddleware(container);
app.UseRequestScopeContext();
app.UseAutofacWebApi(config);
app.UseWebApi(config);
关于原理部分另外一个https://github.com/DavidLievrouw/OwinRequestScopeContext 中提到 http://odetocode.com/Articles/112.aspx
解答2
ou just need to use OwinContext for this:
From your middleware:
public class HelloWorldMiddleware : OwinMiddleware
{
public HelloWorldMiddleware (OwinMiddleware next) : base(next) { }
public override async Task Invoke(IOwinContext context)
{
context.Set("Hello", "World");
await Next.Invoke(context);
}
}
From MVC or WebApi:
Request.GetOwinContext().Get<string>("Hello");
Should I use OwinContext's Environment to hold application specific data per request
OWIN environment dictionary can be used to store per-request data. Properties collection of the request object can be used to do the same.
The main difference is OWIN environment dictionary is an OWIN concept and is applicable to any middleware running in a OWIN host. Properties collection of the request object is an ASP.NET Web API concept and is applicable only to that specific framework.
BTW, ASP.NET Web API itself runs as a middleware in OWIN pipeline. So, to answer your question, you cannot access the request properties collection of Web API from your middleware because it is applicable only to Web API middleware (or that specific framework).
If you want to write your cross-cutting concern stuff as OWIN middleware you have to use OWIN environment dictionary. If Web API extension points like a filter or a message handler is okay, then you can use the properties collection.
Obviously, anything you write leveraging Web API extension points is applicable only to Web API whereas OWIN middleware is applicable to any kind of app running in OWIN pipeline and that includes Web API.
这个回答下,有人提到了上一个链接中的解答1
Data caching per request in Owin application的更多相关文章
- Failed to retrieve data for this request. (Microsoft.SqlServer.Management.Sdk.Sfc)
使用Microsoft SQL SERVER 2014 Management Studio访问Azure SQL Database时,查看存储过程时遇到下面错误信息: TITLE: Microsoft ...
- JSP最常用的五种内置对象(out,request,response,session,application)
为了简化开发过程,JSP提供了一些内置对象,它们由容器实现和管理.开发者在JSP页面中无需声明,无需实例化就可使用.主要有out,request,response,session,applicatio ...
- Struts2中的数据处理的三种方式对比(Action中三种作用域request,session,application对象)
1:在Action中如何获得作用域(request,session,application)对象: 取得Map(键值对映射集)类型的requet,session,application; 对数据操作的 ...
- 处理flutter http请求添加application/json报错Cannot set the body fields of a Request with content-type “application/json”
在flutter中在http请求发送时设置"content-type": "application/json"会出现报错Cannot set the body ...
- JSP 内置对象(request response session application out pageContext)
request对象 javax.servlet.http.HttpServletRequest接口的实例 request.setCharacterEncoding("utf-8" ...
- #3 working with data stored in files && securing your application (PART II)
Security problems is more and more important on the internet today. You can see the problems . This ...
- #3 working with data stored in files && securing your application
This chapter reveals that you can use files and databases together to build PHP application that waa ...
- [AngualrJS] Using Angular-Cache for caching http request
Check the website: https://jmdobry.github.io/angular-cache/#using-angular-cache-with-http Install: n ...
- JSP内置对象--pageContent,request,response,session,application,config,out,page,exception
随机推荐
- 【APIO2016】Fireworks[DP 可并堆维护凸包优化]
4585: [Apio2016]烟火表演 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 100 Solved: 66[Submit][Status] ...
- hdu 5318 The Goddess Of The Moon
The Goddess Of The Moon Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- 【BZOJ3529】[Sdoi2014]数表 莫比乌斯反演+树状数组
[BZOJ3529][Sdoi2014]数表 Description 有一张N×m的数表,其第i行第j列(1 < =i < =礼,1 < =j < =m)的数值为能同时整除i和 ...
- java 中的String类型数据添加双引号
转义符 \ 加上引号 \" <?xml version="1.0"encoding="GBK"?> String temp = &qu ...
- windows下resin的配置部署与调试
配置 从Resin官网(http://www.caucho.com)下载Resin解压后,启动Resin,运行resin根目录下的resin.exe文件,运行期间将出现下图所示的命令提示符窗口. 表示 ...
- java设计模式----外观模式(门面模式)
外观模式主要应用场景在于为复杂的子系统提供一个简单的接口,提高子系统的独立性. 创建DrawerOne类: package facade; public class DrawerOne { publi ...
- 有关velocity的资料(等待整理)
proxy-target-class="true" 与proxy-target-class="false"的区别: proxy-target-class属性值决 ...
- rest_framework之版本详解 04
访问不同版本,给不同内容. rest规范:版本要么放url上:要么放请求头里. 1.原来的url的版本都写死了.只能写v1 from django.conf.urls import url from ...
- Oracle命令(三):Oracle用户
1.显示当前用户名 select user from dual; show user 2.显示当然用户有哪些表 select * from tab; 3.显示当所有用户的表 select * from ...
- 锁、volatile、CAS 比较
一.锁的劣势 (1) 在JDK1.5之前都是使用synchronized关键字保证同步的,这种通过使用一致的锁定协议来协调对共享状态的访问,可以确保无论哪个线程持有守 护变量的锁,都采用独占的方式来访 ...