Exemple #1
0
        public static void RegisterComponents(IServiceCollection services)
        {
            services.AddHttpClient();
            services.AddSingleton <RequestToHttpHelper>();
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            RegisterBLL.RegisterComponents(services);
            RegisterDAL.RegisterComponents(services);
            RegisterMediatR.RegisterComponents(services);

            //自动注入IAutoInject
            services.Scan(x =>
            {
                var entryAssembly        = Assembly.GetEntryAssembly();
                var referencedAssemblies = entryAssembly.GetReferencedAssemblies().Select(Assembly.Load);
                var assemblies           = new List <Assembly> {
                    entryAssembly
                }.Concat(referencedAssemblies);

                x.FromAssemblies(assemblies)
                .AddClasses(classes => classes.AssignableTo(typeof(IAutoInject)))
                .AsImplementedInterfaces()
                .WithScopedLifetime()
                //接口注册Scoped
                .AddClasses(classes => classes.AssignableTo(typeof(IScopedAutoInject)))
                .AsImplementedInterfaces()
                .WithScopedLifetime()
                //接口注册Singleton
                .AddClasses(classes => classes.AssignableTo(typeof(ISingletonAutoInject)))
                .AsImplementedInterfaces()
                .WithSingletonLifetime()
                //接口注册Transient
                .AddClasses(classes => classes.AssignableTo(typeof(ITransientAutoInject)))
                .AsImplementedInterfaces()
                .WithTransientLifetime()
                //具体类注册Scoped
                .AddClasses(classes => classes.AssignableTo(typeof(ISelfScopedAutoInject)))
                .AsSelf()
                .WithScopedLifetime()
                //具体类注册Singleton
                .AddClasses(classes => classes.AssignableTo(typeof(ISelfSingletonAutoInject)))
                .AsSelf()
                .WithSingletonLifetime()
                //具体类注册Transient
                .AddClasses(classes => classes.AssignableTo(typeof(ISelfTransientAutoInject)))
                .AsSelf()
                .WithTransientLifetime();
            });
        }