Beispiel #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                              ILoggerFactory loggerFactory, LibraryContext libraryContext)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                //add configuration to exception handler middleware to have it return a generic error message. Configure by passing in a lambda that returns an action on IApplicationBuilder.
                app.UseExceptionHandler(appBuilder =>
                {
                    //then call run on that appBuilder. Adds a piece of code to the request response pipeline. Make sure that the SC is 500 and that we write out a generic error message in the response body.
                    appBuilder.Run(async context =>
                    {
                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("An unexpected fault happened. Try again later");
                    });
                });
            }

            AutoMapper.Mapper.Initialize(cfg =>
            {
                cfg.CreateMap <Entities.Author, Models.AuthorDto>()
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => $"{src.FirstName} {src.LastName}"))
                .ForMember(dest => dest.Age, opt => opt.MapFrom(src => src.DateOfBirth.GetCurrentAge()));
            });


            libraryContext.EnsureSeedDataForContext();

            app.UseMvc();
        }
Beispiel #2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                              ILoggerFactory loggerFactory, LibraryContext libraryContext)
        {
            loggerFactory.AddConsole();
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            ////app.Run(async (context) =>
            ////{
            ////    await context.Response.WriteAsync("Hello World!");
            ////});

            AutoMapper.Mapper.Initialize(con =>
            {
                con.CreateMap <Author, AuthorDto>()
                .ForMember("Name", dest => dest.MapFrom(src => $"{src.FirstName} {src.LastName}"))
                .ForMember(dest => dest.Age, option => option.MapFrom(src => src.DateOfBirth.GetCurrentAge()));

                con.CreateMap <Book, BookDto>();

                con.CreateMap <AuthorCreationDto, Author>();
                con.CreateMap <BookCreationDto, Book>();
                con.CreateMap <BookUpdationDto, Book>();
                con.CreateMap <Book, BookUpdationDto>();
            });


            libraryContext.EnsureSeedDataForContext();

            app.UseMvc();
        }
Beispiel #3
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, LibraryContext libraryContext)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler(appBuilder => {
                    appBuilder.Run(async context => {
                        var exception = context.Features.Get <IExceptionHandlerFeature>();
                        if (exception != null)
                        {
                            var logger = loggerFactory.CreateLogger("Global error logger.");
                            logger.LogError(500,
                                            exception.Error,
                                            "Something happend, and that's not good. Error: " + exception.Error.Message);
                        }

                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("Unexpected fault happened. Try again later.");
                    });
                });
            }

            libraryContext.EnsureSeedDataForContext();

            InitializeAutoMapper();

            app.UseMvc();
        }
Beispiel #4
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                              ILoggerFactory loggerFactory, LibraryContext libraryContext)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler(appBuilder =>
                {
                    appBuilder.Run(async context =>
                    {
                        var exceptionHandlerFeature = context.Features.Get <IExceptionHandlerFeature>();
                        if (exceptionHandlerFeature != null)
                        {
                            var logger = loggerFactory.CreateLogger("Global exception logger");
                            logger.LogError(500,
                                            exceptionHandlerFeature.Error,
                                            exceptionHandlerFeature.Error.Message);
                        }

                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("An unexpected fault occurred. Try again.");
                    });
                });
            }

            AutoMapper.Mapper.Initialize(cfg =>
            {
                cfg.CreateMap <Entities.Author, Models.AuthorDto>()
                .ForMember(dest => dest.Name, opt => opt.MapFrom(
                               src => $"{src.FirstName} {src.LastName}"))
                .ForMember(dest => dest.Age, opt => opt.MapFrom(
                               src => src.DateOfBirth.GetCurrentAge(src.DateOfDeath)));

                cfg.CreateMap <Entities.Book, Models.BookDto>();

                cfg.CreateMap <Models.AuthorForCreationDto, Entities.Author>();

                cfg.CreateMap <Models.AuthorForCreationWithDateOfDeathDto, Entities.Author>();

                cfg.CreateMap <Models.BookForCreationDto, Entities.Book>();

                cfg.CreateMap <Models.BookForUpdateDto, Entities.Book>();

                cfg.CreateMap <Entities.Book, Models.BookForUpdateDto>();
            });

            libraryContext.EnsureSeedDataForContext();

            // Should go before everything else except our exceptions and automapper
            app.UseIpRateLimiting();

            app.UseResponseCaching();

            app.UseHttpCacheHeaders();

            app.UseMvc();
        }
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                              ILoggerFactory loggerFactory, LibraryContext libraryContext)
        {
            loggerFactory.AddConsole();
            loggerFactory.AddDebug(LogLevel.Information);

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

                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("An unexpected fault happened. Try again later.");
                    });
                });
            }

            libraryContext.EnsureSeedDataForContext();

            AutoMapper.Mapper.Initialize(x =>
            {
                x.CreateMap <Author, AuthorDto> ()
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src =>
                                                                 $"{src.FirstName} {src.LastName}"
                                                                 ))
                .ForMember(dest => dest.Age, opt => opt.MapFrom(src =>
                                                                src.DateOfBirth.GetCurrentAge()
                                                                ));
                x.CreateMap <Book, BookDto> ();
                x.CreateMap <Book, BookForUpdateDto> ();

                x.CreateMap <AuthorForCreationDto, Author> ();
                x.CreateMap <BookForCreationDto, Book> ();
                x.CreateMap <BookForUpdateDto, Book> ();
            });

            app.UseIpRateLimiting();
            app.UseResponseCaching();
            app.UseHttpCacheHeaders();
            app.UseMvc();
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Library API");
            });
        }
