标签:
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; } }
标签:
原文地址:http://www.cnblogs.com/shiningrise/p/5568880.html