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的更多相关文章

  1. Failed to retrieve data for this request. (Microsoft.SqlServer.Management.Sdk.Sfc)

    使用Microsoft SQL SERVER 2014 Management Studio访问Azure SQL Database时,查看存储过程时遇到下面错误信息: TITLE: Microsoft ...

  2. JSP最常用的五种内置对象(out,request,response,session,application)

    为了简化开发过程,JSP提供了一些内置对象,它们由容器实现和管理.开发者在JSP页面中无需声明,无需实例化就可使用.主要有out,request,response,session,applicatio ...

  3. Struts2中的数据处理的三种方式对比(Action中三种作用域request,session,application对象)

    1:在Action中如何获得作用域(request,session,application)对象: 取得Map(键值对映射集)类型的requet,session,application; 对数据操作的 ...

  4. 处理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 ...

  5. JSP 内置对象(request response session application out pageContext)

    request对象  javax.servlet.http.HttpServletRequest接口的实例 request.setCharacterEncoding("utf-8" ...

  6. #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 ...

  7. #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 ...

  8. [AngualrJS] Using Angular-Cache for caching http request

    Check the website: https://jmdobry.github.io/angular-cache/#using-angular-cache-with-http Install: n ...

  9. JSP内置对象--pageContent,request,response,session,application,config,out,page,exception

随机推荐

  1. kubernetes创建yaml,pod服务一直处于 ContainerCreating状态的原因查找与解决

    最近刚刚入手研究kubernetes,运行容器的时候,发现一直处于ContainerCreating状态,悲了个催,刚入手就遇到了点麻烦,下面来讲讲如何查找问题及解决的 运行容器命令: kubectl ...

  2. IOS中使用轻量级数据库

    IOS中使用轻量级数据库 目录 概述 IOS中的轻量级数据库 sqlite的方法 数据库的实用操作 第三方类库 FMDatabase 概述 IOS中的轻量级数据库 sqlite的方法 sqlite3 ...

  3. 最小树形图(hdu4009)

    Transfer water Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) T ...

  4. 二叉树各种相关操作(建立二叉树、前序、中序、后序、求二叉树的深度、查找二叉树节点,层次遍历二叉树等)(C语言版)

    将二叉树相关的操作集中在一个实例里,有助于理解有关二叉树的相关操作: 1.定义树的结构体: typedef struct TreeNode{ int data; struct TreeNode *le ...

  5. sql 将一张表中的数据插入到另一张表

    将表T_wz_wz中的部分数据插入到表t_wz_kc: insert into t_wz_kc(wzid,jldwid,kcsl,yfpkcsl,cshwcbz) select wzid,jldwid ...

  6. .NET截取指定长度字符超出部分以"..."代替

    /// <summary> /// 将指定字符串按指定长度进行剪切, /// </summary> /// <param name= "Str "&g ...

  7. debug error 错误日志的调试模式

    https://docs.nginx.com/nginx/admin-guide/monitoring/logging/ error_log logs/error.log warn; In this ...

  8. os模块os.walk() 方法和os.path.join()的简单使用

    os.walk:   http://www.runoob.com/python/os-walk.html os.path.join:   https://blog.csdn.net/zmdzbzbhs ...

  9. python基础之协程函数、列表表达式、生成器表达式

    一.协程函数 协程函数的定义?如果在一个函数内部yield的使用方式是表达式形式的话,如x=yield,那么该函数称为协程函数 协程函数补充: def init(func): def wrapper( ...

  10. Qt 控制线程的顺序执行(使用QWaitCondition,并且线程类的run函数里记得加exec(),使得线程常驻)

    背景项目中用到多线程,对线程的执行顺序有要求: A.一个线程先收数据 B.一个线程处理数据 C.一个线程再将处理后的数据发送出去 要求三个线程按照ABC的顺序循环执行. 思路子类化多线程方法 重写子类 ...