簡體   English   中英

Servicestack將IServiceCollection轉換為Funq條目

[英]Servicestack convert IServiceCollection into Funq entries

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;
        }

沒有Funq不打算取代.NET Core的IoC,但是在.NET Core的IoC中注冊的任何依賴項也可用於ServiceStack

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM