Carrying per-request context using the HttpRequestMessage.Properties
In a Web API application, I use Castle Windsor to supply services configured with PerWebRequest lifetime and everything works fine on IIS.
However, when I use the ASP.NET Web API Self Host (Beta) package I need to create a custom lifetime in order to scope those services per HTTP request.
I'd suggest you using a message handler to set some your object into HttpRequestMessage.Property:
public class MyApplication : HttpApplication
{
protected void Application_Start()
{
RegisterHttpMessageHandlers(GlobalConfiguration.Configuration);
}
public void RegisterHttpMessageHandlers(HttpConfiguration config)
{
config.MessageHandlers.Add(new MyMessageHandler());
}
} public static class MyHttpMessageHandlerExtensions
{
public static class HttpPropertyKey
{
public static readonly string MyProperty = "MyCompany_MyProperty";
} public static MyContext GetContext(this HttpRequestMessage request)
{
return (MyContext)request.Properties[HttpPropertyKey.MyProperty ];
} public static void SetContext(this HttpRequestMessage request, MyContext ctx)
{
request.Properties[HttpPropertyKey.MyProperty] = ctx;
}
}
public class MyMessageHandler : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.SetContext(new MyContext(){/*some your data*/});
return base.SendAsync(request, cancellationToken);
}
} public class MyController: ApiController
{
public object GetData()
{
MyContext ctx = this.Request.GetContext(); // the extenstion method is used
}
}
Carrying per-request context using the HttpRequestMessage.Properties的更多相关文章
- go官方的http.request + context样例
go官方的http.request + context样例 https://github.com/DavadDi/go_study/blob/master/src/httpreq_context/ma ...
- How do I set the timeout for a JAX-WS webservice client?
How do I set the timeout for a JAX-WS webservice client? up vote58down votefavorite 27 I've used JAX ...
- 遭遇ASP.NET的Request is not available in this context
如果ASP.NET程序以IIS集成模式运行,在Global.asax的Application_Start()中,只要访问Context.Request,比如下面的代码 var request = Co ...
- spring配置文件引入properties文件:<context:property-placeholder>标签使用总结
一.问题描述: 1.有些参数在某些阶段中是常量,比如: (1)在开发阶段我们连接数据库时的连接url.username.password.driverClass等 (2)分布式应用中client端访问 ...
- Spring 梳理-使用<context:property-placeholder>标签导入多个properties文件
使用<context:property-placeholder>标签导入多个properties文件 2017年12月20日 10:10:36 sf_climber 阅读数:5830更多 ...
- Asp.Net Mvc4 Webapi Request获取参数
最近用mvc4中的WEBAPI,发现接收参数不是很方便,跟传统的request.querystring和request.form有很大区别,在网上搜了一大圈,各种方案都有,但不是太详细,于是跟踪Act ...
- HttpRequestMessage
mvc4中的WEBAPI,发现接收参数不是很方便,跟传统的request.querystring和request.form有很大区别,在网上搜了一大圈,各种方案都有,但不是太详细,于是跟踪Action ...
- django 模板context的理解
context作为view与template之间的桥梁,理解它的工作原理对于djagno的模板工作机制至关重要. class ContextDict(dict):#上下文词典,由词典可以通过conte ...
- 【转】在SpringMVC Controller中注入Request成员域
原文链接:https://www.cnblogs.com/abcwt112/p/7777258.html 原文作者:abcwt112 主题 在工作中遇到1个问题....我们定义了一个Controlle ...
随机推荐
- python判断key是否存在
d = {: , : , : , : , : , : } def is_key_present(x): if x in d: print('Key is present in the dictiona ...
- h.264_javascript_资料
1. 用ffmpeg制作推流工具,实现推流系统声音和桌面到rtmp服务器-CSDN论坛-CSDN.NET-中国最大的IT技术社区.html http://bbs.csdn.net/topics/392 ...
- C# 使用Dictionary、linq实现根据集合里面的字符串进行分组
//对下面集合里面的字符串按照“_”进行分组. List<string> list = new List<string>() { "1_32", " ...
- UVA-10806 Dijkstra, Dijkstra. (最小费用流,网络流建模)
题目大意:给一张带权简单图,找出一条经过起点s和终点t的最小回路. 题目分析:建立网络,以s为源点,t为汇点,另每条边的容量为1,单位费用为边权值.求最小费用流,增广两次后的最小费用便是答案. 代码如 ...
- 高级浏览器-SRWare Iron 29.0.1600.0 版本发布
SRWare Iron是德国一安全公司srware改造的Chrome(铬)命名为铁(iron)的浏览器.于2008年9月18日首次发布. 据官方介绍,Iron浏览器砍掉了Chromium原程序中的很多 ...
- java静态代理和动态代理(一)
代理Proxy: Proxy代理模式是一种结构型设计模式,主要解决的问题是:在直接访问对象时带来的问题. 代理是一种常用的设计模式,其目的就是为其他对象提供一个代理以控制对某个对象的访问.代理类负责为 ...
- scikit-learn 学习笔记-- Generalized Linear Models (一)
scikit-learn 是非常优秀的一个有关机器学习的 Python Lib,包含了除深度学习之外的传统机器学习的绝大多数算法,对于了解传统机器学习是一个很不错的平台.每个算法都有相应的例子,既可以 ...
- HTML save data to CSV or excel
/********************************************************************************* * HTML save data ...
- Android GPS GPSBasics project hacking
一.参考源码: GPS Basic Example - Android Example http://androidexample.com/GPS_Basic__-__Android_Example/ ...
- Nginx 设置rewrite规则是遇到的一个{}大括号引发的报错问题
一个群友提到: 用nginx image_filter模块裁图,用!拼宽高能够实现,现在想用参数传宽高总是报错,配置如下: location ~ ^/images/.* { if ( $q ...