Exemple #1
0
 public void Init()
 {
     AutoMapper.Mapper.Reset();
     AutoMapperForFittifyApi.Initialize();
 }
Exemple #2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public virtual void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            if (env.IsDevelopment() || env.IsTestInMemoryDb())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler(appBuilder =>
                {
                    appBuilder.Run(async httpContext =>
                    {
                        // Required for logging exceptions when it is not caught later in code, see plural sight api course by
                        // https://app.pluralsight.com/player?course=asp-dot-net-core-restful-api-building&author=kevin-dockx&name=asp-dot-net-core-restful-api-building-m2&clip=12&mode=live
                        // https://app.pluralsight.com/player?course=asp-dot-net-core-restful-api-building&author=kevin-dockx&name=asp-dot-net-core-restful-api-building-m5&clip=5&mode=live
                        // log2file https://app.pluralsight.com/player?course=asp-dot-net-core-restful-api-building&author=kevin-dockx&name=asp-dot-net-core-restful-api-building-m5&clip=7&mode=live
                        var exceptionHandlerFeature = httpContext.Features.Get <IExceptionHandlerFeature>();
                        if (exceptionHandlerFeature != null)
                        {
                            var logger = loggerFactory.CreateLogger("Global exception logger");
                            logger.LogError(500, exceptionHandlerFeature.Error, exceptionHandlerFeature.Error.Message);
                        }

                        httpContext.Response.StatusCode = 500;
                        await httpContext.Response.WriteAsync("An unexpected error happened. Please try again later.");
                    });
                });
            }

            app.UseFittifyHeaderValidation(Configuration);
            app.EnsureFittifyHeaderDefaultValues(Configuration);

            loggerFactory.AddDebug(LogLevel.Debug);

            // Allows cross domain api consumption
            app.UseCors(builder =>
                        builder.WithOrigins("http://localhost:4200")
                        .AllowAnyHeader()
                        );

            AutoMapperForFittifyApi.Initialize();

            Debug.Write(string.Format("Creating a foo: {0}", JsonConvert.SerializeObject(new WeightLiftingSet())));

            if (!HostingEnvironment.IsDevelopment() && !HostingEnvironment.IsTestInMemoryDb())
            {
                app.UseIpRateLimiting();
            }

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

            app.UseAuthentication();

            app.UseMvc();

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

            if (env.IsDevelopment())
            {
                app.Run(async(context) =>
                {
                    await context.Response.WriteAsync("<h1>Environment Development</h1>");
                });
            }
            if (env.IsProduction())
            {
                app.Run(async(context) =>
                {
                    await context.Response.WriteAsync("<h1>Environment Production</h1>");
                });
            }
            if (env.IsTestInMemoryDb())
            {
                app.Run(async(context) =>
                {
                    await context.Response.WriteAsync("<h1>Environment TestInMemoryDb</h1>");
                });
            }
        }