如何使用SimpleInjector创建通用依赖注入容器创建程序

时间:2017-08-23 18:57:14

标签: c# .net dependency-injection asp.net-core simple-injector

我在下面的库中有一个基于SimpleInjector 3.3.0的方法:

    public static Container GenerateContainer(ScopedLifestyle lifestyle, IServiceCollection services)
    {
        var container = new Container();
        container.Options.DefaultScopedLifestyle = lifestyle;

        container.RegisterSingleton(container);
        container.RegisterSingleton<IIocContainer>(new IocContainer(container));
        container.ResolveUnregisteredType += (sender, e) =>
        {
            var mapService = new Func<Type, bool>(serviceType =>
            {
                var serviceRegistration = services.FirstOrDefault(s => s.ServiceType == serviceType);
                if (serviceRegistration != null)
                {
                    var serviceProvider = services.BuildServiceProvider();
                    switch (serviceRegistration.Lifetime)
                    {
                        case ServiceLifetime.Transient:
                            e.Register(Lifestyle.Transient.CreateRegistration(e.UnregisteredServiceType, () => serviceProvider.GetService(e.UnregisteredServiceType), container));
                            break;
                        case ServiceLifetime.Scoped:
                            e.Register(Lifestyle.Scoped.CreateRegistration(e.UnregisteredServiceType, () => serviceProvider.GetService(e.UnregisteredServiceType), container));
                            break;
                        case ServiceLifetime.Singleton:
                            e.Register(Lifestyle.Singleton.CreateRegistration(e.UnregisteredServiceType, () => serviceProvider.GetService(e.UnregisteredServiceType), container));
                            break;
                    }
                    return true;
                }
                return false;
            });

            if (!mapService(e.UnregisteredServiceType) && e.UnregisteredServiceType.IsGenericType)
                mapService(e.UnregisteredServiceType.GetGenericTypeDefinition());
        };

        return container;
    }

我可以使用SimpleInjector.Integration.AspNetCore 3.3.0轻松访问我的.net核心项目之一:

Container container = GenerateContainer(new AspNetRequestLifestyle(), services);

但我最近将SimpleInjector.Integration.AspNetCore升级到4.0.10,并且不推荐使用AspNetRequestLifestyle()。但如果我尝试使用建议的那个:

Container container = GenerateContainer(new AsyncScopedLifestyle(), services);

我得到以下运行时异常:

  

启动应用程序时发生错误。您正尝试将Container注册为服务类型,但不允许注册此类型,因为类型不明确。这种类型的注册几乎总是表明应用程序的设计存在缺陷,因此是不允许的。请更改依赖于此类型依赖项的任何组件。确保容器不必通过注入其他类型来注入此类型的任何依赖项。参数名称:TService

SimpleInjector.Requires.IsNotAnAmbiguousType(Type type, string paramName)
SimpleInjector.Container.RegisterSingleton<TService>(TService instance)
Web.AspNetCore.Services.SimpleInjectorHelper.GenerateContainer(ScopedLifestyle lifestyle, IServiceCollection services)
AspNetCore.Areas.Config.Ioc.Configure(IServiceCollection services, IConfigurationRoot configuration) in Ioc.cs
+
Container = SimpleInjectorHelper.GenerateContainer(new SimpleInjector.Lifestyles.AsyncScopedLifestyle(), services);
AspNetCore.Areas.CloudStorageArea.ConfigureServices(IServiceCollection services) in PluginArea.cs
+
Ioc.Configure(services, Configuration);
Web.AspNetCore.AreaRegister.ConfigureAreasServices(IServiceCollection services)
AspNetCore.TestHost.Startup.ConfigureServices(IServiceCollection services) in Startup.cs
+
AreaRegister.ConfigureAreasServices(services);
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

如何摆脱这个问题?

0 个答案:

没有答案