Ejemplo n.º 1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app)
        {
            app.UseExceptionHandler(new ExceptionHandlerOptions()
            {
                ExceptionHandler = async context =>
                {
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;

                    var ex = context.Features.Get <IExceptionHandlerFeature>()?.Error;
                    if (ex == null)
                    {
                        return;
                    }

                    var error = new
                    {
                        message = ex.Message,
                        detail  = ex.StackTrace
                    };

                    context.Response.ContentType = "application/json";

                    using (var writer = new StreamWriter(context.Response.Body))
                    {
                        new JsonSerializer().Serialize(writer, error);
                        await writer.FlushAsync().ConfigureAwait(false);
                    }
                }
            });


            using (var scope = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                var provider = scope.ServiceProvider;
                using (var dbContext = provider.GetRequiredService <DataContext>())
                {
                    dbContext.Seed();
                }
            }


            app.UseCors(builder => builder.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod().AllowCredentials());

            JwtConfig.Configure(app, this.AuthConfiguration);

            app.UseStaticFiles();
            app.UseMvc();

            //Configure Swagger
            SwaggerConfig.Configure(app, this.SwaggerConfiguration, this.AuthConfiguration);

            //Configure ServiceBusQueue
            if (this.ServiceBusQueueConfiguration?.IsEnabled == true)
            {
                ServiceBusQueueConfig.Configure(app);
            }
        }
Ejemplo n.º 2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            // global error handler
            app.UseMiddleware <ErrorHandlingMiddleware>();

            app.UseHttpsRedirection();

            JwtConfig.Configure(app, this.AuthConfiguration);

            // Configure Swagger
            SwaggerConfig.Configure(app, this.SwaggerConfiguration, this.AuthConfiguration, this.ApiMetadata);

            // CORS
            app.UseCors(builder => builder
                        .AllowAnyOrigin()
                        .AllowAnyMethod()
                        .AllowAnyHeader());

            app.UseRouting();

            if (this.AuthConfiguration?.IsEnabled == true)
            {
                app.UseAuthentication();
                app.UseAuthorization();

                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers()
                    .RequireAuthorization();
                });
            }
            else
            {
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                });
            }
        }