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

  1. go官方的http.request + context样例

    go官方的http.request + context样例 https://github.com/DavadDi/go_study/blob/master/src/httpreq_context/ma ...

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

  3. 遭遇ASP.NET的Request is not available in this context

    如果ASP.NET程序以IIS集成模式运行,在Global.asax的Application_Start()中,只要访问Context.Request,比如下面的代码 var request = Co ...

  4. spring配置文件引入properties文件:<context:property-placeholder>标签使用总结

    一.问题描述: 1.有些参数在某些阶段中是常量,比如: (1)在开发阶段我们连接数据库时的连接url.username.password.driverClass等 (2)分布式应用中client端访问 ...

  5. Spring 梳理-使用<context:property-placeholder>标签导入多个properties文件

    使用<context:property-placeholder>标签导入多个properties文件 2017年12月20日 10:10:36 sf_climber 阅读数:5830更多 ...

  6. Asp.Net Mvc4 Webapi Request获取参数

    最近用mvc4中的WEBAPI,发现接收参数不是很方便,跟传统的request.querystring和request.form有很大区别,在网上搜了一大圈,各种方案都有,但不是太详细,于是跟踪Act ...

  7. HttpRequestMessage

    mvc4中的WEBAPI,发现接收参数不是很方便,跟传统的request.querystring和request.form有很大区别,在网上搜了一大圈,各种方案都有,但不是太详细,于是跟踪Action ...

  8. django 模板context的理解

    context作为view与template之间的桥梁,理解它的工作原理对于djagno的模板工作机制至关重要. class ContextDict(dict):#上下文词典,由词典可以通过conte ...

  9. 【转】在SpringMVC Controller中注入Request成员域

    原文链接:https://www.cnblogs.com/abcwt112/p/7777258.html 原文作者:abcwt112 主题 在工作中遇到1个问题....我们定义了一个Controlle ...

随机推荐

  1. 教你上传代码到码云(与github一样)

    以下所有操作都在命令行进行 1 git 配置 git config --global user.name “用户名” git config --global user.email “邮箱”2 生成公钥 ...

  2. JavaScript权威指南--正则表达式

    知识要点 正则表达式,是一个描述字符模式的对象.javascript的RegExp类表示正则表达式,String和RegExp都定义了方法,后者使用正则表达式进行强大的模式匹配和文本检索与替换功能. ...

  3. Eclipse开发Android应用 找不到平板

    1.驱动安装正确2.平板的连接方式正确,不要用大容量存储/sd卡模式这个设置在4.3上很难找呀.设置->存储->点右上角的菜单 3.打开USB调试.4.上述问题都检查后,在eclipse里 ...

  4. JSP 文件上传

    JSP 文件上传 JSP可以通过HTML的form表单上传文件到服务器. 文件类型可以是文本文件.二进制文件.图像文件等其他任何文档. 创建文件上传表单 接下来我们使用HTML标签来创建文件上传表单, ...

  5. Autofac Mvc注入

    private void DependencyInjection() { var builder = new ContainerBuilder(); builder.RegisterControlle ...

  6. Idea2018激活

    [help]-->[register]-->[license server]-->输入下方链接 http://xdouble.cn:8888/ 如果不行,请用下面的这个: http: ...

  7. python学习笔记(一)---python下载以及环境的安装

    转载网址:https://www.runoob.com/python/python-install.html 1.下载python安装包: 安装包下载网址(如下图所在的网址):https://www. ...

  8. RabbitMQ的几种工作模式

    maven: <dependencies> <!-- RabbitMQ的客户端 --> <dependency> <groupId>com.rabbit ...

  9. 利用DotNetZip服务端压缩文件并下载

    public void DownFile() {              string filePath = Server.MapPath("/Files/txt/bb.txt" ...

  10. 从JDK源码角度看Short

    概况 Java的Short类主要的作用就是对基本类型short进行封装,提供了一些处理short类型的方法,比如short到String类型的转换方法或String类型到short类型的转换方法,当然 ...