Skip to content

Commit 032855d

Browse files
committed
Showing exception details + middleware to match the environment
1 parent ad55880 commit 032855d

File tree

5 files changed

+25
-59
lines changed

5 files changed

+25
-59
lines changed

OdeToFoodRider/OdeToFoodRider/Startup.cs

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,17 @@ public void ConfigureServices(IServiceCollection services)
2424
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
2525
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IGreeter greeter, ILogger<Startup> logger)
2626
{
27-
// if (env.IsDevelopment())
28-
// {
29-
// app.UseDeveloperExceptionPage();
30-
// }
27+
if (env.IsDevelopment())
28+
{
29+
app.UseDeveloperExceptionPage();
30+
}
3131

32-
app.Use(next =>
33-
{
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-
});
52-
53-
app.UseWelcomePage(new WelcomePageOptions()
54-
{
55-
Path = "/wp"
56-
});
5732

5833
app.Run(async (context) =>
5934
{
6035
var greeting = greeter.GetMessageOfTheDay();
61-
await context.Response.WriteAsync(greeting);
36+
// RDVS: wanted to use string interpolation for the WriteAsync() argument but a context action is missing :(
37+
await context.Response.WriteAsync($"{greeting}: {env.EnvironmentName}");
6238
});
6339
}
6440
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"Greeting": "Hello from Rider's Development appsettings",
3+
"ConnectionStrings": {
4+
"DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;MultipleActiveResultSets=true"
5+
}
6+
7+
}

OdeToFoodVisualStudio/OdeToFoodVisualStudio/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"commandName": "Project",
2020
"launchBrowser": true,
2121
"environmentVariables": {
22-
"ASPNETCORE_ENVIRONMENT": "Development"
22+
"ASPNETCORE_ENVIRONMENT": "Production"
2323
},
2424
"applicationUrl": "http://localhost:54449/"
2525
}

OdeToFoodVisualStudio/OdeToFoodVisualStudio/Startup.cs

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.Extensions.Configuration;
55
using Microsoft.Extensions.DependencyInjection;
66
using Microsoft.Extensions.Logging;
7+
using System;
78

89
namespace OdeToFoodVisualStudio
910
{
@@ -21,36 +22,12 @@ public void ConfigureServices(IServiceCollection services)
2122
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
2223
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IGreeter greeter, ILogger<Startup> logger)
2324
{
24-
//if (env.IsDevelopment())
25-
//{
26-
// app.UseDeveloperExceptionPage();
27-
//}
28-
29-
app.Use(next =>
25+
if (env.IsDevelopment())
3026
{
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-
}
27+
app.UseDeveloperExceptionPage();
28+
}
4629

47-
};
48-
});
4930

50-
app.UseWelcomePage(new WelcomePageOptions {
51-
Path = "/wp"
52-
});
53-
5431
app.Run(async (context) =>
5532
{
5633
// 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
6340

6441
var greeting = greeter.GetMessageOfTheDay();
6542

66-
await context.Response.WriteAsync(greeting);
43+
await context.Response.WriteAsync($"{greeting}: {env.EnvironmentName}");
6744
});
6845
}
6946
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"Greeting": "Hello from Visual Studio's Development appsettings",
3+
"ConnectionStrings": {
4+
"DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;MultipleActiveResultSets=true"
5+
}
6+
}

0 commit comments

Comments
 (0)