Autofac.Integration.Mvc

  1. static ILifetimeScope LifetimeScope
  2. {
  3. get { return (ILifetimeScope)HttpContext.Current.Items[typeof(ILifetimeScope)]; }
  4. set { HttpContext.Current.Items[typeof(ILifetimeScope)] = value; }
  5. }
  1. namespace Autofac.Integration.Mvc
  2. {
  3. /// <summary>
  4. /// An <see cref="IHttpModule"/> and <see cref="ILifetimeScopeProvider"/> implementation
  5. /// that creates a nested lifetime scope for each HTTP request.
  6. /// </summary>
  7. internal class RequestLifetimeHttpModule : IHttpModule
  8. {
  9. /// <summary>
  10. /// Gets the lifetime scope provider that should be notified when a HTTP request ends.
  11. /// </summary>
  12. internal static ILifetimeScopeProvider LifetimeScopeProvider { get; private set; }
  13.  
  14. /// <summary>
  15. /// Initializes a module and prepares it to handle requests.
  16. /// </summary>
  17. /// <param name="context">An <see cref="T:System.Web.HttpApplication"/> that provides access to the
  18. /// methods, properties, and events common to all application objects within an ASP.NET application</param>
  19. /// <exception cref="System.ArgumentNullException">
  20. /// Thrown if <paramref name="context" /> is <see langword="null" />.
  21. /// </exception>
  22. public void Init(HttpApplication context)
  23. {
  24. if (context == null)
  25. {
  26. throw new ArgumentNullException("context");
  27. }
  28. context.EndRequest += OnEndRequest;
  29. }
  30.  
  31. /// <summary>
  32. /// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"/>.
  33. /// </summary>
  34. public void Dispose()
  35. {
  36. }
  37.  
  38. public static void SetLifetimeScopeProvider(ILifetimeScopeProvider lifetimeScopeProvider)
  39. {
  40. if (lifetimeScopeProvider == null) throw new ArgumentNullException("lifetimeScopeProvider");
  41.  
  42. LifetimeScopeProvider = lifetimeScopeProvider;
  43. }
  44.  
  45. static void OnEndRequest(object sender, EventArgs e)
  46. {
  47. if (LifetimeScopeProvider != null)
  48. LifetimeScopeProvider.EndLifetimeScope();
  49. }
  50. }
  51. }
  1. using System.ComponentModel;
  2. using Microsoft.Web.Infrastructure.DynamicModuleHelper;
  3.  
  4. namespace Autofac.Integration.Mvc
  5. {
  6. /// <summary>
  7. /// Container class for the ASP.NET application startup method.
  8. /// </summary>
  9. [EditorBrowsable(EditorBrowsableState.Never)]
  10. public static class PreApplicationStartCode
  11. {
  12. private static bool _startWasCalled;
  13.  
  14. /// <summary>
  15. /// Performs ASP.NET application startup logic early in the pipeline.
  16. /// </summary>
  17. public static void Start()
  18. {
  19. // Guard against multiple calls. All Start calls are made on the same thread, so no lock needed here.
  20. if (_startWasCalled) return;
  21.  
  22. _startWasCalled = true;
  23. DynamicModuleUtility.RegisterModule(typeof(RequestLifetimeHttpModule));
  24. }
  25. }
  26. }

Autofac.Integration.Mvc分析的更多相关文章

  1. Autofac.Integration.Mvc.Owin分析

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

  2. Autofac.Integration.Web分析

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

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

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

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

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

  5. ioc初步理解(二) 简单实用autofac搭建mvc三层+automapper=》ioc(codeFirst)

    之前在园子闲逛的时候,发现许多关于automapper的文章,以及用aotufac+automapper合在一起用.当然发现大多数文章是将automapper的特点说出或将automapper几处关键 ...

  6. AutoFac在MVC中的使用

    在asp.net mvc控制器中使用Autofac来解析依赖 如下Controller中使用构造函数依赖注入接口IUserService: public IUserService _IUserServ ...

  7. 记一次autofac+dapper+mvc的框架搭建实践

    1,环境 .net framework4.7.2,Autofac,Autofac.Mvc5,sql server 2,动机 公司项目用的是ef,之前留下代码的大哥,到处using,代码没有分层,连复用 ...

  8. IOC容器-Autofac在MVC中实现json方式注入使用

    在你阅读时,默认已经了解IOC和autofac的基本用法, 我在最近的我的博客项目中运用了IOC autofac 实现了依赖注入 由于我的项目时asp.net MVC所以我目前向大家展示MVC中如何使 ...

  9. 【半小时大话.net依赖注入】(下)详解AutoFac+实战Mvc、Api以及.NET Core的依赖注入

    系列目录 上|理论基础+实战控制台程序实现AutoFac注入 下|详解AutoFac+实战Mvc.Api以及.NET Core的依赖注入 前言 本来计划是五篇文章的,每章发个半小时随便翻翻就能懂,但是 ...

随机推荐

  1. Entity Framework Code First (一)Conventions

    Entity Framework 简言之就是一个ORM(Object-Relational Mapper)框架. Code First 使得你能够通过C#的类来描述一个模型,模型如何被发现/检测就是通 ...

  2. url中#号的作用

    url中#号的作用就是本页面位置跳转 比如这个url地址:http://www.aaaaa.com/index.html?ad=34&m=c#red red就是index.html页面的依哥位 ...

  3. ASP.NET MVC 扩展HtmlHelper类方法

    1.扩展HtmlHelper类方法ShowPageNavigate 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...

  4. 从CIO、CEO、CFO、COO...到CVO 这22个你了解几个? (史上最完整版)

    1.CEO:是Chief Executive Officer的缩写,即首席执行官. 由于市场风云变幻,决策的速度和执行的力度比以往任何时候都更加重要.传统的“董事会决策.经理层执行”的公司体制已经难以 ...

  5. composer更新不成功,启用国内镜像网站的配置更改办法

    通过 composer config -l -g 命令可以列出 composer 的全局配置信息 [home] 配置项就是 composer 的主目录 修改config.json { "co ...

  6. 为什么局域网IP是192.168开头?

    IPv4地址分为A.B.C.D.E五类,除去特殊作用的D.E两类,剩下的A.B.C三类地址是我们常见的IP地址段.A类地址的容量最大,可以容纳16777214个主机,B类地址可以容纳65534个主机, ...

  7. C++ 序列式容器之vector

     什么是容器 容器,顾名思义,是用来容放东西的场所.C++容器容放某种数据结构,以利于对数据的搜寻或排序或其他特殊目的.众所周知,常用的数据结构不外乎:数组array,  链表list,  树tree ...

  8. 【学习笔记&训练记录】数位DP

    数位DP,即对数位进行拆分,利用数位来转移的一种DP,一般采用记忆化搜索,或者是先预处理再进行转移 一个比较大略的思想就是可以对于给定的大数,进行按数位进行固定来转移记录答案 区间类型的,可以考虑前缀 ...

  9. 【poj1985】 Cow Marathon

    http://poj.org/problem?id=1985 (题目链接) 题意 求树上两点间最长距离.题目背景以及输入描述请见poj1984. Solution 树的直径. 代码 // poj198 ...

  10. BZOJ3436 小K的农场

    Description 背景 小K是个特么喜欢玩MC的孩纸... 描述 小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了,他只记得 一些含糊的信息(共m个 ...