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 ...
随机推荐
- install ros-indigo-tf
sudo apt-get install ros-indigo-tf
- Linux常用命令--网终设置
1.把自己(sa)添加到sudoers配置文件中,以便于获取权限 vim /etc/sudoers 编辑文件(部分centOS版本没有vim命令,则用vi即可) 找到[root ALL=(ALL) A ...
- 10个有趣的Javascript和CSS库
Tailwind CSS Tailwind是用于构建自定义用户界面的实用CSS框架. 每个Tailwind小应用都有多种尺寸,这使得创建响应式界面变得非常简单. 您可以自定义颜色,边框尺寸,字体,阴影 ...
- 基于Oracle的SQL优化(崔华著)-整理笔记-工具集
一.脚本display_cursor_9i.sql是可以得到SQL的真实执行计划,使用示例 使用示例,请看以下case 1.执行测试sql: SELECT T1.*,T2.* FROM T_0504 ...
- 苹果iPhone 5C和5S发布后,消费者如何选择?
9月11日凌晨苹果新品发布会,笔者的朋友圈居然没有看直播的,真果粉太少了.笔者来阐述一些容易忽略的东西. iPhone5C和5S与5有什么不一样? 新品iPhone 5S 外观与iPhone5 相似度 ...
- css中用#id.class的形式定义样式,为什么这样用,不直接写成.class.代码如下:#skin_0.selected{}这种的
<ul class="skin"> <li id="skin_0" title="蓝色" class="sele ...
- New Concept English Two 19 49
$课文47 嗜酒的鬼魂 481. A public house which was recently bought by Mr.Ian Thompson is up for sale. 伊恩.汤普森先 ...
- timer Compliant Controller project (2)--Project Demonstration
1software flow diagram As we know, Embedded design is the core of Electronic Product Design. Di ...
- LNMP架构下的nginx、mysql、php的源码安装
一.LNMP的介绍 LNMP就是Linux+Nginx+Mysql+Php这种网站服务架构.Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统,常见版本有:centos.ubun ...
- antd中form自定义rules
1.使用getFieldDecorator <FormItem label="手机号" > {getFieldDecorator('phone', { initialV ...