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
随机推荐
- 在腾讯云服务器上体验Docker
版权声明:本文由姚俊刚原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/85 来源:腾云阁 https://www.qclou ...
- jstl标签怎么实现分页中下一页
<script type="text/javascript"> //分页按钮处理 function goPageAction(page ...
- tomcat日志 之 catalina.log & localhost.log
体会 catalina.out catalina.log 是tomcat的标准输出(stdout)和标准出错(stderr) cataliana.{yyyy-MM-dd}.log和localhost. ...
- Egret3D初步学习笔记四 (地形使用)
一 导出地形 Skinedmesh没反应.得选择导出scene. 二 直接报错 三 修改错误 选择关闭程序后,仍然可以导出完成. 由于地图的lightmap.exr没法解析报错. 在获得MapCon ...
- Windows 2008 server R2安装.NET Framework4时提示“灾难性故障”
报错信息: 安装.NET Framework 4时,提示安装未成功,“灾难性故障”.服务器的操作系统是windows server 2008 R2. 查看系统日志时显示“无法安装 Windows 更 ...
- 【UOJ274】【清华集训2016】温暖会指引我们前行 LCT
[UOJ274][清华集训2016]温暖会指引我们前行 任务描述 虽然小R住的宿舍楼早已来了暖气,但是由于某些原因,宿舍楼中的某些窗户仍然开着(例如厕所的窗户),这就使得宿舍楼中有一些路上的温度还是很 ...
- 微信小程序 --- 事件绑定
事件类别: tap:点击事件: longtap:长按事件: touchstart:触摸开始: touchend:触摸结束: touchcansce:取消触摸: 事件绑定: bind绑定: catch绑 ...
- MUI窗口管理
参考:窗口管理 http://dev.dcloud.net.cn/mui/window/ 页面初始化:在app开发中,若要使用HTML5+扩展api,必须等plusready事件发生后才能正常使用,m ...
- 摄像头的管理(camera) ---- HTML5+
模块:camera Camera模块管理设备的摄像头,可用于拍照.摄像操作,通过plus.camera获取摄像头管理对象. 应用场景:保存自拍,保存照片,上传照片,保存视频,上传视频: 通过之前的模块 ...
- 微软官方:SELECT语句逻辑处理顺序
以下步骤显示SELECT 语句的逻辑处理顺序或绑定顺序.此顺序确定在一个步骤中定义的对象何时可用于后续步骤中的子句. 例如,如果查询处理器可以绑定到(访问)在FROM 子句中定义的表或视图,则这些对象 ...