• Autofac.Integration.Web分析


    using System;
    using System.Web;
    using Autofac.Core.Lifetime;
    
    namespace Autofac.Integration.Web
    {
        /// <summary>
        /// Provides application-wide and per-request containers.
        /// </summary>
        public class ContainerProvider : IContainerProvider
        {
            readonly IContainer _applicationContainer;
            readonly Action<ContainerBuilder> _requestLifetimeConfiguration;
    
            /// <summary>
            /// Initializes a new instance of the <see cref="ContainerProvider"/> class.
            /// </summary>
            /// <param name="applicationContainer">The application container.</param>
            public ContainerProvider(IContainer applicationContainer)
            {
                if (applicationContainer == null) throw new ArgumentNullException("applicationContainer");
                _applicationContainer = applicationContainer;
            }
    
            /// <summary>
            /// Initializes a new instance of the <see cref="ContainerProvider"/> class.
            /// </summary>
            /// <param name="applicationContainer">The application container.</param>
            /// <param name="requestLifetimeConfiguration">An action that will be executed when building
            /// the per-request lifetime. The components visible within the request can be
            /// customised here.</param>
            public ContainerProvider(IContainer applicationContainer, Action<ContainerBuilder> requestLifetimeConfiguration)
                : this(applicationContainer)
            {
                if (requestLifetimeConfiguration == null) throw new ArgumentNullException("requestLifetimeConfiguration");
                _requestLifetimeConfiguration = requestLifetimeConfiguration;
            }
    
            /// <summary>
            /// Dispose of the current request's container, if it has been
            /// instantiated.
            /// </summary>
            public void EndRequestLifetime()
            {
                var rc = AmbientRequestLifetime;
                if (rc != null)
                    rc.Dispose();
            }
    
            /// <summary>
            /// The global, application-wide container.
            /// </summary>
            /// <value></value>
            public ILifetimeScope ApplicationContainer
            {
                get
                {
                    return _applicationContainer;
                }
            }
    
            /// <summary>
            /// The container used to manage components for processing the
            /// current request.
            /// </summary>
            /// <value></value>
            public ILifetimeScope RequestLifetime
            {
                get
                {
                    var result = AmbientRequestLifetime;
                    if (result == null)
                    {
                        result = _requestLifetimeConfiguration == null ?
                            ApplicationContainer.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag) :
                            ApplicationContainer.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag, _requestLifetimeConfiguration);
    
                        AmbientRequestLifetime = result;
                    }
    
                    return result;
                }
            }
    
            static ILifetimeScope AmbientRequestLifetime
            {
                get
                {
                    return (ILifetimeScope)HttpContext.Current.Items[typeof(ILifetimeScope)];
                }
                set
                {
                    HttpContext.Current.Items[typeof(ILifetimeScope)] = value;
                }
            }
        }
    }
            /// <summary>
            /// The container used to manage components for processing the
            /// current request.
            /// </summary>
            /// <value></value>
            public ILifetimeScope RequestLifetime
            {
                get
                {
                    var result = AmbientRequestLifetime;
                    if (result == null)
                    {
                        result = _requestLifetimeConfiguration == null ?
                            ApplicationContainer.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag) :
                            ApplicationContainer.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag, _requestLifetimeConfiguration);
    
                        AmbientRequestLifetime = result;
                    }
    
                    return result;
                }
            }
    
            static ILifetimeScope AmbientRequestLifetime
            {
                get
                {
                    return (ILifetimeScope)HttpContext.Current.Items[typeof(ILifetimeScope)];
                }
                set
                {
                    HttpContext.Current.Items[typeof(ILifetimeScope)] = value;
                }
            }
  • 相关阅读:
    SCP 命令
    Android 系统默认参数的修改
    java类加载时机与过程
    Git学习总结四(删除)
    Git学习总结三(工作区和暂存区、撤销修改)
    Git学习总结二(版本回退)(git resrt / git revert / git checkout)
    Git学习总结一(下载、初始化、添加文件)
    Mybatis逆向工程使用方法
    Mybatis与Spring整合方法
    Mybatis学习总结四(关联查询)
  • 原文地址:https://www.cnblogs.com/shiningrise/p/5568880.html
走看看 - 开发者的网上家园