1. public static IAppBuilder UseAutofacMiddleware(this IAppBuilder app, ILifetimeScope container)
  2. {
  3. if (app == null)
  4. {
  5. throw new ArgumentNullException("app");
  6. }
  7.  
  8. if (container == null)
  9. {
  10. throw new ArgumentNullException("container");
  11. }
  12.  
  13. return app
  14. .RegisterAutofacLifetimeScopeInjector(container)
  15. .UseAllMiddlewareRegisteredInContainer(container);
  16. }
  1. private static IAppBuilder RegisterAutofacLifetimeScopeInjector(this IAppBuilder app, ILifetimeScope container)
  2. {
  3. app.Use(async (context, next) =>
  4. {
  5. using (var lifetimeScope = container.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag,
  6. b => b.RegisterInstance(context).As<IOwinContext>()))
  7. {
  8. context.Set(Constants.OwinLifetimeScopeKey, lifetimeScope);
  9. await next();
  10. }
  11. });
  12.  
  13. app.Properties[InjectorRegisteredKey] = true;
  14. return app;
  15. }
  1. using System;
  2. using Microsoft.Owin;
  3.  
  4. namespace Autofac.Integration.Owin
  5. {
  6. /// <summary>
  7. /// Extension methods for using Autofac within an OWIN context.
  8. /// </summary>
  9. public static class OwinContextExtensions
  10. {
  11. /// <summary>
  12. /// Gets the current Autofac lifetime scope from the OWIN context.
  13. /// </summary>
  14. /// <param name="context">The OWIN context.</param>
  15. /// <returns>The current lifetime scope.</returns>
  16. /// <exception cref="System.ArgumentNullException">
  17. /// Thrown if <paramref name="context" /> is <see langword="null" />.
  18. /// </exception>
  19. public static ILifetimeScope GetAutofacLifetimeScope(this IOwinContext context)
  20. {
  21. if (context == null)
  22. {
  23. throw new ArgumentNullException("context");
  24. }
  25.  
  26. return context.Get<ILifetimeScope>(Constants.OwinLifetimeScopeKey);
  27. }
  28. }
  29. }

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. JSP 中的 tag 文件

    在jsp文件中,可以引用 tag 和tld 文件,本文主要针对 tag 对于tag 文件 1)将此类文件放在 WEB-INF 下,比如 /WEB-INF/tags,tags 是目录,其下可以有多个.t ...

  2. java 读取数据库中表定义

    将数据库中的表信息读取出来 package com.cloud.smartreport.utils; import java.sql.Connection; import java.sql.Datab ...

  3. 数据库开发基础-SQl Server 变量、运算符、if、while

    变量: SQL语言也跟其他编程语言一样,拥有变量.分支.循环等控制语句. 在SQL语言里面把变量分为局部变量和全局变量,全局变量又称系统变量. 局部变量: 使用declare关键字给变量声明,语法非常 ...

  4. oracle 10g 安装时字符集的选择,和后边的修改

    先用system和密码登陆SQLPLUS,然后: 1.用sys登录到oracle中 connect sys/密码 as sysdba; (能直接以sys登陆到sqlplus吗?)2.开始修改编码 sh ...

  5. css-关于absolute和relative的一些笔记

    根据张鑫旭老师的博客,一些笔记留作备忘 1.position:absolute 具有包裹性--包裹性换种说法就是让元素inline-block化,例如一个div标签默认宽度是100%显示的,但是一旦被 ...

  6. MapReduce基础知识

    hadoop版本:1.1.2 一.Mapper类的结构 Mapper类是Job.setInputFormatClass()方法的默认值,Mapper类将输入的键值对原封不动地输出. org.apach ...

  7. 【BZOJ-1912】patrol巡逻 树的直径 + DFS(树形DP)

    1912: [Apio2010]patrol 巡逻 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 1034  Solved: 562[Submit][St ...

  8. 【BZOJ-4519】不同的最小割 最小割树(分治+最小割)

    4519: [Cqoi2016]不同的最小割 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 393  Solved: 239[Submit][Stat ...

  9. Birthday-24

    2013 LEXUS花样滑冰 和母亲在一起,生日快乐!

  10. Linux Overflow Vulnerability General Hardened Defense Technology、Grsecurity/PaX

    Catalog . Linux attack vector . Grsecurity/PaX . Hardened toolchain . Default addition of the Stack ...