zoukankan      html  css  js  c++  java
  • 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;
                }
            }
  • 相关阅读:
    centos6.x 配置bond
    Js学习(2)
    Js学习(1)
    Java源码阅读计划(1) String<II>
    【461】汉明距离
    【617】合并二叉树
    Java源码阅读计划(1) String<I>
    Dubbo的高可用性
    Dubbo SpringBoot配置方法
    Dubbo基本配置属性
  • 原文地址:https://www.cnblogs.com/shiningrise/p/5568880.html
Copyright © 2011-2022 走看看