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. LeetCode——Delete Node in a Linked List

    Description: Write a function to delete a node (except the tail) in a singly linked list, given only ...

  2. C#如何判断程序调用的exe已结束

    二个方法:以运行系统记事本为例 方法一:这种方法会阻塞当前进程,直到运行的外部程序退出System.Diagnostics.Process exep = System.Diagnostics.Proc ...

  3. centos7 安装后,出现Please make your choice from above ['q' to quit | 'c' to continue | 'r' to refresh]

    PS:出现以上信息,是要求你阅读或者接收协议: Initial setup of CentOS Linux 7 (core)解决步骤如下: 1,输入[1],按Enter键阅读许可协议,2,输入[2], ...

  4. 关于string的length

    在C++里面,std::string的length()返回的是字节数,与编码方式有关. int main() { std::string s = "我是中国人"; std::cou ...

  5. HTTP cookies 详解(国外一位大牛的文章)

    原文:http://blog.csdn.net/lijing198997/article/details/9378047 HTTP cookies,通常又称作"cookies",已 ...

  6. OC最基础的系统转场动画

    SystemAnimationViewController *system = [SystemAnimationViewController new]; CATransition *animation ...

  7. iOS邮箱、手机号等常用验证功能 判断字符串是否int float

    手机号验证 /* 手机号码 13[0-9],14[5|7|9],15[0-3],15[5-9],17[0|1|3|5|6|8],18[0-9] 移动:134[0-8],13[5-9],147,15[0 ...

  8. Django REST framework 之JWT认证

    Json Web Token 1.JWT简介 JWT 是一个开放标准(RFC 7519),它定义了一种用于简洁,自包含的用于通信双方之间以 JSON 对象的形式安全传递信息的方法.JWT 可以使用 H ...

  9. 2006 - MySQL server has gone away

    mysql出现ERROR : (2006, 'MySQL server has gone away') 的问题意思就是指client和MySQL server之间的链接断开了. 造成这样的原因一般是s ...

  10. Deep Learning(1)

    深度学习是机器学习研究中的一个新的领域,其动机在于建立.模拟人脑进行分析学习的神经网络,它模仿人脑的机制来解释数据,例如图像,声音和文本.深度学习是无监督学习的一种. 深度学习的概念源于人工神经网络的 ...