Beispiel #6
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                              LibraryContext libraryContext, ILoggerFactory loggerFactory)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler(appBuilder =>
                                        appBuilder.Run(async context =>
                {
                    context.Response.StatusCode = 500;
                    await context.Response.WriteAsync("An unexpected fault happened. Try again later.");
                })
                                        );
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            libraryContext.EnsureSeedDataForContext();

            app.UseHttpsRedirection();
            app.UseMvc();
        }
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                              ILoggerFactory loggerFactory, LibraryContext libraryContext)
        {
            loggerFactory.AddConsole();

            loggerFactory.AddDebug(LogLevel.Information);

            //  loggerFactory.AddProvider(new NLog.Extensions.Logging.NLogLoggerProvider());

            loggerFactory.AddNLog();

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

                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("An unexpected fault happened. Try again later.");
                    });
                });
            }

            AutoMapper.Mapper.Initialize(cfg =>
            {
                cfg.CreateMap <Entities.Author, Models.AuthorDto>()
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src =>
                                                                 $"{src.FirstName} {src.LastName}"))
                .ForMember(dest => dest.Age, opt => opt.MapFrom(src =>
                                                                src.DateOfBirth.GetCurrentAge()));

                cfg.CreateMap <Entities.Book, Models.BookDto>();

                cfg.CreateMap <Models.AuthorForCreationDto, Entities.Author>();

                cfg.CreateMap <Models.BookForCreationDto, Entities.Book>();

                cfg.CreateMap <Models.BookForUpdateDto, Entities.Book>();

                cfg.CreateMap <Entities.Book, Models.BookForUpdateDto>();
            });


            libraryContext.EnsureSeedDataForContext();

            app.UseMvc();
        }
Beispiel #8
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                              ILoggerFactory loggerFactory, LibraryContext libraryContext)
        {
            loggerFactory.AddConsole();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler(appBuilder =>
                {
                    appBuilder.Run(async context =>
                    {
                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("An unexpected fault occured. Try again later.");
                    });
                });
            }

            var modelMappings = new ModelMappings();

            modelMappings.MapModels();


            libraryContext.EnsureSeedDataForContext();

            app.UseMvc();
        }
