Esempio n. 1
0
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, AnnouncementsDbContext announcementsDbContext)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logger"));
            loggerFactory.AddDebug();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler(appBuilder =>
                {
                    appBuilder.Run(async context =>
                    {
                        var exceptionHandler = context.Features.Get <IExceptionHandlerFeature>();
                        if (exceptionHandler != null)
                        {
                            var logger = loggerFactory.CreateLogger("Global exception logger");
                            logger.LogError(500, exceptionHandler.Error, exceptionHandler.Error.Message);
                        }

                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("An unxpected error happened. Try again later.");
                    });
                });
                app.UseHsts();
            }

            // Mapping entities and view models
            AutoMapping();

            // Seeding fake data
            announcementsDbContext.SeedAnnouncementsData();

            app.UseSwaggerUi(typeof(Startup).GetTypeInfo().Assembly, settings =>
            {
                settings.GeneratorSettings.DefaultPropertyNameHandling =
                    PropertyNameHandling.CamelCase;
            });

            app.UseSwagger(typeof(Startup).Assembly, settings =>
            {
                settings.PostProcess = document =>
                {
                    document.Info.Version        = "V1";
                    document.Info.Title          = "Announcements API";
                    document.Info.Description    = "Web API for announcements";
                    document.Info.TermsOfService = "None";
                    document.Info.Contact        = new SwaggerContact
                    {
                        Name  = "David Chkhitunidze",
                        Email = "*****@*****.**"
                    };
                    document.Info.License = new SwaggerLicense
                    {
                        Name = "License",
                        Url  = "https://example.com/license"
                    };
                };
            });

            app.UseIpRateLimiting();
            //app.UseResponseCaching();
            //app.UseHttpCacheHeaders();
            app.UseHttpsRedirection();
            app.UseMvc();
        }