Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
seungyongshim committed Feb 18, 2024
1 parent 503733d commit 130d8f0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
```csharp
var builder = WebApplication.CreateBuilder(args);

builder.Services.Configure<RouteHandlerOptions>(o => o.ThrowOnBadRequest = true);
builder.Services.AddExceptionHandler<BadRequestExceptionHandler>();
builder.Services.AddExceptionHandler<GlobalExceptionHandler>();
builder.Services.AddProblemDetails();
builder.Services.AddProblemDetails(options =>
options.CustomizeProblemDetails = context =>
context.ProblemDetails.Extensions["traceId"] =
Activity.Current?.Id ?? context.HttpContext.TraceIdentifier);

var app = builder.Build();

Expand All @@ -23,7 +28,10 @@ app.UseExceptionHandler();
* https://github.com/Carl-Hugo/FluentValidation.AspNetCore.Http
* `dotnet add package ForEvolve.FluentValidation.AspNetCore.Http`
```csharp
var root = app.MapGroup("/")
.AddFluentValidationFilter();

root.MapPost(...)
```

## Swagger Request Exapmle 작성
Expand Down Expand Up @@ -116,3 +124,11 @@ For methods not supported 405
Generic server error 500
```

## HttpClient의 올바른 사용방법
* https://devblogs.microsoft.com/dotnet/author/martintomka/
* https://learn.microsoft.com/ko-kr/dotnet/core/resilience/http-resilience
* `Microsoft.Extensions.Http.Resilience` 패키지 사용(암시적으로 polly를 사용함)
* `Chaos Engineering`
```
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http.HttpResults;

namespace WebApplicationMinimalApi8.ExceptionHandlers;

public sealed class BadRequestExceptionHandler(ILogger<GlobalExceptionHandler> logger) : IExceptionHandler
{
public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken)
{
if (exception is BadHttpRequestException ex)
{
logger.LogError(exception, "Exception occurred: {Message}", exception.Message);

await Results.Problem(
title: exception.Message,
statusCode: StatusCodes.Status400BadRequest
).ExecuteAsync(httpContext);

return true;
}
return false;
}
}
25 changes: 14 additions & 11 deletions src/WebApplicationMinimalApi8/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,24 @@
};
});
builder.Services.AddValidatorsFromAssemblyContaining<Program>();

builder.AddFluentValidationEndpointFilter();


builder.Services.AddHttpClient("echo", client =>
{
client.BaseAddress = new("https://httpbin.org");
});

if (builder.Environment.IsEnvironment("Best"))
{
builder.Services.Configure<RouteHandlerOptions>(o => o.ThrowOnBadRequest = true);
builder.Services.AddExceptionHandler<BadRequestExceptionHandler>();
builder.Services.AddExceptionHandler<GlobalExceptionHandler>();
builder.Services.AddProblemDetails(options =>
options.CustomizeProblemDetails = (context) =>
{
if (!context.ProblemDetails.Extensions.ContainsKey("traceId"))
{
string? traceId = Activity.Current?.Id ?? context.HttpContext.TraceIdentifier;
context.ProblemDetails.Extensions.Add(new KeyValuePair<string, object?>("traceId", traceId));
}
}
);
options.CustomizeProblemDetails = context =>
context.ProblemDetails.Extensions["traceId"] =
Activity.Current?.Id ?? context.HttpContext.TraceIdentifier);
}

var app = builder.Build();
Expand Down Expand Up @@ -79,9 +82,9 @@
.WithDescription("사람을 생성합니다.")
.WithOpenApi();

root.MapPut("/Persons", (PersonDto person) => Results.Created("/Persons/1", new
root.MapPut("/Persons/{id}", (PersonDto person, int id) => Results.Ok(new
{
Id = 1,
Id = id,
person.Name,
person.Age,
person.Emails
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageReference Include="ForEvolve.FluentValidation.AspNetCore.Http" Version="1.0.26" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.2.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="8.0.1" />
</ItemGroup>
Expand Down

0 comments on commit 130d8f0

Please sign in to comment.