Csharp aspnet patterns

From FerroxLabs/murage · View interactive version

Csharp aspnet patterns is an AI agent skill from the GitHub repository FerroxLabs/murage. Guides expert-level ASP.NET Core development: minimal APIs vs Controllers decision tree, middleware pipeline, filters, configuration binding, health checks, and OpenAPI integration. Use when the user asks about ASP.NET Core, minimal APIs, Controllers, middleware, filters, configuration, health check

How to use this skill

Skill file

C# ASP.NET Core Patterns

When to Use

Use this skill when the user asks about:

Do NOT use this skill when the user asks about:


Process

1. Decide: Minimal API vs. Controller-Based Architecture

Before writing any endpoint code, apply this decision framework. The wrong choice creates expensive refactors.

2. Configure the Middleware Pipeline in Correct Order

ASP.NET Core middleware executes in registration order on request and in reverse on response. Order mistakes cause authentication bypass, missing CORS headers, and silent data loss.

The canonical production order for a secured JSON API is:

UseExceptionHandler / UseDeveloperExceptionPage
UseHttpsRedirection
UseHsts (production only)
UseRouting
UseCors          ← must be after UseRouting, before UseAuthentication
UseAuthentication
UseAuthorization
UseOutputCaching / UseResponseCaching
UseRateLimiter   ← .NET 7+ built-in, after auth so you can rate-limit per user
MapControllers / MapGroup endpoints

3. Implement Filters Correctly for Cross-Cutting Concerns

Filters run inside the MVC/Minimal API action pipeline, after middleware but before model binding (resource filters) or after binding (action filters).

// IEndpointFilter example for Minimal APIs
app.MapPost("/orders", CreateOrderAsync)
   .AddEndpointFilter<ValidationFilter<CreateOrderRequest>>()
   .AddEndpointFilter<IdempotencyFilter>();

4. Bind Configuration to Strongly Typed Options

Never inject IConfiguration directly into services. It bypasses validation, makes testing harder, and breaks refactoring.

public class DatabaseOptions
{
    [Required, MinLength(10)]
    public string ConnectionString { get; init; } = string.Empty;

    [Range(1, 500)]
    public int MaxPoolSize { get; init; } = 100;

    public TimeSpan CommandTimeout { get; init; } = TimeSpan.FromSeconds(30);
}

5. Implement Health Checks with Liveness/Readiness Separation

Kubernetes and cloud load balancers expect two distinct probe endpoints. Conflating them causes unnecessary pod restarts.

builder.Services.AddHealthChecks()
    .AddCheck("self", () => HealthCheckResult.Healthy(), tags: ["live"])
    .AddNpgsql(connectionString, tags: ["ready", "db"])
    .AddRedis(redisConfig, tags: ["ready", "cache"])
    .AddUrlGroup(new Uri("https://payment-service/health"), "payment-svc", tags: ["ready"]);

app.MapHealthChecks("/healthz/live",  new() { Predicate = r => r.Tags.Contains("live") });
app.MapHealthChecks("/healthz/ready", new() { Predicate = r => r.Tags.Contains("ready") });

6. Integrate OpenAPI Documentation

7. Harden and Finalize the Configuration

Before declaring any API production-ready, complete this checklist:


Output Format

When responding to a user request about ASP.NET Core patterns, structure your answer as follows:

## Decision Summary

| Concern | Recommendation | Rationale |
|---------|----------------|-----------|
| API surface style | Minimal API / Controller | [reason based on endpoint count and team context] |
| Auth scheme | JWT Bearer / Cookie / ApiKey | [based on client type: SPA, mobile, server-to-server] |
| Configuration source | appsettings + env vars + secrets | [standard for cloud-native] |
| Health checks | Liveness + Readiness split | [Kubernetes-compatible] |
| Error model | ProblemDetails (RFC 7807) | [interoperable, standard] |

## Program.cs Skeleton

// Builder phase

var builder = WebApplication.CreateBuilder(args);

// [Configuration binding section]

builder.Services.AddOptions<[Name]Options>()

.BindConfiguration("[SectionName]")

.ValidateDataAnnotations()

.ValidateOnStart();

// [Service registration section]

builder.Services.AddControllers() // or AddEndpointsApiExplorer for Minimal

.AddJsonOptions(opts => opts.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase);

builder.Services.AddProblemDetails();

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)

.AddJwtBearer(opts => { /* bind from options */ });

builder.Services.AddAuthorization();

builder.Services.AddHealthChecks()

