• 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;
                }
            }
  • 相关阅读:
    Java中常见的异常
    WebView中Js与Android本地函数的相互调用
    Gson解析POJO类中的泛型参数
    JAVA反射技术的使用
    Couchbase 找回登录密码
    微信内网页支付开发手记
    Android实现自定义字体
    Android实现图片裁切
    Android实现ExpandableTextView可扩展TextView
    仿美团实现地域选择(二)
  • 原文地址:https://www.cnblogs.com/shiningrise/p/5568880.html
Copyright © 2020-2023  润新知