<PackageReference Include="Castle.Windsor" Version="5.1.2" />

WindsorContainer

Implementation of IWindsorContainer which delegates to IKernel implementation.
using Castle.MicroKernel; using Castle.MicroKernel.Registration; using Castle.MicroKernel.SubSystems.Configuration; using Castle.MicroKernel.SubSystems.Resource; using Castle.Windsor.Configuration; using Castle.Windsor.Configuration.Interpreters; using Castle.Windsor.Diagnostics; using Castle.Windsor.Installer; using Castle.Windsor.Proxy; using System; using System.Collections.Generic; using System.Diagnostics; using System.Text; using System.Threading; namespace Castle.Windsor { [Serializable] [DebuggerDisplay("{name,nq}")] [DebuggerTypeProxy(typeof(KernelDebuggerProxy))] public class WindsorContainer : IWindsorContainer, IDisposable { private const string CastleUnicode = "🏰"; private static int instanceCount; private readonly IKernel kernel; private readonly string name; private readonly IComponentsInstaller installer; private IWindsorContainer parent; private readonly Dictionary<string, IWindsorContainer> childContainers = new Dictionary<string, IWindsorContainer>(StringComparer.OrdinalIgnoreCase); private readonly object childContainersLocker = new object(); public IComponentsInstaller Installer => installer; public virtual IKernel Kernel => kernel; public string Name => name; public virtual IWindsorContainer Parent { get { return parent; } set { if (value == null) { if (parent != null) { parent.RemoveChildContainer(this); parent = null; } } else if (value != parent) { parent = value; parent.AddChildContainer(this); } } } public WindsorContainer() : this(new DefaultKernel(), new DefaultComponentInstaller()) { } public WindsorContainer(IConfigurationStore store) : this() { kernel.ConfigurationStore = store; RunInstaller(); } public WindsorContainer(IConfigurationInterpreter interpreter) : this() { if (interpreter == null) throw new ArgumentNullException("interpreter"); interpreter.ProcessResource(interpreter.Source, kernel.ConfigurationStore, kernel); RunInstaller(); } public WindsorContainer(IConfigurationInterpreter interpreter, IEnvironmentInfo environmentInfo) : this() { if (interpreter == null) throw new ArgumentNullException("interpreter"); if (environmentInfo == null) throw new ArgumentNullException("environmentInfo"); interpreter.EnvironmentName = environmentInfo.GetEnvironmentName(); interpreter.ProcessResource(interpreter.Source, kernel.ConfigurationStore, kernel); RunInstaller(); } public WindsorContainer(string configurationUri) : this() { if (configurationUri == null) throw new ArgumentNullException("configurationUri"); XmlInterpreter interpreter = GetInterpreter(configurationUri); interpreter.ProcessResource(interpreter.Source, kernel.ConfigurationStore, kernel); RunInstaller(); } public WindsorContainer(IKernel kernel, IComponentsInstaller installer) : this(MakeUniqueName(), kernel, installer) { } public WindsorContainer(string name, IKernel kernel, IComponentsInstaller installer) { if (name == null) throw new ArgumentNullException("name"); if (kernel == null) throw new ArgumentNullException("kernel"); if (installer == null) throw new ArgumentNullException("installer"); this.name = name; this.kernel = kernel; this.kernel.ProxyFactory = new DefaultProxyFactory(); this.installer = installer; } public WindsorContainer(IProxyFactory proxyFactory) { if (proxyFactory == null) throw new ArgumentNullException("proxyFactory"); kernel = new DefaultKernel(proxyFactory); installer = new DefaultComponentInstaller(); } public WindsorContainer(IWindsorContainer parent, IConfigurationInterpreter interpreter) : this() { if (parent == null) throw new ArgumentNullException("parent"); if (interpreter == null) throw new ArgumentNullException("interpreter"); parent.AddChildContainer(this); interpreter.ProcessResource(interpreter.Source, kernel.ConfigurationStore, kernel); RunInstaller(); } public WindsorContainer(string name, IWindsorContainer parent, IConfigurationInterpreter interpreter) : this() { if (name == null) throw new ArgumentNullException("name"); if (parent == null) throw new ArgumentNullException("parent"); if (interpreter == null) throw new ArgumentNullException("interpreter"); this.name = name; parent.AddChildContainer(this); interpreter.ProcessResource(interpreter.Source, kernel.ConfigurationStore, kernel); RunInstaller(); } protected virtual void RunInstaller() { if (installer != null) installer.SetUp(this, kernel.ConfigurationStore); } private void Install(IWindsorInstaller[] installers, DefaultComponentInstaller scope) { using (PartialConfigurationStore store = new PartialConfigurationStore((IKernelInternal)kernel)) { for (int i = 0; i < installers.Length; i++) { installers[i].Install(this, store); } scope.SetUp(this, store); } } public virtual void Dispose() { Parent = null; childContainers.Clear(); kernel.Dispose(); } public virtual void AddChildContainer(IWindsorContainer childContainer) { if (childContainer == null) throw new ArgumentNullException("childContainer"); if (!childContainers.ContainsKey(childContainer.Name)) { lock (childContainersLocker) { if (!childContainers.ContainsKey(childContainer.Name)) { kernel.AddChildKernel(childContainer.Kernel); childContainers.Add(childContainer.Name, childContainer); childContainer.Parent = this; } } } } public IWindsorContainer AddFacility(IFacility facility) { kernel.AddFacility(facility); return this; } public IWindsorContainer AddFacility<T>() where T : IFacility, new { kernel.AddFacility<T>(); return this; } public IWindsorContainer AddFacility<T>(Action<T> onCreate) where T : IFacility, new { kernel.AddFacility(onCreate); return this; } public IWindsorContainer GetChildContainer(string name) { lock (childContainersLocker) { childContainers.TryGetValue(name, out IWindsorContainer value); return value; } } public IWindsorContainer Install(params IWindsorInstaller[] installers) { if (installers == null) throw new ArgumentNullException("installers"); if (installers.Length == 0) return this; DefaultComponentInstaller scope = new DefaultComponentInstaller(); IKernelInternal kernelInternal = kernel as IKernelInternal; if (kernelInternal == null) Install(installers, scope); else { IDisposable disposable = kernelInternal.OptimizeDependencyResolution(); Install(installers, scope); disposable?.Dispose(); } return this; } public IWindsorContainer Register(params IRegistration[] registrations) { Kernel.Register(registrations); return this; } public virtual void Release(object instance) { kernel.ReleaseComponent(instance); } public virtual void RemoveChildContainer(IWindsorContainer childContainer) { if (childContainer == null) throw new ArgumentNullException("childContainer"); if (childContainers.ContainsKey(childContainer.Name)) { lock (childContainersLocker) { if (childContainers.ContainsKey(childContainer.Name)) { kernel.RemoveChildKernel(childContainer.Kernel); childContainers.Remove(childContainer.Name); childContainer.Parent = null; } } } } public virtual object Resolve(Type service, Arguments arguments) { return kernel.Resolve(service, arguments); } public virtual object Resolve(Type service) { return kernel.Resolve(service, null); } public virtual object Resolve(string key, Type service) { return kernel.Resolve(key, service, null); } public virtual object Resolve(string key, Type service, Arguments arguments) { return kernel.Resolve(key, service, arguments); } public T Resolve<T>(Arguments arguments) { return (T)kernel.Resolve(typeof(T), arguments); } public virtual T Resolve<T>(string key, Arguments arguments) { return (T)kernel.Resolve(key, typeof(T), arguments); } public T Resolve<T>() { return (T)kernel.Resolve(typeof(T), null); } public virtual T Resolve<T>(string key) { return (T)kernel.Resolve(key, typeof(T), null); } public T[] ResolveAll<T>() { return (T[])ResolveAll(typeof(T)); } public Array ResolveAll(Type service) { return kernel.ResolveAll(service); } public Array ResolveAll(Type service, Arguments arguments) { return kernel.ResolveAll(service, arguments); } public T[] ResolveAll<T>(Arguments arguments) { return (T[])ResolveAll(typeof(T), arguments); } private XmlInterpreter GetInterpreter(string configurationUri) { try { return new XmlInterpreter(((IResourceSubSystem)Kernel.GetSubSystem(SubSystemConstants.ResourceKey)).CreateResource(configurationUri)); } catch (Exception) { return new XmlInterpreter(configurationUri); } } private static string MakeUniqueName() { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("🏰"); stringBuilder.Append(" "); stringBuilder.Append(Interlocked.Increment(ref instanceCount)); return stringBuilder.ToString(); } } }