.AddCheck("self", () => HealthCheckResult.Healthy(), tags: ["live"])

// Add domain-specific checks here

;

// Pipeline phase

var app = builder.Build();

app.UseExceptionHandler();

app.UseHttpsRedirection();

app.UseRouting();

app.UseCors("[PolicyName]");

app.UseAuthentication();

app.UseAuthorization();

if (app.Environment.IsDevelopment()) app.UseSwaggerUI();

app.MapControllers(); // or MapGroup / individual MapPost/MapGet

app.MapHealthChecks("/healthz/live", new() { Predicate = r => r.Tags.Contains("live") });

app.MapHealthChecks("/healthz/ready", new() { Predicate = r => r.Tags.Contains("ready") });

app.Run();


## Middleware Order Verification

| Position | Middleware | Why Here |
|----------|------------|----------|
| 1 | UseExceptionHandler | Catches all downstream exceptions |
| 2 | UseHttpsRedirection | Redirects before any processing |
| 3 | UseRouting | Enables endpoint-aware middleware |
| 4 | UseCors | After routing, before auth |
| 5 | UseAuthentication | Populates ClaimsPrincipal |
| 6 | UseAuthorization | Evaluates policies after identity set |
| 7 | UseOutputCaching | After auth, results are user-specific |
| 8 | MapControllers/Endpoints | Terminal middleware |

## Key Implementation Notes

