Skip to content

Commit

Permalink
Merge pull request #7 from christianacca/cc/net6-full
Browse files Browse the repository at this point in the history
Full net6 upgrade
  • Loading branch information
christianacca authored Nov 18, 2023
2 parents 2bafc48 + 1f18932 commit a7e8e7d
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 158 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Data;
using Microsoft.AspNetCore.Http;
using System.Data;
using Microsoft.AspNetCore.Mvc;
using ProblemDetailsDemo.Api.ExampleMiddleware;

Expand Down
4 changes: 1 addition & 3 deletions src/ProblemDetailsDemo.Api/Controllers/ApiMvcController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using CcAcca.LogDimensionCollection.AspNetCore;
using CcAcca.LogDimensionCollection.AspNetCore;
using Hellang.Middleware.ProblemDetails;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProblemDetailsDemo.Api.Models;
using ProblemDetailsDemo.Api.MvcCustomizations;
Expand Down
10 changes: 5 additions & 5 deletions src/ProblemDetailsDemo.Api/ProblemDetailsDemo.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IncludeOpenAPIAnalyzers>true</IncludeOpenAPIAnalyzers>
<NoWarn>1701;1702;1591</NoWarn>
<UserSecretsId>9db4e509-61e7-4301-9bbc-15c904c824f3</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CcAcca.LogDimensionCollection.AppInsights" Version="3.0.0-beta.2" />
<PackageReference Include="CcAcca.LogDimensionCollection.AspNetCore" Version="3.0.0-beta.2" />
<PackageReference Include="Hellang.Middleware.ProblemDetails" Version="6.4.0" />
<PackageReference Include="CcAcca.LogDimensionCollection.AppInsights" Version="3.0.0" />
<PackageReference Include="CcAcca.LogDimensionCollection.AspNetCore" Version="3.0.0" />
<PackageReference Include="Hellang.Middleware.ProblemDetails" Version="6.5.1" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
<PackageReference Include="CcAcca.ApplicationInsights.ProblemDetails" Version="2.0.0-beta.2" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.16" />
<PackageReference Include="CcAcca.ApplicationInsights.ProblemDetails" Version="2.0.0" />
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="2.1.175" />
<PackageReference Include="NSwag.AspNetCore" Version="13.20.0" />
</ItemGroup>
Expand Down
143 changes: 128 additions & 15 deletions src/ProblemDetailsDemo.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,135 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using System.Data;
using CcAcca.ApplicationInsights.ProblemDetails;
using CcAcca.LogDimensionCollection.AppInsights;
using CcAcca.LogDimensionCollection.AspNetCore;
using Hellang.Middleware.ProblemDetails;
using Hellang.Middleware.ProblemDetails.Mvc;
using Microsoft.AspNetCore.Mvc;
using ProblemDetailsDemo.Api.ExampleMiddleware;
using ProblemDetailsDemo.Api.MvcCustomizations;

namespace ProblemDetailsDemo.Api
// ReSharper disable VariableHidesOuterVariable

[assembly: ApiConventionType(typeof(DefaultApiConventions))]

const string uiRoutePrefix = "ui";

var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
public class Program
{
public static void Main(string[] args)
Args = args,
// Normally set by DOTNET_ENVIRONMENT env var; we set it explicitly to Development for demo purposes so as
// force ProblemDetails middleware to always add exception details the responses.
// YOUR APP should remove this line so that ProblemDetails middleware only adds exception details to responses
// when running locally where DOTNET_ENVIRONMENT env var is typically set to Development
EnvironmentName = Environments.Development
});

ConfigureServices(builder.Services);

var app = builder.Build();

ConfigureMiddleware(app);

app.Run();


void ConfigureServices(IServiceCollection services)
{
services.AddHttpContextAccessor();

// Configure Hellang.Middleware.ProblemDetails package and tune ASP.Net Core MVC to line up with it's conventions
services
.AddProblemDetails(o => {
o.ValidationProblemStatusCode = StatusCodes.Status400BadRequest;
o.MapToStatusCode<DBConcurrencyException>(StatusCodes.Status409Conflict);
o.MapToStatusCode<NotImplementedException>(StatusCodes.Status501NotImplemented);
})
.AddControllersWithViews(o => {
// optional tweak to built-in mvc http responses:
o.Conventions.Add(new NotFoundResultApiConvention());
})
.AddProblemDetailsConventions();

// configuration for CcAcca.LogDimensionCollection.AspNetCore package...
services
.AddMvcActionDimensionCollection()
.AddMvcResultItemsCountDimensionSelector()
.AddMvcActionArgDimensionSelector()
.ConfigureActionArgDimensionSelector(options => {
// note: use with extreme caution and probably never for production workloads!
options.AutoCollect = true;
});

services.AddSwaggerDocument(
configure => {
configure.PostProcess = document => {
document.Info.Version = "v1";
document.Info.Title = "ProblemDetailsDemo";
document.Info.Description = "ASP.NET Core 3.1 Problem Details demo";
};
});

// configuration for CcAcca.ApplicationInsights.ProblemDetails package...
services
.AddApplicationInsightsTelemetry()
// enrich request telemetry with ProblemDetails
.AddProblemDetailTelemetryInitializer(o => {
// for demo purposes we're commenting this line out so that 4xx responses show in the failures pane in
// app insights azure service.
// YOUR APP will want to uncomment this line to ensure 4xx responses are not considered failures
// o.IsFailure = ProblemDetailsTelemetryOptions.ServerErrorIsFailure;
})
.AddMvcActionDimensionTelemetryInitializer();
}

void ConfigureMiddleware(IApplicationBuilder app)
{
// apply different exception middleware pipeline depending on whether the request is for the UI or API
app.UseIfElse(IsUIRequest, UIExceptionMiddleware, NonUIExceptionMiddleware);

app.UseOpenApi();
app.UseSwaggerUi3();

app.UseStaticFiles();

app.UseRouting();

// demo a middleware throwing exceptions or setting StatusCode
// try browsing to:
// - middleware/error
// - middleware/status/nnn (replacing nnn with a http status code eg 501)
app.UseMiddleware<MaybeBadMiddleware>();

app.UseEndpoints(endpoints => {
endpoints.MapControllers();
endpoints.MapControllerRoute("home", "", new { controller = "Home", action = "Index" });
endpoints.MapControllerRoute("default", uiRoutePrefix + "/{controller=Bad}/{action=Index}/{id?}");
});
}

void NonUIExceptionMiddleware(IApplicationBuilder app)
{
// Hellang.Middleware.ProblemDetails
app.UseProblemDetails();
}

void UIExceptionMiddleware(IApplicationBuilder app)
{
if (builder.Environment.IsDevelopment())
{
CreateWebHostBuilder(args).Build().Run();
app.UseDeveloperExceptionPage();
}

public static IHostBuilder CreateWebHostBuilder(string[] args)
else
{
return Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => webBuilder
.UseStartup<Startup>()
// .UseEnvironment(Environments.Production) // Uncomment to remove exception details from responses.
);
app.UseExceptionHandler("/ui/home/error");
}
}

app.UseStatusCodePages();
}

bool IsUIRequest(HttpContext httpContext)
{
var requestPath = httpContext.Request.Path;
return requestPath == "/" ||
requestPath.StartsWithSegments($"/{uiRoutePrefix}", StringComparison.OrdinalIgnoreCase);
}
132 changes: 0 additions & 132 deletions src/ProblemDetailsDemo.Api/Startup.cs

This file was deleted.

0 comments on commit a7e8e7d

Please sign in to comment.