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
随机推荐
- Android 生成分享长图并且添加全图水印
转载自 : http://blog.csdn.net/gengqiquan/article/details/65938021 领导最近觉得携程的截屏生成长图分享效果比较好,所以我们也加了个:产品觉得分 ...
- WEB安全第四篇--与数据库的亲密接触:SQL注入攻击
零.前言 最近做专心web安全有一段时间了,但是目测后面的活会有些复杂,涉及到更多的中间件.底层安全.漏洞研究与安全建设等越来越复杂的东东,所以在这里想写一个系列关于web安全基础以及一些讨巧的pay ...
- 【BZOJ2768】[JLOI2010]冠军调查/【BZOJ1934】[Shoi2007]Vote 善意的投票 最小割
[BZOJ2768][JLOI2010]冠军调查 Description 一年一度的欧洲足球冠军联赛已经进入了淘汰赛阶段.随着卫冕冠军巴萨罗那的淘汰,英超劲旅切尔西成为了头号热门.新浪体育最近在吉林教 ...
- 【BZOJ1500】[NOI2005]维修数列 Splay
[BZOJ1500][NOI2005]维修数列 Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目.第2行 ...
- 监控linux流量shell版
想要实时查看linux流量情况,又不想再去下第三方工具,可以直接写脚步运行! 系统:centos 6.5 原理:从/proc/net/dev中获取到流量情况,再通过换算并除以间隔时间来得到流量单位M ...
- Linux下安装和卸载jdk步骤详述
安装jdk 1.下载jdk8 jdk下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-21331 ...
- 解决IE7下scroll的bug
IE7下scroll滚动问题 没法撑开 ie7下没办法撑开div; 即是设置了overflow-y:scroll; 解决版本:给设置scroll的容器加上position:relative
- Android N 通知概览及example
概述 Android App的通知在维护你的App和用户之间的交互起着举足轻重的作用,为了提供更好的用户体验,Android N上的通知提供了可视化刷新,自定义视图和直接回复等功能.另外还提出了Mes ...
- Python开发【笔记】:git&github 快速入门
github入门 简介: 很多人都知道,Linus在1991年创建了开源的Linux,从此,Linux系统不断发展,已经成为最大的服务器系统软件了. Linus虽然创建了Linux,但Linux的壮大 ...
- mysql 取当前日期对应的周一或周日
select subdate(curdate(),date_format(curdate(),'%w')-1)//获取当前日期在本周的周一 select subdate(curdate(),date_ ...