Servicestack将IServiceCollection转换为Funq条目

时间:2016-11-28 13:26:40

标签: c# asp.net-core servicestack

servicestack.core包是否包含https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection#replacing-the-default-services-container实现?

将IServiceCollection转换为Funq容器注册?这样的事情?

        var container = new Container();
        services.Each(x =>
        {
            if (x.ImplementationType != null)
                container.Register(x.ImplementationInstance, x.ServiceType);
            else if (x.ImplementationFactory != null)
                container.RegisterFn(x.ServiceType, x.ImplementationFactory);
            else
                container.RegisterAutoWiredType(x.ServiceType, x.ImplementationType, x.Lifetime == ServiceLifetime.Singleton ? ReuseScope.Container : x.Lifetime == ServiceLifetime.Scoped ? ReuseScope.Request : ReuseScope.None);

        });

        public static Container RegisterFn(this Container container, Type serviceType, Func<IServiceProvider, object> factory)
        {

            typeof(FunqExtensions).GetMethodInfos().First(x =>x.Name == "Register").MakeGenericMethod(serviceType).Invoke(null, new object[]{ container, factory});
            return container;
        }

        private static Container Register<T>(this Container container, Func<IServiceProvider, object> registrationFactory)
        {
            container.Register<T>(null, c => (T) registrationFactory(c));
            return container;
        }

1 个答案:

答案 0 :(得分:0)

没有Funq不打算取代.NET Core的IoC,而是取代任何dependencies registered in .NET Core's IoC are also available to ServiceStack