From ad558809904c44af3a7f2aa3242958896c805c30 Mon Sep 17 00:00:00 2001 From: gorohoroh Date: Tue, 3 Jul 2018 17:14:00 +0300 Subject: [PATCH] Startup and middleware: using IApplicationBuilder --- OdeToFoodRider/OdeToFoodRider/Startup.cs | 35 ++++++++++++++++--- .../OdeToFoodVisualStudio/Startup.cs | 34 +++++++++++++++--- 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/OdeToFoodRider/OdeToFoodRider/Startup.cs b/OdeToFoodRider/OdeToFoodRider/Startup.cs index 2b23fdd..270418b 100644 --- a/OdeToFoodRider/OdeToFoodRider/Startup.cs +++ b/OdeToFoodRider/OdeToFoodRider/Startup.cs @@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; namespace OdeToFoodRider { @@ -21,13 +22,39 @@ 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) + public void Configure(IApplicationBuilder app, IHostingEnvironment env, IGreeter greeter, ILogger logger) { - if (env.IsDevelopment()) +// if (env.IsDevelopment()) +// { +// app.UseDeveloperExceptionPage(); +// } + + app.Use(next => { - app.UseDeveloperExceptionPage(); - } + // 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(); diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Startup.cs b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Startup.cs index bcdd24f..e0993b9 100644 --- a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Startup.cs +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Startup.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; namespace OdeToFoodVisualStudio { @@ -18,13 +19,38 @@ 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) + public void Configure(IApplicationBuilder app, IHostingEnvironment env, IGreeter greeter, ILogger logger) { - if (env.IsDevelopment()) + //if (env.IsDevelopment()) + //{ + // app.UseDeveloperExceptionPage(); + //} + + app.Use(next => { - app.UseDeveloperExceptionPage(); - } + 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.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)