Skip to content

Commit

Permalink
Showing exception details + middleware to match the environment
Browse files Browse the repository at this point in the history
  • Loading branch information
gorohoroh committed Jul 3, 2018
1 parent ad55880 commit 032855d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 59 deletions.
36 changes: 6 additions & 30 deletions OdeToFoodRider/OdeToFoodRider/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Startup> 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}");
});
}
}
Expand Down
7 changes: 7 additions & 0 deletions OdeToFoodRider/OdeToFoodRider/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Greeting": "Hello from Rider's Development appsettings",
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;MultipleActiveResultSets=true"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
"ASPNETCORE_ENVIRONMENT": "Production"
},
"applicationUrl": "http://localhost:54449/"
}
Expand Down
33 changes: 5 additions & 28 deletions OdeToFoodVisualStudio/OdeToFoodVisualStudio/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;

namespace OdeToFoodVisualStudio
{
Expand All @@ -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<Startup> 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)
Expand All @@ -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}");
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
}

0 comments on commit 032855d

Please sign in to comment.