refactor: use Correlation Package Nuget

This commit is contained in:
Jean Gatto
2024-06-07 16:53:24 -03:00
parent 63946874d5
commit 95bea85d3a
8 changed files with 7 additions and 76 deletions
+2 -1
View File
@@ -12,13 +12,14 @@
<PackageVersion Include="AspNetCore.HealthChecks.UI.Client" Version="8.0.1" />
<PackageVersion Include="AutoMapper" Version="13.0.1" />
<PackageVersion Include="Bogus" Version="35.5.1" />
<PackageVersion Include="CorrelationId" Version="3.0.1" />
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="coverlet.msbuild" Version="6.0.2" />
<PackageVersion Include="EntityFrameworkCore.DataEncryption" Version="5.0.0" />
<PackageVersion Include="FluentAssertions" Version="6.12.0" />
<PackageVersion Include="FluentValidation" Version="11.9.1" />
<PackageVersion Include="FluentValidation.DependencyInjectionExtensions" Version="11.9.1" />
<PackageVersion Include="MediatR" Version="12.2.0" />
<PackageVersion Include="MediatR" Version="12.3.0" />
<PackageVersion Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.6" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.1.0" />
-5
View File
@@ -2,17 +2,12 @@ using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.DependencyInjection;
using Shop.Core.AppSettings;
using Shop.Core.SharedKernel;
using Shop.Core.SharedKernel.Correlation;
namespace Shop.Core;
[ExcludeFromCodeCoverage]
public static class ConfigureServices
{
public static IServiceCollection AddCorrelationGenerator(this IServiceCollection services) =>
services
.AddTransient<ICorrelationIdGenerator, CorrelationIdGenerator>();
public static IServiceCollection ConfigureAppSettings(this IServiceCollection services) =>
services
.AddOptionsWithValidation<ConnectionOptions>()
@@ -1,12 +0,0 @@
using System;
namespace Shop.Core.SharedKernel.Correlation;
public class CorrelationIdGenerator : ICorrelationIdGenerator
{
private string _correlationId = Guid.NewGuid().ToString("D");
public string Get() => _correlationId;
public void Set(string correlationId) => _correlationId = correlationId;
}
@@ -1,19 +0,0 @@
namespace Shop.Core.SharedKernel.Correlation;
/// <summary>
/// Represents a correlation ID generator.
/// </summary>
public interface ICorrelationIdGenerator
{
/// <summary>
/// Generates a correlation ID.
/// </summary>
/// <returns>The generated correlation ID.</returns>
string Get();
/// <summary>
/// Sets the correlation ID.
/// </summary>
/// <param name="correlationId">The correlation ID to set.</param>
void Set(string correlationId);
}
@@ -5,9 +5,6 @@ namespace Shop.PublicApi.Extensions;
internal static class MiddlewareExtensions
{
public static void UseCorrelationId(this IApplicationBuilder builder) =>
builder.UseMiddleware<CorrelationIdMiddleware>();
public static void UseErrorHandling(this IApplicationBuilder builder) =>
builder.UseMiddleware<ErrorHandlingMiddleware>();
}
@@ -1,35 +0,0 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using Shop.Core.SharedKernel.Correlation;
namespace Shop.PublicApi.Middlewares;
public class CorrelationIdMiddleware(RequestDelegate next)
{
private const string CorrelationIdHeaderKey = "X-Correlation-Id";
public async Task Invoke(HttpContext httpContext, ICorrelationIdGenerator correlationIdGenerator)
{
var correlationId = GetOrCreateCorrelationId(httpContext, correlationIdGenerator);
httpContext.Response.OnStarting(() =>
{
httpContext.Response.Headers.Append(CorrelationIdHeaderKey, correlationId);
return Task.CompletedTask;
});
await next(httpContext);
}
private static string GetOrCreateCorrelationId(HttpContext httpContext, ICorrelationIdGenerator correlationIdGenerator)
{
if (httpContext.Request.Headers.TryGetValue(CorrelationIdHeaderKey, out var correlationId))
{
correlationIdGenerator.Set(correlationId);
return correlationId;
}
return correlationIdGenerator.Get();
}
}
+4 -1
View File
@@ -1,5 +1,7 @@
using System.Globalization;
using System.IO.Compression;
using CorrelationId;
using CorrelationId.DependencyInjection;
using FluentValidation;
using FluentValidation.Resources;
using HealthChecks.UI.Client;
@@ -59,7 +61,6 @@ builder.Services.AddControllers()
// Adding the application services in ASP.NET Core DI.
builder.Services
.ConfigureAppSettings()
.AddCorrelationGenerator()
.AddInfrastructure()
.AddCommandHandlers()
.AddQueryHandlers()
@@ -70,6 +71,8 @@ builder.Services
.AddCacheService(builder.Configuration)
.AddHealthChecks(builder.Configuration);
builder.Services.AddDefaultCorrelationId();
// MiniProfiler for .NET
// https://miniprofiler.com/dotnet/
builder.Services.AddMiniProfiler(options =>
+1
View File
@@ -17,6 +17,7 @@
<PackageReference Include="AspNetCore.HealthChecks.Redis" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" />
<PackageReference Include="AutoMapper" />
<PackageReference Include="CorrelationId" />
<PackageReference Include="MediatR" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" />