コード例 #1
0
ファイル: Startup.cs プロジェクト: twix20/JetAnotherEMS
        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.AddCors();

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info {
                    Title = "JetAnotherEMS API", Version = "v1"
                });
            });

            AddAutoMapperSetup(services);
            AddAuthorization(services);
            AddAuthentication(services);

            var builder = new ContainerBuilder();

            builder.RegisterType <HttpContextAccessor>().As <IHttpContextAccessor>();
            builder.RegisterType <ClaimsRequirementHandler>().As <IAuthorizationHandler>();

            builder.Populate(services);
            AutofacBootStrapper.RegisterServices(builder);

            // DB Contexts
            builder.RegisterType <ApplicationContextDbFactory>().AsSelf().AsImplementedInterfaces();

            builder.RegisterType <EventStoreSQLContext>().InstancePerLifetimeScope();
            builder.RegisterType <JetAnotherEmsContext>().InstancePerLifetimeScope();

            builder.Register <ApplicationDbContext>(ctx =>
            {
                var factory = ctx.Resolve <IDesignTimeDbContextFactory <ApplicationDbContext> >();
                return(factory.CreateDbContext(new string[] { }));
            }).InstancePerLifetimeScope();

            ApplicationContainer = builder.Build();

            return(new AutofacServiceProvider(this.ApplicationContainer));
        }