Skip to content

Commit ad55880

Browse files
committed
Startup and middleware: using IApplicationBuilder
1 parent ad091de commit ad55880

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

OdeToFoodRider/OdeToFoodRider/Startup.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.AspNetCore.Http;
88
using Microsoft.Extensions.Configuration;
99
using Microsoft.Extensions.DependencyInjection;
10+
using Microsoft.Extensions.Logging;
1011

1112
namespace OdeToFoodRider
1213
{
@@ -21,13 +22,39 @@ public void ConfigureServices(IServiceCollection services)
2122
}
2223

2324
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
24-
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IGreeter greeter)
25+
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IGreeter greeter, ILogger<Startup> logger)
2526
{
26-
if (env.IsDevelopment())
27+
// if (env.IsDevelopment())
28+
// {
29+
// app.UseDeveloperExceptionPage();
30+
// }
31+
32+
app.Use(next =>
2733
{
28-
app.UseDeveloperExceptionPage();
29-
}
34+
// RDVS: Rider's completion doesn't expect an identifier after the async keyword, so it starts to suggest weird classes,
35+
// and this is where completion on space hurts: I wanted to type "context" but completion triggered on Space and inserted the unwanted ContextBoundObject
36+
return async context =>
37+
{
38+
// RDVS: again bad completion on unresolved symbol: wanted to use undeclared "logger" to add it as parameter later, but got Logger<> completed instead
39+
logger.LogInformation("Request incoming");
40+
if (context.Request.Path.StartsWithSegments("/mym"))
41+
{
42+
await context.Response.WriteAsync("Hit!!!");
43+
logger.LogInformation("Request handled");
44+
}
45+
else
46+
{
47+
await next(context);
48+
logger.LogInformation("Response outgoing");
49+
}
50+
};
51+
});
3052

53+
app.UseWelcomePage(new WelcomePageOptions()
54+
{
55+
Path = "/wp"
56+
});
57+
3158
app.Run(async (context) =>
3259
{
3360
var greeting = greeter.GetMessageOfTheDay();

OdeToFoodVisualStudio/OdeToFoodVisualStudio/Startup.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.AspNetCore.Http;
44
using Microsoft.Extensions.Configuration;
55
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Logging;
67

78
namespace OdeToFoodVisualStudio
89
{
@@ -18,13 +19,38 @@ public void ConfigureServices(IServiceCollection services)
1819
}
1920

2021
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
21-
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IGreeter greeter)
22+
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IGreeter greeter, ILogger<Startup> logger)
2223
{
23-
if (env.IsDevelopment())
24+
//if (env.IsDevelopment())
25+
//{
26+
// app.UseDeveloperExceptionPage();
27+
//}
28+
29+
app.Use(next =>
2430
{
25-
app.UseDeveloperExceptionPage();
26-
}
31+
return async context =>
32+
{
33+
logger.LogInformation("Request incoming");
34+
if(context.Request.Path.StartsWithSegments("/mym"))
35+
{
36+
// RDVS: VS completion breaks down after the await keyword - no longer suggests anything. Workaround: type a sync statement first, then add the "await" keyword
37+
// Rider handles this just fine.
38+
await context.Response.WriteAsync("Hit!!!");
39+
logger.LogInformation("Request handled");
40+
}
41+
else
42+
{
43+
await next(context);
44+
logger.LogInformation("Response outgoing");
45+
}
46+
47+
};
48+
});
2749

50+
app.UseWelcomePage(new WelcomePageOptions {
51+
Path = "/wp"
52+
});
53+
2854
app.Run(async (context) =>
2955
{
3056
// 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)

0 commit comments

Comments
 (0)