public static IAppBuilder UseAutofacMiddleware(this IAppBuilder app, ILifetimeScope container)
{
if (app == null)
{
throw new ArgumentNullException("app");
} if (container == null)
{
throw new ArgumentNullException("container");
} return app
.RegisterAutofacLifetimeScopeInjector(container)
.UseAllMiddlewareRegisteredInContainer(container);
}
        private static IAppBuilder RegisterAutofacLifetimeScopeInjector(this IAppBuilder app, ILifetimeScope container)
{
app.Use(async (context, next) =>
{
using (var lifetimeScope = container.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag,
b => b.RegisterInstance(context).As<IOwinContext>()))
{
context.Set(Constants.OwinLifetimeScopeKey, lifetimeScope);
await next();
}
}); app.Properties[InjectorRegisteredKey] = true;
return app;
}
using System;
using Microsoft.Owin; namespace Autofac.Integration.Owin
{
/// <summary>
/// Extension methods for using Autofac within an OWIN context.
/// </summary>
public static class OwinContextExtensions
{
/// <summary>
/// Gets the current Autofac lifetime scope from the OWIN context.
/// </summary>
/// <param name="context">The OWIN context.</param>
/// <returns>The current lifetime scope.</returns>
/// <exception cref="System.ArgumentNullException">
/// Thrown if <paramref name="context" /> is <see langword="null" />.
/// </exception>
public static ILifetimeScope GetAutofacLifetimeScope(this IOwinContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
} return context.Get<ILifetimeScope>(Constants.OwinLifetimeScopeKey);
}
}
}

Autofac.Integration.Owin的更多相关文章

  1. Autofac.Integration.Mvc.Owin分析

    using System; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Secur ...

  2. integration asp.net web api with autofac and owin

    There is an example project showing Web API in conjunction with OWIN self hosting https://github.com ...

  3. Autofac.Integration.Web分析

    using System; using System.Web; using Autofac.Core.Lifetime; namespace Autofac.Integration.Web { /// ...

  4. Autofac.Integration.Mvc分析

    Autofac.Integration.Mvc static ILifetimeScope LifetimeScope { get { return (ILifetimeScope)HttpConte ...

  5. 使用Autofac,提示重写成员“Autofac.Integration.Mvc.AutofacDependencyResolver.GetService(System.Type)”时违反了继承安全性规则。重写方法的安全可访问性必须与所重写方法的安全可访问性匹配。

    接触Autofac大概有2天左右,第2天,亲自动手搭建demo,搭完,以为大功告成的时候,提示了这个错误,网上找了很多方法,都没有解决. 为以后的朋友,避免踩坑,分享一下我的解决方法. Dmeo我是新 ...

  6. 未能加载文件或程序集“Autofac.Integration.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)

    是因为web.config中dependentAssembly结点下的版本号和当前引用的程序集的版本号不一致!

  7. autofac + owin + webform + mvc + webapi集成demo

    http://git.oschina.net/shiningrise/AutofacOwinDemo using Microsoft.Owin; using Owin; using System.We ...

  8. Autofc与Mvc,WebForm,Weiapi,Owin整合源码分析

    主要分析一下的几个项目: Autofac.Integration.Mvc Autofac.Integration.WebApi Autofac.Integration.Owin Autofac.Int ...

  9. 要引用这几个才有GetOwinContext与GetAutofacLifetimeScope

    using Owin; using Autofac; using Autofac.Integration.Owin; using System.Web; var owin = this.Request ...

随机推荐

  1. editplus-使用正则表达式替换每行首字母和尾字母

    打开EditPlus,输入多行数据,快捷键ctrl+h 打开替换窗口,选择“正则表达式”替换 行首批量添加   查找"^" 替换为“我是行首aaa” 行尾批量添加   查找&quo ...

  2. js-新窗口打开页面

    window.open(basePath+"/web/homeIndex?code="+code); 1.超链接<a href="http://www.xxx.ne ...

  3. JPA学习笔记1——JPA基础

    1.JPA简介: Java持久化规范,是从EJB2.x以前的实体Bean(Entity bean)分离出来的,EJB3以后不再有实体bean,而是将实体bean放到JPA中实现.JPA是sun提出的一 ...

  4. 由一段JS代码引发的思考

    不知道大家在编程的时候有没有遇到过这种情况,就是在循环遍历删除一部分内容的时候,发现只能删除其中一部分,而另一部分却总也删不掉,然后觉得自己的逻辑没有问题啊,于是陷入了深深的抑郁之中…… 昨天在处理一 ...

  5. PL/SQL连接Oracle客户端步骤

    首先,安装oracle,再安装plsql 打开plsql——>选择“首选项(preferences)”——>输入oracle客户端路径 如下图:

  6. [bzoj2243][SDOI2011]染色

    Description 给定一棵有$n$个节点的无根树和$m$个操作,操作有$2$类: 1.将节点$a$到节点$b$路径上所有点都染成颜色$c$; 2.询问节点$a$到节点$b$路径上的颜色段数量(连 ...

  7. 【SDOI2010题集整合】BZOJ1922~1927&1941&1951&1952&1972&1974&1975

    BZOJ1922大陆争霸 思路:带限制的单源最短路 限制每个点的条件有二,路程和最早能进入的时间,那么对两个值一起限制跑最短路,显然想要访问一个点最少满足max(dis,time) 那么每次把相连的点 ...

  8. 在DDwrt下对Firmware操作的一些技巧

    [备注]这里是对ddwrt的操作,事实上,对openwrt同样也适用. 基础知识: 1.MTD MTD是Memory Technology Devices的缩写,它主要提供了一个raw Flash设备 ...

  9. python实现自动输入命令回车操作

    苦逼的在sf上等了一天(问题链接),都没人来解答,只好自己想办法,东平西凑还是勉强实现了,记录一下: 安装完python2.7后,在cmd命令行输入python回车,后出现python相关的提示信息, ...

  10. AngularJs $interval 和 $timeout

    $interval window.setInterval的Angular包装形式.Fn是每次延迟时间后被执行的函数. 间隔函数的返回值是一个承诺.这个承诺将在每个间隔刻度被通知,并且到达规定迭代次数后 ...