ServiceCollectionResolver.cs

35 lines | 1.129 kB Blame History Raw Download
namespace Web.Infrastructure
{
    using Castle.Core;
    using Castle.MicroKernel.Context;
    using Castle.MicroKernel;
    using Microsoft.Extensions.DependencyInjection;
    using System.Diagnostics;
    using System.Linq;

    [DebuggerStepThrough]
    public class ServiceCollectionResolver : ISubDependencyResolver
    {
        public IServiceCollection Container { get; }

        [DebuggerStepThrough]
        public ServiceCollectionResolver(IServiceCollection container)
        {
            Container = container;
        }

        [DebuggerStepThrough]
        public bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
        {
            return Container.Any(x => x.ServiceType == dependency.TargetType);
        }

        [DebuggerStepThrough]
        public object Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
        {
            var provider = Container.BuildServiceProvider();
            return provider.GetService(dependency.TargetType);
        }
    }
}