Beispiel #9
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                              ILoggerFactory loggerFactory, LibraryContext libraryContext)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler(appBuilder =>
                {
                    appBuilder.Run(async context =>
                    {
                        // globalne odchytavame exceptions a pokud je k dispozici exception handle feature - poskytuje informace o exception
                        // tak logujeme pomoci NLog do souboru
                        var exceptionHandlerFeature = context.Features.Get <IExceptionHandlerFeature>();
                        if (exceptionHandlerFeature != null)
                        {
                            var logger = loggerFactory.CreateLogger("Global exception logger");
                            logger.LogError(500, exceptionHandlerFeature.Error, exceptionHandlerFeature.Error.Message);
                        }

                        // na zaver odesleme status code 500 a nasi error message
                        // tyka se vsech neosetrenych chyb
                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("An unexpected fault happened. Try again later.");
                    });
                });
            }

            AutoMapper.Mapper.Initialize(cfg =>
            {
                // pokud mame nejake specialni property, ktere jsou vyutvareny dodatecne, tak je namapujeme
                // Name ziskame spojenim FirstName + LastName
                // Age zavolanim matody GetCurrentAge()
                cfg.CreateMap <Entities.Author, Models.AuthorDto>()
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => $"{src.FirstName} {src.LastName}"))
                .ForMember(dest => dest.Age, opt => opt.MapFrom(src => src.DateOfBirth.GetCurrentAge(src.DateOfDeath)));

                cfg.CreateMap <Entities.Book, Models.BookDto>();

                cfg.CreateMap <Models.AuthorForCreationDto, Entities.Author>();

                cfg.CreateMap <Models.AuthorForCreationWithDateOfDeathDto, Entities.Author>();

                cfg.CreateMap <Models.BookForCreationDto, Entities.Book>();

                cfg.CreateMap <Models.BookForUpdateDto, Entities.Book>();

                cfg.CreateMap <Entities.Book, Models.BookForUpdateDto>();
            });

            libraryContext.EnsureSeedDataForContext();

            app.UseIpRateLimiting();

            app.UseHttpCacheHeaders();

            app.UseMvc();
        }
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, LibraryContext libraryContext)
        {
            loggerFactory.AddConsole();
            loggerFactory.AddDebug(LogLevel.Information);

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

                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("An unexpected fault happened. Try again later.");
                    });
                });
            }

            libraryContext.EnsureSeedDataForContext();

            app.UseMvc();
        }
Beispiel #11
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, LibraryContext context)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            context.EnsureSeedDataForContext();
            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
Beispiel #12
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                              ILoggerFactory loggerFactory, LibraryContext libraryContext)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler(appBuilder =>
                {
                    appBuilder.Run(async context =>
                    {
                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("An unexpected fault happened. Try again later.");
                    });
                });
            }

            AutoMapper.Mapper.Initialize(cfg =>
            {
                cfg.CreateMap <Entities.Author, Models.AuthorDto>()
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src =>
                                                                 $"{src.FirstName} {src.LastName}"))
                .ForMember(dest => dest.Age, opt => opt.MapFrom(src =>
                                                                src.DateOfBirth.GetCurrentAge()));

                cfg.CreateMap <Entities.Book, Models.BookDto>();
            });


            libraryContext.EnsureSeedDataForContext();

            app.UseMvc();
        }
Beispiel #13
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, LibraryContext context)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            AutoMapper.Mapper.Initialize(config => {
                config.CreateMap <Entities.Author, Models.AuthorDto>()
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => $"{src.FirstName} {src.LastName}"))
                .ForMember(dest => dest.Age, opt => opt.MapFrom(src => src.DateOfBirth.GetCurrentAge()));

                config.CreateMap <Entities.Book, Models.BookDto>();

                config.CreateMap <Models.CreateAuthorDto, Entities.Author>();
            });

            context.EnsureSeedDataForContext();
            app.UseHttpsRedirection();
            app.UseMvc();
        }
Beispiel #14
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                              ILoggerFactory loggerFactory, LibraryContext libraryContext)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler();
            }

            AutoMapper.Mapper.Initialize(cfg =>
            {
                cfg.CreateMap <Entities.Author, Models.AuthorDto>()
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src =>
                                                                 $"{src.FirstName} {src.LastName}"))
                .ForMember(dest => dest.Age, opt => opt.MapFrom(src =>
                                                                src.DateOfBirth.GetCurrentAge()));
            });

            libraryContext.EnsureSeedDataForContext();

            app.UseMvc();
        }
Beispiel #15
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                              ILoggerFactory loggerFactory, LibraryContext libraryContext)
        {
            if (env.IsDevelopment())
            {
                //app.UseDeveloperExceptionPage();
                app.UseExceptionHandler(appBuild =>
                {
                    appBuild.Run(async context =>
                    {
                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("Falha inesperada. Tenta novamente mais tarde!");
                    });
                });
            }
            else
            {
                app.UseExceptionHandler(appBuild =>
                {
                    appBuild.Run(async context =>
                    {
                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("Falha inesperada. Tenta novamente mais tarde!");
                    });
                });
            }

            libraryContext.EnsureSeedDataForContext();

            app.UseMvc();
        }
