Example #1
0
 /// <summary>
 /// 单例模式
 /// </summary>
 /// <returns></returns>
 public static DependencyInjectionService GetInstance(IServiceCollection services)
 {
     _services = services;
     if (_dependencyInjectionConfiguration == null)
     {
         lock (LockObj)
         {
             if (_dependencyInjectionConfiguration == null)
             {
                 _dependencyInjectionConfiguration = new DependencyInjectionService();
             }
         }
     }
     return(_dependencyInjectionConfiguration);
 }
        public static AutofacServiceProvider Configure(this IServiceCollection services, string connectionString, IConfigurationRoot configuration)
        {
            services.AddDbContext <MyContext>(options => options.UseSqlServer(connectionString));
            //注入
            DependencyInjectionService.GetInstance(services)
            .AddMvc()
            .AddCookie()
            .AddSwagger()
            .AddCors()
            .AddDddLayering()
            .AddEventBus()
            .AddCap();
            //.AddIdentityServer();
            //Autofac依赖注入 Class的后面名字必须一致才能注入
            var builder = new ContainerBuilder();

            builder.Populate(services);
            var module = new ConfigurationModule(configuration);

            builder.RegisterModule(module);
            Container = builder.Build();
            return(new AutofacServiceProvider(Container));
        }