Skip to content

Commit 00148bf

Browse files
this commit introduces package references and reusable components across all ASP.NET projects
1 parent 0d7e10b commit 00148bf

7 files changed

Lines changed: 89 additions & 1 deletion

File tree

Artifacts/Comanda.Internal.AspNet/Source/Comanda.Internal.AspNet.csproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,16 @@
2929
</ItemGroup>
3030

3131
<ItemGroup>
32-
<!-- source link configuration for debugging -->
3332
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
33+
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.1" />
34+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.12" />
35+
<PackageReference Include="Scalar.AspNetCore" Version="2.12.27" />
36+
<PackageReference Include="Scalar.AspNetCore.Microsoft" Version="2.12.27" />
37+
<PackageReference Include="Sentry.AspNetCore" Version="6.0.0" />
38+
<PackageReference Include="Sentry.Serilog" Version="6.0.0" />
39+
<PackageReference Include="Serilog.AspNetCore" Version="10.0.0" />
40+
<PackageReference Include="Serilog.Sinks.Seq" Version="9.0.0" />
41+
<PackageReference Include="HttpsRichardy.Federation.Sdk" Version="4.2.0" />
3442
</ItemGroup>
3543

3644
</Project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Comanda.Internal.AspNet.Constants;
2+
3+
public static class Headers
4+
{
5+
public const string Credential = "Credential";
6+
public const string Location = "Location";
7+
public const string Link = "Link";
8+
public const string Pagination = "Pagination";
9+
public const string Authorization = "Authorization";
10+
public const string Idempotency = "Idempotency-Key";
11+
public const string Correlation = "Correlation";
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Comanda.Internal.AspNet.Constants;
2+
3+
public static class SecuritySchemes
4+
{
5+
public const string OAuth2 = "OAuth2";
6+
public const string Bearer = "Bearer";
7+
}
8+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace Comanda.Internal.AspNet.Interceptors;
2+
3+
public sealed class CorrelationInterceptor(IHttpContextAccessor accessor) : DelegatingHandler
4+
{
5+
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
6+
{
7+
var correlation = accessor.HttpContext?.Items[Headers.Correlation]?.ToString();
8+
9+
if (!string.IsNullOrWhiteSpace(correlation))
10+
{
11+
request.Headers.Remove(Headers.Correlation);
12+
request.Headers.Add(Headers.Correlation, correlation);
13+
}
14+
15+
return await base.SendAsync(request, cancellationToken);
16+
}
17+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace Comanda.Internal.AspNet.Middlewares;
2+
3+
public sealed class CorrelationMiddleware(RequestDelegate next)
4+
{
5+
public async Task InvokeAsync(HttpContext context)
6+
{
7+
var correlationId = context.Request.Headers[Headers.Correlation].FirstOrDefault();
8+
9+
if (string.IsNullOrWhiteSpace(correlationId))
10+
correlationId = context.TraceIdentifier;
11+
12+
context.Items[Headers.Correlation] = correlationId;
13+
context.Response.Headers[Headers.Correlation] = correlationId;
14+
15+
/* enriches the logging scope with the current correlation identifier, ensuring all logs within this request pipeline share the same identifier */
16+
/* more details: https://microsoft.github.io/code-with-engineering-playbook/observability/correlation-id/ */
17+
18+
using (LogContext.PushProperty(Headers.Correlation, correlationId))
19+
using (SentrySdk.PushScope())
20+
{
21+
SentrySdk.ConfigureScope(scope =>
22+
{
23+
scope.SetTag("correlation_id", correlationId);
24+
});
25+
26+
await next(context);
27+
}
28+
}
29+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Comanda.Internal.AspNet.Middlewares;
2+
3+
public static class CorrelationMiddlewareExtension
4+
{
5+
public static IApplicationBuilder UseCorrelationMiddleware(this IApplicationBuilder builder)
6+
{
7+
return builder.UseMiddleware<CorrelationMiddleware>();
8+
}
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
global using Microsoft.AspNetCore.Http;
2+
global using Microsoft.AspNetCore.Builder;
3+
4+
global using Comanda.Internal.AspNet.Constants;
5+
global using Serilog.Context;

0 commit comments

Comments
 (0)