Beispiel #16
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, LibraryContext libraryContext)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler(appBuilder =>
                {
                    appBuilder.Run(async context =>
                    {
                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("error");
                    });
                });
                app.UseHsts();
            }
            libraryContext.EnsureSeedDataForContext();
            //app.UseHttpsRedirection();

            AutoMapper.Mapper.Initialize(cfg =>
            {
                cfg.CreateMap <Author, AuthorDto>()
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src =>
                                                                 $"{src.FirstName} {src.LastName}"))
                .ForMember(dest => dest.Age, opt => opt.MapFrom(src =>
                                                                src.DateOfBirth.GetCurrentAge()));
                cfg.CreateMap <Book, BookDto>();
                cfg.CreateMap <AuthorForCreationDto, Author>();
                cfg.CreateMap <BookForCreationDto, Book>();
            });
            app.UseMvc();
        }
Beispiel #17
0
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, LibraryContext libraryContext)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();


            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                //Cambio  "ASPNETCORE_ENVIRONMENT": "Development" por >> "ASPNETCORE_ENVIRONMENT": "Production"
                app.UseExceptionHandler(appBuilder =>
                {
                    appBuilder.Run(async context =>
                    {
                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("Ocurrio un error. Intente nuevamente o ponganse en contacto con [email protected]");
                    });
                });
            }


            AutoMapper.Mapper.Initialize(cfg =>
            {
                cfg.CreateMap <Author, AuthorDto>()
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src =>
                                                                 $"{src.FirstName} {src.LastName}"))
                .ForMember(dest => dest.Age, opt => opt.MapFrom(src =>

                                                                src.DateOfBirth.GetCurrentAge()));

                cfg.CreateMap <Book, BookDto>();


                cfg.CreateMap <Models.AuthorCreationDto, Author>();

                cfg.CreateMap <Models.BookCreationDto, Book>();

                //TODO : 04 - Agrego la configuracion de los nuevos DTO


                //cfg.CreateMap<BookUpdateDto, Book>();

                //cfg.CreateMap<Book, BookUpdateDto>();
            });
            app.UseSwagger();

            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            });

            libraryContext.EnsureSeedDataForContext();

            app.UseMvc();
        }
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, LibraryContext libraryContext)
        {
            //  loggerFactory.AddProvider(new NLog.Extensions.Logging.NLogLoggerProvider());

            loggerFactory.AddNLog();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseExceptionHandler(appBuilder =>
                {
                    appBuilder.Run(async context =>
                    {
                        var exceptionHandlerFeature = context.Features.Get <IExceptionHandlerFeature>();
                        if (exceptionHandlerFeature != null)
                        {
                            var logger = loggerFactory.CreateLogger("Global exception logger");
                            logger.LogError(500,
                                            exceptionHandlerFeature.Error,
                                            exceptionHandlerFeature.Error.Message);
                        }

                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("An unexpected fault happened. Try again later.");
                    });
                });

                app.UseHsts();
            }

            AutoMapper.Mapper.Initialize(cfg =>
            {
                cfg.CreateMap <Author, AuthorDto>()
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src =>
                                                                 $"{src.FirstName} {src.LastName}"))
                .ForMember(dest => dest.Age, opt => opt.MapFrom(src =>
                                                                src.DateOfBirth.GetCurrentAge()));

                cfg.CreateMap <Book, BookDto>();

                cfg.CreateMap <AuthorForCreationDto, Author>();

                cfg.CreateMap <BookForCreationDto, Book>();

                cfg.CreateMap <BookForUpdateDto, Book>();

                cfg.CreateMap <Book, BookForUpdateDto>();
            });


            libraryContext.EnsureSeedDataForContext();
            app.UseHttpsRedirection();
            app.UseMvc();
        }
