Exemplo n.º 1
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
            services.AddApplicationInsightsTelemetry(Configuration);

            services.AddIdentity <ApplicationUser, IdentityRole>(config =>
            {
                config.SignIn.RequireConfirmedEmail    = true;
                config.Password.RequireNonAlphanumeric = false;
                config.Password.RequireLowercase       = false;
                config.Password.RequireUppercase       = false;
                config.Password.RequireDigit           = false;
                //config.Password.RequiredLength = 8;
            })
            .AddEntityFrameworkStores <AwardContext>()
            .AddDefaultTokenProviders();

            var connection = Configuration.GetConnectionString(dbContextName);
            var useInMemDb = connection.IsNullOrWhiteSpace();

            if (useInMemDb)
            {
                services.AddDbContext <AwardContext>(
                    options => options.UseInMemoryDatabase(dbContextName)
                    );
                var provider    = services.BuildServiceProvider();
                var ctx         = provider.GetRequiredService <AwardContext>();
                var userManager = provider.GetRequiredService <UserManager <ApplicationUser> >();
                DbSeeder.Seed(ctx, userManager);
                var cachedData = new CachedData();
                var crawls     = ctx.Crawls.Include(c => c.Route).ThenInclude(r => r.FromAirport).Include(c => c.Route).ThenInclude(r => r.ToAirport).ToList();
                cachedData.Set(crawls);
                services.AddSingleton <ICachedData>(cachedData);
            }
            else
            {
                services.AddLetsEncrypt();
                services.AddSingleton <ICachedData, CachedData>();
                services.AddDbContextPool <AwardContext>(options => options.UseSqlServer(Configuration.GetConnectionString(dbContextName)));
                services.AddHostedService <HostedDataService>();
            }

            services.AddHttpClient <SASRestClient>()
            .ConfigurePrimaryHttpMessageHandler(() => FlysasLib.HttpClientFactory.CreateHandler())
            .ConfigureHttpClient(client => FlysasLib.HttpClientFactory.SetDefaultHeaders(client));

            services.Configure <Models.SMTPOptions>(Configuration.GetSection("SMTPOptions"), (BinderOptions o) => o.BindNonPublicProperties = true);
            services.Configure <Models.AppSettings>(Configuration.GetSection("AppSettings"), (BinderOptions o) => o.BindNonPublicProperties = true);

            services.AddTransient <IEmailSender, EmailSender>();
            services.AddScoped <IViewRenderService, ViewRenderService>();
        }