From 032855d2ac63c7bdca103045f5da2b01cda12405 Mon Sep 17 00:00:00 2001 From: gorohoroh Date: Tue, 3 Jul 2018 18:17:14 +0300 Subject: [PATCH] Showing exception details + middleware to match the environment --- OdeToFoodRider/OdeToFoodRider/Startup.cs | 36 ++++--------------- .../appsettings.Development.json | 7 ++++ .../Properties/launchSettings.json | 2 +- .../OdeToFoodVisualStudio/Startup.cs | 33 +++-------------- .../appsettings.Development.json | 6 ++++ 5 files changed, 25 insertions(+), 59 deletions(-) create mode 100644 OdeToFoodRider/OdeToFoodRider/appsettings.Development.json create mode 100644 OdeToFoodVisualStudio/OdeToFoodVisualStudio/appsettings.Development.json diff --git a/OdeToFoodRider/OdeToFoodRider/Startup.cs b/OdeToFoodRider/OdeToFoodRider/Startup.cs index 270418b..36b66d9 100644 --- a/OdeToFoodRider/OdeToFoodRider/Startup.cs +++ b/OdeToFoodRider/OdeToFoodRider/Startup.cs @@ -24,41 +24,17 @@ public void ConfigureServices(IServiceCollection services) // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, IGreeter greeter, ILogger logger) { -// if (env.IsDevelopment()) -// { -// app.UseDeveloperExceptionPage(); -// } + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } - app.Use(next => - { - // RDVS: Rider's completion doesn't expect an identifier after the async keyword, so it starts to suggest weird classes, - // and this is where completion on space hurts: I wanted to type "context" but completion triggered on Space and inserted the unwanted ContextBoundObject - return async context => - { - // RDVS: again bad completion on unresolved symbol: wanted to use undeclared "logger" to add it as parameter later, but got Logger<> completed instead - logger.LogInformation("Request incoming"); - if (context.Request.Path.StartsWithSegments("/mym")) - { - await context.Response.WriteAsync("Hit!!!"); - logger.LogInformation("Request handled"); - } - else - { - await next(context); - logger.LogInformation("Response outgoing"); - } - }; - }); - - app.UseWelcomePage(new WelcomePageOptions() - { - Path = "/wp" - }); app.Run(async (context) => { var greeting = greeter.GetMessageOfTheDay(); - await context.Response.WriteAsync(greeting); + // RDVS: wanted to use string interpolation for the WriteAsync() argument but a context action is missing :( + await context.Response.WriteAsync($"{greeting}: {env.EnvironmentName}"); }); } } diff --git a/OdeToFoodRider/OdeToFoodRider/appsettings.Development.json b/OdeToFoodRider/OdeToFoodRider/appsettings.Development.json new file mode 100644 index 0000000..e45997d --- /dev/null +++ b/OdeToFoodRider/OdeToFoodRider/appsettings.Development.json @@ -0,0 +1,7 @@ +{ + "Greeting": "Hello from Rider's Development appsettings", + "ConnectionStrings": { + "DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;MultipleActiveResultSets=true" + } + +} \ No newline at end of file diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Properties/launchSettings.json b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Properties/launchSettings.json index 39f9009..695efc0 100644 --- a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Properties/launchSettings.json +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Properties/launchSettings.json @@ -19,7 +19,7 @@ "commandName": "Project", "launchBrowser": true, "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" + "ASPNETCORE_ENVIRONMENT": "Production" }, "applicationUrl": "http://localhost:54449/" } diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Startup.cs b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Startup.cs index e0993b9..e44967a 100644 --- a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Startup.cs +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Startup.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using System; namespace OdeToFoodVisualStudio { @@ -21,36 +22,12 @@ public void ConfigureServices(IServiceCollection services) // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, IGreeter greeter, ILogger logger) { - //if (env.IsDevelopment()) - //{ - // app.UseDeveloperExceptionPage(); - //} - - app.Use(next => + if (env.IsDevelopment()) { - return async context => - { - logger.LogInformation("Request incoming"); - if(context.Request.Path.StartsWithSegments("/mym")) - { - // RDVS: VS completion breaks down after the await keyword - no longer suggests anything. Workaround: type a sync statement first, then add the "await" keyword - // Rider handles this just fine. - await context.Response.WriteAsync("Hit!!!"); - logger.LogInformation("Request handled"); - } - else - { - await next(context); - logger.LogInformation("Response outgoing"); - } + app.UseDeveloperExceptionPage(); + } - }; - }); - app.UseWelcomePage(new WelcomePageOptions { - Path = "/wp" - }); - app.Run(async (context) => { // Configuration sources by descending priority: 1. command-line parameter, 2. environment variable, 3. appsettings.json (enables storing dev settings in appsettings.json and overriding them in production wtih environment variables for example) @@ -63,7 +40,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, IGreeter var greeting = greeter.GetMessageOfTheDay(); - await context.Response.WriteAsync(greeting); + await context.Response.WriteAsync($"{greeting}: {env.EnvironmentName}"); }); } } diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/appsettings.Development.json b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/appsettings.Development.json new file mode 100644 index 0000000..b4cd0bc --- /dev/null +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/appsettings.Development.json @@ -0,0 +1,6 @@ +{ + "Greeting": "Hello from Visual Studio's Development appsettings", + "ConnectionStrings": { + "DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;MultipleActiveResultSets=true" + } +} \ No newline at end of file