Beispiel #19
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, LibraryContext libraryContext,
                              ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole();

            /*
             * LogLevel.Trace is used for most defailed log message, only valuable into a developer debugging issue
             * LogLevel.Debug contains information that may be useful for debugging but it doesn't have any long term value.
             * LogLevel.Information is used to track the general flow of the application. Has some long term value.
             * LogLevel.Warning shoudl be used for unexpected events in the application flow (Errors for example that don't make the application stop, but we must investigate in the future
             * LogLevel.Error should be used when the current flow of the application stop because of an unhandled error.
             * LogLevel.Critical should be used for any system crashes
             */
            loggerFactory.AddDebug(LogLevel.Information);

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                //Global Error handling for API
                app.UseExceptionHandler(appBuilder =>
                {
                    appBuilder.Run(async context =>
                    {
                        var exceptionhandlerFeature = context.Features.Get <IExceptionHandlerFeature>();
                        if (exceptionhandlerFeature != null)
                        {
                            var logger = loggerFactory.CreateLogger("Global exception logger");

                            //The first parameter is the code(or Id) of the error
                            logger.LogError(500, exceptionhandlerFeature.Error, exceptionhandlerFeature.Error.Message);
                        }

                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("An unexpected fault happened. Try again later.");
                    });
                });
            }

            app.UseCors(c =>
            {
                c.AllowAnyHeader();
                c.AllowAnyMethod();
                c.AllowAnyOrigin();
            });

            libraryContext.EnsureSeedDataForContext();

            app.UseHttpCacheHeaders();

            app.UseResponseCaching();

            app.UseMvc();
        }
Beispiel #20
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app,
                              IHostingEnvironment env,
                              ILoggerFactory loggerFactory,
                              LibraryContext libraryContext)
        {
            loggerFactory.AddConsole();
            loggerFactory.AddDebug(LogLevel.Information);

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler(appBuilder =>
                {
                    appBuilder.Run(async context =>
                    {
                        var exceptionHandlerFeature = context.Features.Get <IExceptionHandlerFeature>();

                        if (exceptionHandlerFeature != null)
                        {
                            var logger = loggerFactory.CreateLogger("Global exception logger");
                            logger.LogError(StatusCodes.Status500InternalServerError,
                                            exceptionHandlerFeature.Error,
                                            exceptionHandlerFeature.Error.Message);
                        }

                        context.Response.StatusCode = StatusCodes.Status500InternalServerError;
                        await context.Response.WriteAsync("An unexpected fault happened. Try again later.");
                    });
                });
            }

            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap <Author, AuthorDTO>()
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => $"{src.FirstName} {src.LastName}"))
                .ForMember(dest => dest.Age, opt => opt.MapFrom(src => src.DateOfBirth.GetCurrentAge()));

                cfg.CreateMap <CreateAuthorDTO, Author>();

                cfg.CreateMap <Book, BookDTO>();

                cfg.CreateMap <CreateBookDTO, Book>();

                cfg.CreateMap <UpdateBookDTO, Book>();

                cfg.CreateMap <Book, UpdateBookDTO>();
            });

            libraryContext.EnsureSeedDataForContext();

            app.UseMvc();
        }
Beispiel #21
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                              ILoggerFactory loggerFactory, LibraryContext libraryContext)
        {
            _env = env;
            loggerFactory.AddConsole();
            loggerFactory.AddDebug(LogLevel.Information);
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler(appBuilder =>
                {
                    appBuilder.Run(async context =>
                    {
                        var exceptionHandlerFeature = context.Features.Get <IExceptionHandlerFeature>();
                        if (exceptionHandlerFeature != null)
                        {
                            var logger = loggerFactory.CreateLogger("GlobalException");
                            logger.LogError(500, exceptionHandlerFeature.Error, exceptionHandlerFeature.Error.Message);
                        }

                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("error- DANZY");
                    });
                });
            }

            ////app.Run(async (context) =>
            ////{
            ////    await context.Response.WriteAsync("Hello World!");
            ////});

            AutoMapper.Mapper.Initialize(con =>
            {
                con.CreateMap <Author, AuthorDto>()
                .ForMember("Name", dest => dest.MapFrom(src => $"{src.FirstName} {src.LastName}"))
                .ForMember(dest => dest.Age, option => option.MapFrom(src => src.DateOfBirth.GetCurrentAge(src.DateOfDeath)));

                con.CreateMap <Book, BookDto>();

                con.CreateMap <AuthorCreationDto, Author>();
                con.CreateMap <AuthorCreationWithDateOfDeathDto, Author>();

                con.CreateMap <BookCreationDto, Book>();
                con.CreateMap <BookUpdationDto, Book>();
                con.CreateMap <Book, BookUpdationDto>();
            });


            libraryContext.EnsureSeedDataForContext();

            app.UseMvc();
        }
