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. OPRNGL总结(一)OPENGL的理论原理

    1.计算机图形学的发展——走向3D 1.纸带 2.荧光屏,打印出*等等 3.阴极射线管 4实时3D 2.实现“3D”的原理 1.其实现在看到的3D都是伪3D,并不是真正的三维图像,真正的三维图像 真3 ...

  2. jsp使用servlet实现验证码

    在进行表单设计中,验证码的增加恰恰可以实现是否为“人为”操作,增加验证码可以防止网站数据库信息的冗杂等... 现在,小编将讲述通过servlet实现验证码: 验证码作为一个图片,在页面中为“画”出来的 ...

  3. [NOIP2012] 提高组 洛谷P1080 国王游戏

    题目描述 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右 手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排 成一排,国王站在队伍 ...

  4. MongoDB: 数据库复制

    db.copyDatabase("from","to","127.0.0.1:16161"); 将127.0.0.1上的from库.拷贝到t ...

  5. 批处理(.bat)For命令使用

    for命令使用 @echo off REM 打印当前目录里所有子目录的名称 REM for /d %%i in (*) do @echo %%i REM 打印当前目录里所有名字为3个字符的子目录的名称 ...

  6. select 1 from dual 中的1表示的含义

    select 1 from dual   在这条sql语句中的1代表什么意思?查出来是个什么结果?   其实: select 1 from table; select anycol(目的表集合中的任意 ...

  7. 构建第一个maven工程

    一.maven介绍 Maven 是一个强大的项目管理和构建自动化工具,它通过抽象的项目对象模型和构建生命周期模型来对项目及其构建过程进行管理,Maven 最大化的消除了构建的重复,提升了构建的效率与标 ...

  8. [JavaEE]如何唤醒Sleep中的线程

    主线程调用子线程的interrupt()方法,导致子线程抛出InterruptedException, 在子线程中catch这个Exception,不做任何事即可从Sleep状态唤醒线程,继续执行. ...

  9. Appium for IOS testing on Mac

    一:环境 1.Mac OS X 10.9.1 2.Xcod 5.0.2 3.Appium 1.3.6 下载地址:https://bitbucket.org/appium/appium.app/down ...

  10. PhyLab2.0设计分析阶段任务大纲(α)

    任务概述 由于接手软剑攻城队的PhyLab项目,省去了用户需求分析.团队编码规范.用户界面原型设计和后端逻辑设计的大部分环节,因此前期的主要任务落在了用户使用反馈.功能优化增改方向.用户体验优化以及源 ...