Exemple #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
            {
                // 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, Library.API.Models.AuthorDetails>()
                .ForMember(dest => dest.Name, Opt => Opt.MapFrom(src =>
                                                                 $"{src.FirstName} {src.LastName}"))
                .ForMember(dest => dest.Age, Opt => Opt.MapFrom(src =>
                                                                DateTimeOffsetExtensions.GetCurrentAge(src.DateOfBirth)));
            });

            app.UseHttpsRedirection();
            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)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler(appBuilder =>
                {
                    appBuilder.Run(async context =>
                    {
                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("An unespected fault happend. 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 <Book, BookDto>();
                cfg.CreateMap <AuthorForCreationDto, Author>();
                cfg.CreateMap <BookForCreationDto, Book>();
            });

            libraryContext.EnsureSeedDataForContext();

            app.UseMvc();
        }
Exemple #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)
        {
            loggerFactory.AddDebug(LogLevel.Information);

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {   //Global exception handling
                app.UseExceptionHandler(appBuilder =>
                {
                    appBuilder.Run(async context =>
                    {
                        //para conseguir apanhar o erro antes de enviar a mensagem de erro genérica
                        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 <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.AuthorForCreationDto, Author>();

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

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

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

            libraryContext.EnsureSeedDataForContext();

            app.UseStatusCodePages();
            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)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler();
            }

            libraryContext.EnsureSeedDataForContext();

            app.UseMvc();
        }
Exemple #5
0
        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("Fault happened");
                    });
                });
            }

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

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

            libraryContext.EnsureSeedDataForContext();
            app.UseResponseCaching();
            app.UseHttpCacheHeaders();
            app.UseMvc();
            app.UseSwag();
        }