Beispiel #22
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env,
                              ILoggerFactory loggerFactory, LibraryContext libraryContext)
        {
            // Microsoft.Extensions.Logging.Debug
            // NLog.Extensions.Logging
            loggerFactory = LoggerFactory.Create(builder => {
                builder.AddConsole();
                builder.AddDebug();
                builder.AddNLog();
            });

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler(appBuider =>
                {
                    appBuider.Run(async context =>
                    {
                        /*context.Response.StatusCode = 500;
                         * await context.Response.WriteAsync("An unexpected fault happened. Try again later.");*/
                        var exceptionHandlerFeature = context.Features.Get <IExceptionHandlerFeature>();
                        if (exceptionHandlerFeature != null)
                        {
                            var logger = loggerFactory.CreateLogger("Global exception logger");
                            logger.LogError(500,
                                            exceptionHandlerFeature.Error,
                                            exceptionHandlerFeature.Error.Message);
                        }

                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("An unexpected fault happened. Try again later.");
                    });
                });
            }

            /*if (env.IsDevelopment())
             * {
             *  app.UseDeveloperExceptionPage();
             * }
             * else
             * {
             *  app.UseExceptionHandler();
             * }*/

            libraryContext.EnsureSeedDataForContext();

            app.UseIpRateLimiting();

            app.UseHttpCacheHeaders();

            app.UseMvc();
        }
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                              ILoggerFactory loggerFactory, LibraryContext libraryContext)
        {
            //loggerFactory.AddProvider(new NLog.Extensions.Logging.NLogLoggerProvider());
            loggerFactory.AddNLog();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler(appBuilder =>
                {
                    appBuilder.Run(async context =>
                    {
                        var exceptionHandlerFeature = context.Features.Get <IExceptionHandlerFeature>();
                        if (exceptionHandlerFeature != null)
                        {
                            var logger = loggerFactory.CreateLogger("Global exception logger");
                            logger.LogError(500, exceptionHandlerFeature.Error, exceptionHandlerFeature.Error.Message);
                        }
                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("An unexpected error occured. Try again later.");
                    });
                });
            }

            // create an auto mapper map from the author entity to the author DTO
            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap <Author, AuthorDto>()
                // map the first & last name to a name variable that concatenates them
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src =>
                                                                 $"{src.FirstName} {src.LastName}"))
                // map the authors DoB to just their current age
                .ForMember(dest => dest.Age, opt => opt.MapFrom(src =>
                                                                src.DateOfBirth.GetCurrentAge(src.DateOfDeath)));

                cfg.CreateMap <Book, BookDto>();
                cfg.CreateMap <AuthorForCreationDto, Author>();
                cfg.CreateMap <AuthorForCreationWithDateOfDeathDto, Author>();
                cfg.CreateMap <BookForCreationDto, Book>();
                cfg.CreateMap <BookForUpdateDto, Book>();
                cfg.CreateMap <Book, BookForUpdateDto>();
            });

            libraryContext.EnsureSeedDataForContext();

            app.UseMvc();
            app.UseSwagger();
            app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Library API v1"); });
        }
Beispiel #24
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                              ILoggerFactory loggerFactory, LibraryContext libraryContext)
        {
            //asp.netcore 1.x this also work for asp.netcore 2.x
            //loggerFactory.AddConsole().AddDebug(LogLevel.Information);

            //configuracion de Nlog para almacenar los loggers en un file XML
            //loggerFactory.AddNLog();


            Mapper.Initialize((config) =>
            {
                config.CreateMap <Author, AuthorToReturn>()
                .ForMember(t => t.Age, Ex => Ex.MapFrom(t => DateTimeOffsetExtensions.GetCurrentAge(t.DateOfBirth)))
                .ForMember(t => t.Name, Ex => Ex.MapFrom(t => $"{t.FirstName} {t.LastName}"));

                config.CreateMap <Book, BookToReturn>().ReverseMap();

                config.CreateMap <AuthorDtoCreating, Author>().ReverseMap();
                config.CreateMap <BookCreationDto, Book>().ReverseMap();
                config.CreateMap <BookUpdate, Book>().ReverseMap();
                config.CreateMap <AuthorUpdate, Author>().ReverseMap();
            });

            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("general logger");
                            logger.LogError(500, ExceptionHandler.Error,
                                            ExceptionHandler.Error.Message);
                        }

                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("this is a global exception define in the StartUp CLas only for production environments");
                    });
                });
            }

            libraryContext.EnsureSeedDataForContext();

            app.UseMvc();
        }
