Skip to content

Commit

Permalink
Startup and middleware: using IApplicationBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
gorohoroh committed Jul 3, 2018
1 parent ad091de commit ad55880
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
35 changes: 31 additions & 4 deletions OdeToFoodRider/OdeToFoodRider/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace OdeToFoodRider
{
Expand All @@ -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<Startup> 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();
Expand Down
34 changes: 30 additions & 4 deletions OdeToFoodVisualStudio/OdeToFoodVisualStudio/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace OdeToFoodVisualStudio
{
Expand All @@ -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<Startup> 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)
Expand Down

0 comments on commit ad55880

Please sign in to comment.