Ejemplo n.º 1
0
        /// <summary>Constructs a new instance of <see cref="WebFormsUnityContainerOwner"/>. Registers this instance with <see cref="HostingEnvironment.RegisterObject(IRegisteredObject)"/> and sets the provided container as the <see cref="System.Web.HttpRuntime.WebObjectActivator"/> using <see cref="UnityContainerServiceProvider.SetWebObjectActivatorContainer(IUnityContainer)"/>.</summary>
        /// <param name="applicationContainer">Required. Throws <see cref="ArgumentNullException"/> if <c>null</c>.</param>
        public WebFormsUnityContainerOwner(IUnityContainer applicationContainer)
        {
            this.Container = applicationContainer ?? throw new ArgumentNullException(nameof(applicationContainer));

            // Before continuing, ensure that IChildContainerConfiguration is registered, and if not, add our own dummy implementation:
            if (!applicationContainer.IsRegistered <IChildContainerConfiguration>())
            {
                applicationContainer.RegisterSingleton <IChildContainerConfiguration, DefaultChildContainerConfiguration>();
            }

            HostingEnvironment.RegisterObject(this);

            StaticWebFormsUnityContainerOwner.RootContainer = this.Container;
            this.ucsp = UnityContainerServiceProvider.SetWebObjectActivatorContainer(this.Container);
        }
        /// <summary>Sets the provided container as the <see cref="HttpRuntime.WebObjectActivator"/> by wrapping it in a <see cref="UnityContainerServiceProvider"/> which is then returned.</summary>
        public static UnityContainerServiceProvider SetWebObjectActivatorContainer(IUnityContainer container)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            lock ( _httpRuntimeLock )
            {
                IServiceProvider currentServiceProvider = HttpRuntime.WebObjectActivator;

                UnityContainerServiceProvider ucsp = new UnityContainerServiceProvider(container, currentServiceProvider);

                HttpRuntime.WebObjectActivator = ucsp;

                return(ucsp);
            }
        }