Beispiel #25
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory,
                              LibraryContext libraryContext)
        {
            //loggerFactory.AddConsole();
            //loggerFactory.AddDebug(LogLevel.Information);

            //loggerFactory.AddProvider(new NLog.Extensions.NLogLoggerProvider());
            //loggerFactory.AddNLog();
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler(appBuilder =>
                {
                    appBuilder.Run(async context =>
                    {
                        var exeptionHendlerFeature = context.Features.Get <IExceptionHandlerFeature>();
                        if (exeptionHendlerFeature != null)
                        {
                            var logger = loggerFactory.CreateLogger("Global exeption logger");
                            logger.LogError(500,
                                            exeptionHendlerFeature.Error,
                                            exeptionHendlerFeature.Error.Message);
                        }
                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("An unexpected fault happend.Try again later..");
                    });
                });
            }

            // init Database
            libraryContext.EnsureSeedDataForContext();



            app.UseIpRateLimiting();

            app.UseHttpCacheHeaders();

            //app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
Beispiel #26
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, LibraryContext libraryContext)
        {
            //.Net core 1.1 approach
            //loggerFactory.AddProvider(new NLog.Extensions.Logging.NLogLoggerProvider());
            //loggerFactory.AddNLog();


            if (env.EnvironmentName.Equals("development", System.StringComparison.OrdinalIgnoreCase))
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseHttpsRedirection();


            AutoMapper.Mapper.Initialize(cfg =>
            {
                cfg.CreateMap <Entities.Author, Dtos.Author>()
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => $"{src.FirstName} {src.LastName}"))
                .ForMember(dest => dest.Age, opt => opt.MapFrom(src =>
                                                                src.DateOfBirth.GetCurrentAge(src.DateOfDeath)));

                cfg.CreateMap <Entities.Book, Dtos.Book>();

                cfg.CreateMap <CreateAuthor, Entities.Author>();
                cfg.CreateMap <CreateDeadAuthor, Entities.Author>();

                cfg.CreateMap <CreateBook, Entities.Book>();
                cfg.CreateMap <UpdateBook, Entities.Book>();
                cfg.CreateMap <Entities.Book, UpdateBook>();
            });

            app.UseStaticFiles();
            app.UseRouting();
            app.UseCors();
            libraryContext.EnsureSeedDataForContext();

            app.UseIpRateLimiting();

            app.UseResponseCaching();

            app.UseHttpCacheHeaders();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}");
            });
        }