- [Specific gotchas for this user's scenario]
- [Trade-offs accepted]
- [Configuration values requiring environment-specific tuning]

Rules

  1. Never register IConfiguration directly in service constructors. Always use the Options pattern with IOptions<T>, IOptionsMonitor<T>, or IOptionsSnapshot<T>. Direct IConfiguration injection makes configuration keys stringly-typed, untestable, and impossible to validate at startup.
  1. Always place UseCors after UseRouting and before UseAuthentication. Putting CORS before routing means you cannot select per-endpoint CORS policies. Putting it after authentication means OPTIONS preflight requests (sent without credentials) are rejected with 401 before CORS headers are written.
  1. Never read HttpRequest.Body twice without calling EnableBuffering() first. The default body is a forward-only network stream. Middleware that logs the body must buffer it; otherwise the action receives an empty model after binding fails silently or throws a format exception.
  1. Always call ValidateOnStart() on every AddOptions<T>() registration in production apps. Without it, misconfigured options throw only when the options object is first accessed -- potentially hours into production traffic after restart. Fail fast at startup.
  1. Never use [TypeFilter] or [ServiceFilter] when registering filters globally. Global filter registration via options.Filters.Add<T>() already resolves from DI. Using [TypeFilter] globally creates double-instantiation and can produce duplicate filter execution in some pipeline paths.
  1. Always tag health checks and map them to separate endpoints. A single /health endpoint that checks all dependencies causes Kubernetes to kill and restart pods when a downstream service is degraded -- even though the pod itself is healthy. The liveness probe should contain zero external checks.
  1. Never expose Swagger UI or the OpenAPI spec JSON in production environments. The spec reveals all routes, parameter names, response shapes, and security schemes. Gate it behind app.Environment.IsDevelopment() or an authenticated internal route at minimum.
  1. Always set Kestrel limits explicitly rather than relying on defaults. The default MaxRequestBodySize of 28.6 MB and MaxConcurrentConnections of unlimited are wrong for most production APIs. An API accepting JSON payloads should cap at 1--10 MB. An API accepting file uploads needs a higher cap, a dedicated endpoint, and streaming instead of buffering.
  1. Never use app.UseResponseCaching() and app.UseOutputCaching() simultaneously. They are different implementations (OutputCaching is the .NET 7+ replacement) and they interfere with each other. Use OutputCaching for new code -- it supports per-endpoint policies, cache invalidation by tag, and Redis-backed distributed caching.
  1. Always derive background workers from BackgroundService, not IHostedService directly. BackgroundService handles CancellationToken plumbing correctly, catches unobserved TaskCanceledException, and prevents the host from swallowing background thread exceptions silently. Override ExecuteAsync, and rethrow after logging any unrecoverable exception to trigger graceful host shutdown.

Edge Cases

Long-running Request Body Reads in Minimal APIs

When a Minimal API endpoint receives a large file upload via IFormFile or raw Stream, the default Kestrel request body timeout (the MinRequestBodyDataRate limit) can kill the connection mid-upload on slow clients. Solution: apply [DisableRequestSizeLimit] only to that endpoint using .WithMetadata(new DisableRequestSizeLimitAttribute()) on RouteHandlerBuilder, and set a per-endpoint rate: context.Features.Get<IHttpMaxRequestBodySizeFeature>()?.MaxRequestBodySize = 500_000_000. Do not disable the global limit.

Filters and Minimal API Incompatibility

IActionFilter, IResultFilter, IResourceFilter, and IExceptionFilter are MVC constructs -- they do NOT execute for Minimal API endpoints. Teams migrating from controllers to Minimal APIs lose all global MVC filters silently. Replace them with: IEndpointFilter for action/result behavior, middleware for cross-cutting pipeline behavior (exception handling, logging, correlation IDs), and IHostedService for background concerns. Audit every global filter before migration.

Options Reload in Singleton Services

Injecting IOptionsSnapshot<T> into a singleton throws at runtime because IOptionsSnapshot is scoped. Injecting IOptions<T> into a singleton works but never reflects configuration reloads (e.g., Azure App Configuration refresh). For singletons that must react to live config changes, inject IOptionsMonitor<T> and subscribe to monitor.OnChange(updatedConfig => { /* update local copy */ }). Cache the updated config in a field -- do not call monitor.CurrentValue on every operation (it acquires a lock on every access).

Middleware Exception Handling vs. Action Exception Filters

A common mistake is registering both UseExceptionHandler middleware and a global IExceptionFilter. They handle different exception types. IExceptionFilter catches exceptions thrown during model binding, action execution, and result execution -- but not exceptions thrown in other middleware (database connection pool exhaustion, Redis timeout, etc.). UseExceptionHandler catches everything downstream. If both exist, define a clear boundary: use IExceptionFilter only for domain-specific MVC exceptions (e.g., EntityNotFoundException → 404), and let UseExceptionHandler convert all other exceptions to ProblemDetails 500s. Never swallow exceptions in a filter -- always rethrow if you do not handle them.

CORS Preflight Failures Behind a Reverse Proxy

When deployed behind NGINX or an Azure API Gateway, the Origin header may differ from the application's configured allowed origins due to host rewriting. Symptoms: 403 on OPTIONS requests despite correct CORS configuration. Solution: configure the reverse proxy to forward the original Origin header unchanged, or use builder.Services.AddCors(opts => opts.AddPolicy("prod", p => p.WithOrigins(...).AllowAnyMethod().AllowAnyHeader())) with the exact frontend domain (never AllowAnyOrigin() with AllowCredentials() -- this throws at startup in .NET 6+ because it violates the CORS spec). Also ensure UseForwardedHeaders middleware is registered before UseCors to correctly populate Request.Scheme and Request.Host.

Health Check Authentication Conflicts

If UseAuthentication and UseAuthorization are registered (as they should be), health check endpoints at /healthz/* will be protected by default in apps using a default authorization policy. Kubernetes probe requests carry no credentials and will receive 401/403, causing continuous pod restarts. Fix by explicitly calling .AllowAnonymous() on the health check MapHealthChecks(...) endpoint: app.MapHealthChecks("/healthz/live", options).AllowAnonymous(). Alternatively, add the health check paths to the UseForwardedHeaders exclusion list or use network policy at the infrastructure level to restrict who can reach the probe endpoint.

Keyed Service Resolution in Middleware

.NET 8 keyed services ([FromKeyedServices("key")]) work in controller constructor injection and IEndpointFilter constructors, but NOT in middleware constructors resolved via IMiddleware registration. Middleware is resolved from the root service provider (a singleton scope), while keyed service resolution requires the concrete key at compile time. Workaround: inject IServiceProvider or IKeyedServiceProvider into the middleware constructor and resolve the keyed service inside InvokeAsync -- but be aware this bypasses the DI lifetime system. Prefer resolving keyed services at the endpoint/controller layer, not the middleware layer.

OpenAPI Spec Generation with Nullable Reference Types

Enabling <Nullable>enable</Nullable> in the project (required in .NET 8 templates) changes how Swashbuckle infers property schemas. Non-nullable properties become required in the generated schema; nullable properties get nullable: true. However, string? properties on request DTOs do not automatically get minLength or pattern validation in the schema -- those require explicit [MinLength], [RegularExpression], or [JsonRequired] attributes. Without them, clients see a schema that implies a property is optional when the server actually rejects empty strings. Audit all request/response DTOs for alignment between C# nullability, DataAnnotations, and the emitted OpenAPI schema.


Example

Input: "I'm building a new .NET 8 REST API for an e-commerce order management system. It will have about 30 endpoints across orders, products, and customers. We use JWT Bearer tokens, PostgreSQL via EF Core, Redis for caching, and we need to deploy to Kubernetes. What's the right ASP.NET Core architecture?"


Output:

Decision Summary

| Concern | Recommendation | Rationale |

|---------|----------------|-----------|

| API surface style | Controllers | 30 endpoints across 3 domains benefits from grouping, auto-validation via [ApiController], and per-controller filter scope |

| Auth scheme | JWT Bearer | Stateless, works for SPA + mobile clients, standard for microservices |

| Configuration binding | AddOptions<T> + env vars + secrets | Validation at startup, no stringly-typed access |

| Error model | ProblemDetails (RFC 7807) | Interoperable, built-in .NET 7+ support |

| Health checks | Liveness + Readiness, split endpoints | Kubernetes-compatible, prevents pod cycling on DB hiccups |

| Caching | UseOutputCaching + Redis backing | .NET 7+ output cache supports distributed cache and tag invalidation |

| OpenAPI | Swashbuckle 6.x with XML comments | Mature, well-supported in .NET 8 |


Program.cs

using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.IdentityModel.Tokens;
using HealthChecks.UI.Client;
using System.Text;

var builder = WebApplication.CreateBuilder(args);

// ─── Configuration Binding ───────────────────────────────────────────────────

builder.Services.AddOptions<DatabaseOptions>()
    .BindConfiguration("Database")
    .ValidateDataAnnotations()
    .ValidateOnStart();

builder.Services.AddOptions<JwtOptions>()
    .BindConfiguration("Jwt")
    .ValidateDataAnnotations()
    .ValidateOnStart();

builder.Services.AddOptions<RedisOptions>()
    .BindConfiguration("Redis")
    .ValidateDataAnnotations()
    .ValidateOnStart();

// ─── Core Services ───────────────────────────────────────────────────────────

builder.Services.AddControllers(options =>
{
    options.Filters.Add<GlobalExceptionFilter>();       // domain exceptions → ProblemDetails
    options.Filters.Add<RequestTimingFilter>();         // logs slow requests > 500ms
})
.AddJsonOptions(opts =>
{
    opts.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
    opts.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
});

builder.Services.AddProblemDetails(); // RFC 7807 for unhandled exceptions

// ─── Authentication & Authorization ─────────────────────────────────────────

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(opts =>
    {
        var jwtConfig = builder.Configuration.GetSection("Jwt");
        opts.TokenValidationParameters = new TokenValidationParameters
        {
            ValidateIssuer           = true,
            ValidateAudience         = true,
            ValidateLifetime         = true,
            ValidateIssuerSigningKey = true,
            ValidIssuer              = jwtConfig["Issuer"],
            ValidAudience            = jwtConfig["Audience"],
            IssuerSigningKey         = new SymmetricSecurityKey(
                                           Encoding.UTF8.GetBytes(jwtConfig["SigningKey"]!)),
            ClockSkew                = TimeSpan.FromSeconds(30) // tighten from 5-min default
        };
    });

builder.Services.AddAuthorization(opts =>
{
    opts.AddPolicy("OrderManager", p => p.RequireClaim("role", "order_manager", "admin"));
    opts.AddPolicy("ReadOnly",     p => p.RequireClaim("role", "viewer", "order_manager", "admin"));
});

// ─── Caching ────────────────────────────────────────────────────────────────

var redisConnectionString = builder.Configuration["Redis:ConnectionString"]!;

builder.Services.AddStackExchangeRedisCache(opts =>
    opts.Configuration = redisConnectionString);

builder.Services.AddOutputCache(opts =>
{
    opts.AddBasePolicy(b => b.Cache()); // default: vary by Accept-Encoding
    opts.AddPolicy("ProductCatalog", b => b
        .Tag("products")
        .Expire(TimeSpan.FromMinutes(10))
        .SetVaryByQuery("page", "pageSize", "category"));
});

// ─── Health Checks ───────────────────────────────────────────────────────────

builder.Services.AddHealthChecks()
    .AddCheck("self",        () => HealthCheckResult.Healthy(),     tags: ["live"])
    .AddNpgsql(
        builder.Configuration["Database:ConnectionString"]!,
        name:    "postgres",
        tags:    ["ready", "db"])
    .AddRedis(redisConnectionString,
        name:    "redis",
        tags:    ["ready", "cache"])
    .AddUrlGroup(
        new Uri(builder.Configuration["ExternalServices:PaymentService"]!),
        name:    "payment-svc",
        timeout: TimeSpan.FromSeconds(3),
        tags:    ["ready"]);

// ─── API Versioning + OpenAPI ─────────────────────────────────────────────

builder.Services.AddApiVersioning(opts =>
{
    opts.DefaultApiVersion       = new ApiVersion(1, 0);
    opts.AssumeDefaultVersionWhenUnspecified = true;
    opts.ReportApiVersions       = true;
}).AddApiExplorer(opts =>
{
    opts.GroupNameFormat         = "'v'VVV";
    opts.SubstituteApiVersionInUrl = true;
});

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new() { Title = "Order Management API", Version = "v1" });
    c.SwaggerDoc("v2", new() { Title = "Order Management API", Version = "v2" });

    var xmlPath = Path.Combine(AppContext.BaseDirectory,
        $"{Assembly.GetExecutingAssembly().GetName().Name}.xml");
    c.IncludeXmlComments(xmlPath);

    c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
    {
        Name         = "Authorization",
        Type         = SecuritySchemeType.Http,
        Scheme       = "bearer",
        BearerFormat = "JWT",
        In           = ParameterLocation.Header
    });
    c.OperationFilter<BearerAuthOperationFilter>(); // adds lock icon to [Authorize] routes
});

// ─── CORS ────────────────────────────────────────────────────────────────────

builder.Services.AddCors(opts => opts.AddPolicy("spa-clients", p => p
    .WithOrigins("https://admin.mystore.com", "https://app.mystore.com")
    .AllowAnyMethod()
    .AllowAnyHeader()
    .AllowCredentials()));

// ─── Kestrel Limits ──────────────────────────────────────────────────────────

builder.WebHost.ConfigureKestrel(opts =>
{
    opts.Limits.MaxRequestBodySize         = 5 * 1024 * 1024; // 5 MB
    opts.Limits.KeepAliveTimeout           = TimeSpan.FromMinutes(2);
    opts.Limits.MaxConcurrentConnections   = 1000;
    opts.Limits.Http2.MaxStreamsPerConnection = 100;
});

// ─── Background Services ─────────────────────────────────────────────────────

builder.Services.AddHostedService<OutboxEventProcessor>();

// ─── Build & Pipeline ────────────────────────────────────────────────────────

var app = builder.Build();

app.UseExceptionHandler();          // RFC 7807 ProblemDetails for all unhandled exceptions
app.UseHttpsRedirection();

app.UseForwardedHeaders(new ForwardedHeadersOptions
{
    ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});

app.UseRouting();
app.UseCors("spa-clients");
app.UseAuthentication();
app.UseAuthorization();
app.UseOutputCache();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
        c.SwaggerEndpoint("/swagger/v2/swagger.json", "v2");
    });
}

app.MapControllers();

app.MapHealthChecks("/healthz/live",  new HealthCheckOptions
{
    Predicate      = r => r.Tags.Contains("live"),
    ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
}).AllowAnonymous();

app.MapHealthChecks("/healthz/ready", new HealthCheckOptions
{
    Predicate      = r => r.Tags.Contains("ready"),
    ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
}).AllowAnonymous();

app.Run();

Controller Example: Orders (with filters, caching, versioning)

[ApiController]
[Route("api/v{version:apiVersion}/orders")]
[ApiVersion("1.0")]
[Authorize(Policy = "ReadOnly")]
[Produces("application/json")]
[Consumes("application/json")]
public sealed class OrdersController : ControllerBase
{
    private readonly IOrderService _orders;
    private readonly ILogger<OrdersController> _logger;

    public OrdersController(IOrderService orders, ILogger<OrdersController> logger)
    {
        _orders = orders;
        _logger = logger;
    }

    /// <summary>Returns a paginated list of orders for the authenticated customer.</summary>
    /// <param name="page">1-based page number.</param>
    /// <param name="pageSize">Items per page (1-100).</param>
    [HttpGet]
    [OutputCache(PolicyName = "ProductCatalog")] // tags allow cache invalidation on write
    [ProducesResponseType(typeof(PagedResult<OrderSummary>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ProblemDetails),            StatusCodes.Status401Unauthorized)]
    public async Task<IActionResult> GetOrdersAsync(
        [FromQuery] int page = 1,
        [FromQuery, Range(1, 100)] int pageSize = 25,
        CancellationToken ct = default)
    {
        var customerId = User.FindFirstValue("sub")
            ?? throw new InvalidOperationException("sub claim missing from JWT");

        var result = await _orders.GetPagedAsync(customerId, page, pageSize, ct);
        return Ok(result);
    }

    /// <summary>Creates a new order. Idempotent when the same Idempotency-Key is provided.</summary>
    [HttpPost]
    [Authorize(Policy = "OrderManager")]
    [ProducesResponseType(typeof(OrderCreatedResponse), StatusCodes.Status201Created)]
    [ProducesResponseType(typeof(ProblemDetails),        StatusCodes.Status400BadRequest)]
    [ProducesResponseType(typeof(ProblemDetails),        StatusCodes.Status409Conflict)]
    public async Task<IActionResult> CreateOrderAsync(
        [FromBody] CreateOrderRequest request,
        [FromHeader(Name = "Idempotency-Key")] string? idempotencyKey,
        CancellationToken ct = default)
    {
        // [ApiController] has already validated ModelState -- no manual check needed
        var order = await _orders.CreateAsync(request, idempotencyKey, ct);
        return CreatedAtAction(nameof(GetOrdersAsync), new { id = order.Id }, order);
    }
}

Options Classes

public sealed class DatabaseOptions
{
    [Required, MinLength(10)]
    public string ConnectionString { get; init; } = string.Empty;

    [Range(1, 500)]
    public int MaxPoolSize { get; init; } = 100;

    public TimeSpan CommandTimeout { get; init; } = TimeSpan.FromSeconds(30);
}

public sealed class JwtOptions
{
    [Required] public string Issuer     { get; init; } = string.Empty;
    [Required] public string Audience   { get; init; } = string.Empty;
    [Required, MinLength(32)] public string SigningKey { get; init; } = string.Empty;
    public TimeSpan TokenLifetime { get; init; } = TimeSpan.FromHours(1);
}

public sealed class RedisOptions
{
    [Required] public string ConnectionString { get; init; } = string.Empty;
    public int Database  { get; init; } = 0;
    public TimeSpan ConnectTimeout { get; init; } = TimeSpan.FromSeconds(5);
}

Global Exception Filter (Domain Exceptions → ProblemDetails)

public sealed class GlobalExceptionFilter : IExceptionFilter
{
    private readonly ILogger<GlobalExceptionFilter> _logger;
    private readonly IProblemDetailsService _problemDetails;

    public GlobalExceptionFilter(
        ILogger<GlobalExceptionFilter> logger,
        IProblemDetailsService problemDetails)
    {
        _logger         = logger;
        _problemDetails = problemDetails;
    }

    public void OnException(ExceptionContext context)
    {
        var (status, title) = context.Exception switch
        {
            EntityNotFoundException e  => (StatusCodes.Status404NotFound,       "Resource not found"),
            ValidationException e      => (StatusCodes.Status400BadRequest,      "Validation failed"),
            ConflictException e        => (StatusCodes.Status409Conflict,        "Conflict"),
            UnauthorizedException e    => (StatusCodes.Status403Forbidden,       "Access denied"),
            _                          => (0, null) // let UseExceptionHandler handle the rest
        };

        if (status == 0) return; // do NOT set context.ExceptionHandled -- rethrow to middleware

        _logger.LogWarning(context.Exception, "Domain exception: {Title}", title);

        context.Result = new ObjectResult(new ProblemDetails
        {
            Status   = status,
            Title    = title,
            Detail   = context.Exception.Message,
            Instance = context.HttpContext.Request.Path
        })
        { StatusCode = status };

        context.ExceptionHandled = true;
    }
}

Middleware Pipeline Order Verification

| Position | Middleware | Why Here |

|----------|-----------|----------|

| 1 | UseExceptionHandler() | Wraps entire pipeline; catches infrastructure exceptions as ProblemDetails 500 |

| 2 | UseHttpsRedirection() | Redirect before any work is done |

| 3 | UseForwardedHeaders() | Fixes Request.Scheme/Host behind reverse proxy before CORS evaluates origin |

| 4 | UseRouting() | Endpoint metadata available for CORS policy selection |

| 5 | UseCors("spa-clients") | OPTIONS preflight handled before auth rejects it |

| 6 | UseAuthentication() | Populates ClaimsPrincipal from JWT |

| 7 | UseAuthorization() | Evaluates policies after identity is populated |

| 8 | UseOutputCache() | Caches authenticated responses per user where appropriate |

| 9 | MapControllers() / MapHealthChecks() | Terminal middleware -- writes response |


Key Implementation Notes for This Scenario

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.