public void RemoveChild(MsLifetimeScope lifetimeScope)
 {
     lock (_children)
     {
         _children.Remove(lifetimeScope);
     }
 }
        private object GetServiceInternal(Type serviceType, bool isOptional)
        {
            using (MsLifetimeScope.Using(_ownMsLifetimeScope))
            {
                // MS uses GetService<IEnumerable<TDesiredType>>() to get a collection.
                // This must be resolved with IWindsorContainer.ResolveAll();

                if (serviceType.IsGenericType && serviceType.GetGenericTypeDefinition() == typeof(IEnumerable <>))
                {
                    var allObjects = _container.ResolveAll(serviceType.GenericTypeArguments[0]);
                    Array.Reverse(allObjects);
                    return(allObjects);
                }

                if (!isOptional)
                {
                    return(_container.Resolve(serviceType));
                }

                // A single service was requested.

                // Microsoft.Extensions.DependencyInjection is built to handle optional registrations.
                // However Castle Windsor throws a ComponentNotFoundException when a type wasn't registered.
                // For this reason we have to manually check if the type exists in Windsor.

                if (_container.Kernel.HasComponent(serviceType))
                {
                    return(_container.Resolve(serviceType));
                }

                //Not found
                return(null);
            }
        }
 public void AddChild(MsLifetimeScope lifetimeScope)
 {
     lock (_children)
     {
         _children.Add(lifetimeScope);
     }
 }
        private object GetServiceInternal(Type serviceType, bool isOptional)
        {
            using (MsLifetimeScope.Using(_ownMsLifetimeScope))
            {
                var isAlreadyInResolving = IsInResolving;

                if (!isAlreadyInResolving)
                {
                    IsInResolving = true;
                }

                object instance = null;
                try
                {
                    return(instance = ResolveInstanceOrNull(serviceType, isOptional));
                }
                finally
                {
                    if (!isAlreadyInResolving)
                    {
                        if (instance != null)
                        {
                            _ownMsLifetimeScope?.AddInstance(instance);
                        }

                        IsInResolving = false;
                    }
                }
            }
        }
        /// <summary>
        /// Adds all given services from <see cref="Microsoft.Extensions.DependencyInjection"/>
        /// to the Castle Windsor container.
        /// </summary>
        /// <returns>A Castle Windsor service provider.</returns>
        public static IServiceProvider CreateServiceProvider(IWindsorContainer container, IServiceCollection services)
        {
            AddServices(container, services);

            using (MsLifetimeScope.Using(container.Resolve <GlobalMsLifetimeScope>()))
            {
                return(container.Resolve <GlobalScopedWindsorServiceProvider>());
            }
        }
        public static IDisposable Using(MsLifetimeScope newLifetimeScope)
        {
            var previous = Current;

            Current = newLifetimeScope;
            return(new DisposeAction(() =>
            {
                Current = previous;
            }));
        }
        public WindsorServiceScope(IWindsorContainer container, IMsLifetimeScope currentMsLifetimeScope)
        {
            _parentLifetimeScope = currentMsLifetimeScope;

            LifetimeScope = new MsLifetimeScope(container);

            _parentLifetimeScope?.AddChild(LifetimeScope);

            _msLifetimeScopeDisposable = MsLifetimeScope.Using(LifetimeScope);
            ServiceProvider            = container.Resolve <IServiceProvider>();
        }
        public override object Resolve(CreationContext context, IReleasePolicy releasePolicy)
        {
            if (MsLifetimeScope.Current == null)
            {
                return(base.Resolve(context, releasePolicy));
            }

            using (MsLifetimeScope.Using(_globalMsLifetimeScopeProvider.LifetimeScope))
            {
                return(base.Resolve(context, releasePolicy));
            }
        }
예제 #9
0
        public WindsorServiceScope(IWindsorContainer container, MsLifetimeScope currentMsLifetimeScope)
        {
            _parentLifetimeScope = currentMsLifetimeScope;

            LifetimeScope = new MsLifetimeScope();

            _parentLifetimeScope?.AddChild(LifetimeScope);

            using (MsLifetimeScope.Using(LifetimeScope))
            {
                ServiceProvider = container.Resolve <IServiceProvider>();
            }
        }
 public ScopedWindsorServiceProvider(IWindsorContainer container, MsLifetimeScopeProvider msLifetimeScopeProvider)
 {
     _container          = container;
     _ownMsLifetimeScope = msLifetimeScopeProvider.LifetimeScope;
 }
 public WindsorServiceScopeFactory(IWindsorContainer container, MsLifetimeScopeProvider msLifetimeScopeProvider)
 {
     _container       = container;
     _msLifetimeScope = msLifetimeScopeProvider.LifetimeScope;
 }
예제 #12
0
 public GlobalMsLifetimeScopeProvider()
 {
     LifetimeScope = new MsLifetimeScope();
 }