Beispiel #27
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,
            ILoggerFactory loggerFactory, LibraryContext libraryContext)
        {
            // if (env.IsDevelopment())
            // {
            //     app.UseDeveloperExceptionPage();
            // }
            // else
            // {
                app.UseExceptionHandler(appBuilder => 
                {
                    appBuilder.Run(async context =>
                    {
                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("An unexpected fault happened. Try again later.");
                    });
                });
           // }

            AutoMapper.Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<Entities.Author, Models.AuthorDto>()
                    .ForMember(dest => dest.Name, opt => opt.MapFrom(src =>
                    $"{src.FirstName} {src.LastName}"))
                    .ForMember(dest => dest.Age, opt => opt.MapFrom(src =>
                    src.DateOfBirth.GetCurrentAge()));

                cfg.CreateMap<Book, BookDto>();

                cfg.CreateMap<AuthorForCreationDto, Author>();

                cfg.CreateMap<BookForCreationDto, Book>();
                cfg.CreateMap<BookForUpdateDto, Book>();


            });

            libraryContext.EnsureSeedDataForContext();

            // Enable middleware to serve generated Swagger as a JSON endpoint.
            app.UseSwagger();

            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            });

            app.UseMvc();
        }
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, LibraryContext libraryContext)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();


            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                //TODO : 13 - Manejo la exception a nivel global
                //Cambio  "ASPNETCORE_ENVIRONMENT": "Development" por >>  "ASPNETCORE_ENVIRONMENT": "Production"
                //app.UseExceptionHandler(appBuilder =>
                //{
                //    appBuilder.Run(async context =>
                //    {
                //        context.Response.StatusCode = 500;
                //        await context.Response.WriteAsync("Ocurrio un error. Intente nuevamente o ponganse en contacto con [email protected]");

                //    });
                //});
            }


            //TODO : 08 - Instalo automapper y realizo las configuraciones de mapeo. Utilizamos proyección para el mapeo de la edad
            //Install-Package AutoMapper
            //AutoMapper.Mapper.Initialize(cfg =>
            //{
            //    cfg.CreateMap<Author, AuthorDto>()
            //        .ForMember(dest => dest.Name, opt => opt.MapFrom(src =>
            //        $"{src.FirstName} {src.LastName}"))
            //        .ForMember(dest => dest.Age, opt => opt.MapFrom(src =>
            //        src.DateOfBirth.GetCurrentAge()));

            //    //TODO : 17 - Configuro el automapper
            //    //cfg.CreateMap<book, BookDto>();
            //});
            app.UseSwagger();

            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            });

            libraryContext.EnsureSeedDataForContext();

            app.UseMvc();
        }
Beispiel #29
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                              ILoggerFactory loggerFactory, LibraryContext libraryContext)
        {
            loggerFactory.AddConsole();

            loggerFactory.AddDebug(LogLevel.Information);
            //loggerFactory.AddProvider(new NLog.Extensions.Logging.NLogLoggerProvider()); //Expects an instance of a class that implements ILogger provider.
            loggerFactory.AddNLog(); //Add Nlogger

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage(); //Leave as is during development
            }
            else
            {
                app.UseExceptionHandler(appBuilder =>
                {
                    appBuilder.Run(async context =>
                    {
                        //We get a collection of http features provided by the server and middleware, available in the request.
                        IExceptionHandlerFeature exception = context.Features.Get <IExceptionHandlerFeature>(); //This will let us look at the error property to look at the actual exception
                        if (exception != null)
                        {
                            ILogger log = loggerFactory.CreateLogger("Global exception logger"); //Creating a logger
                            log.LogError(500, exception.Error, exception.Error.Message);         //Logging the exception
                        }
                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("An unexpecte fault happened. Try again later.");
                    });
                }); //Used in a production environment.
            }
            AutoMapper.Mapper.Initialize(cfg =>
            {
                //Author is the source and AuthorDto is the destination
                //Convention based it will map property names on source to same named property on destination. If property doesnt exist in destination then it is ignored
                cfg.CreateMap <Entities.Author, Models.AuthorDto>()
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => $"{src.FirstName} {src.LastName}"))
                .ForMember(dest => dest.Age, opt => opt.MapFrom(src => src.DateOfBirth.GetCurrentAge())); //Projecting since we have name and age in AuthorDto but not in Author
                cfg.CreateMap <Entities.Book, Models.BookDto>();
                cfg.CreateMap <Models.AuthorForCreationDto, Entities.Author>();
                cfg.CreateMap <Models.BooksForCreationDto, Entities.Author>();
                cfg.CreateMap <Models.BookForUpdateDto, Entities.Book>();
                cfg.CreateMap <Entities.Author, Models.BookForUpdateDto>(); //<source, dest>, source to dest
            });

            libraryContext.EnsureSeedDataForContext();

            app.UseMvc();
        }
Beispiel #30
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                              ILoggerFactory loggerFactory, LibraryContext libraryContext)
        {
            //loggerFactory.AddConsole();

            //loggerFactory.AddDebug(LogLevel.Information);

            // loggerFactory.AddProvider(new NLog.Extensions.Logging.NLogLoggerProvider());
            // loggerFactory.AddNLog();

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

                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("An unexpected fault happend. Try again later.");
                    });
                });

                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            libraryContext.EnsureSeedDataForContext();

            //app.UseIpRateLimiting();
            // app.UseClientRateLimiting();

            app.UseResponseCaching();
            app.UseHttpCacheHeaders();

            app.UseHttpsRedirection();
            app.UseMvc();
        }