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. 【jdk源码学习】HashMap

    package com.emsn.crazyjdk.java.util; /** * “人”类,重写了equals和hashcode方法...,以id来区分不同的人,你懂的... * * @autho ...

  2. console输出图案

    探索天猫控制台下的图案是怎么制作的 通过它的源码找到以下代码(还原解压代码) 自己也照葫画瓢搞了个以前公司的logo

  3. 【BZOJ4452】[Cerc2015]Export Estimate 并查集

    [BZOJ4452][Cerc2015]Export Estimate Description 给你一个n个点m条边的无向图,每条边有权值,我们可以选择一个整数lim来生成一个新的图,过程如下:  1 ...

  4. AVG

    AVG([ DISTINCT | ALL ] expr) [ OVER(analytic_clause) ] SELECT MANAGER_ID,           LAST_NAME,       ...

  5. 删除 Linux /tmp 目录下的临时文件

    使用 tmpreaper How to install and use tmpreaper How to Remove Files Older than N days using tmpreaper ...

  6. 04.ActiveMQ与Spring JMS整合

        SpringJMS使用参考:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jms.html ...

  7. spring-boot集成redis

    application.properties #redis 配置 # Redis数据库索引(默认为0) spring.redis.database=0 # Redis服务器地址 spring.redi ...

  8. #pragma once含义及用法

    #pragma once是一个比较常用的C/C++杂注,只要在头文件的最开始加入这条杂注,就能够保证头文件只被编译一次. #pragma once是编译器相关的,有的编译器支持,有的编译器不支持,具体 ...

  9. Python并行编程(十二):进程同步

    1.基本概念 多个进程可以协同工作来完成一项任务,通常需要共享数据.所以在多进程之间保持数据的一致性就很重要,需要共享数据协同的进程必须以适当的策略来读写数据.同步原语和线程的库类似. - Lock: ...

  10. 【Loadrunner】通过loadrunner录制时候有事件但是白页无法出来登录页怎么办?

    loadrunner录制脚本时候有事件但是一直白页怎么办? 解决办法:依次进行下方1.2.方法操作,如果还不行再进行3的操作. 1.勾选下图IE浏览器的Internet选项中“启用第三方浏览器拓展*” ...