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
随机推荐
- AS3在函数内部移除监听(arguments.callee)
scene.addEventListener(Event.ADDED_TO_STAGE, function():void { scene.removeEventListener(Event.ADDED ...
- 纯HTML自动刷新页面或重定向(http-equiv属性的refresh)
refresh 属性值 -- 刷新与跳转(重定向)页面 refresh出现在http-equiv属性中,使用content属性表示刷新或跳转的开始时间与跳转的网址 refresh示例一:5秒之后刷 ...
- 【巷子】:关于Apply、call、bind的详解
call方法: 语法:call(thisObj,'',''........) 定义:调用一个对象的一个方法,以另一个对象替换当前对象 说明:call方法可以用来代替另一个对象调用一个方法.call方法 ...
- ibatis 中#和 $ 符号的区别
1.数据类型匹配 #:会进行预编译,而且进行类型匹配(自动确定数据类型): $:不进行数据类型匹配. 2.实现方式: # 用于变量替换(先生成一个占位符,然后替换) select * from use ...
- Ubbeditor的使用
简单介绍: 作为一种放在客户端文本编辑器,此时不能支持将JS.Html代码直接发送给服务器,这样将会给服务器带来极大的危险,比如UMEditor(富文本编辑器),它的使用需要关闭服务器端的代码检查的, ...
- Gitlab邮箱配置
一些动作会自动发邮件,比如合并代码等 未验证
- Gitlab安装和使用
GitLab是一个利用 Ruby on Rails 开发的开源应用程序,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人项目. GitLab拥有与Github类似 ...
- java-基础-【三】try/catch/finally
原文地址: https://my.oschina.net/bieber/blog/703251 一.单层的try/catch public int test(int a,int b){ try{ re ...
- JAVA优化技巧分享 让游戏更加的流畅
我的世界怎么样可以玩的更加流畅呢?怎么对游戏进行优化呢?相信很多小伙伴都很想知道吧,今天小编为大家带来的是我的世界游戏优化技巧,喜欢的小伙伴不要错 ... 在很多时候如果电脑配置过低的话,玩游戏并不流 ...
- [css]网站骨架布局作业
实现效果图: 代码实现 第一次写的时候不知道如何下手, 后来摸清规律了,由大到小. 由全局到局部 第一次还遇到区块命名问题, 和哪个该怎么划分问题 第一次还遇到由于划分不规整,所以有些代码没达到预期的 ...