| | | 1 | | using Microsoft.AspNetCore.Builder; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.AspNetCore.Endpoints; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides ASP.NET Core application builder extensions for AsiBackbone endpoint governance. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class EndpointGovernanceApplicationBuilderExtensions |
| | | 9 | | { |
| | | 10 | | private const string EndpointRouteBuilderKey = "__EndpointRouteBuilder"; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Adds AsiBackbone endpoint governance middleware to the ASP.NET Core pipeline. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// <para> |
| | | 17 | | /// This middleware governs the <em>selected</em> endpoint, so it must run after endpoint routing has selected one. |
| | | 18 | | /// Placed before routing, every request reaches it with no endpoint, no governance metadata is found, and the |
| | | 19 | | /// request is forwarded ungoverned unless <see cref="EndpointGovernanceOptions.RequireGovernanceMetadata" /> |
| | | 20 | | /// is enabled. Register it after <c>UseRouting</c> and after authentication, so an actor context is available. |
| | | 21 | | /// </para> |
| | | 22 | | /// <para> |
| | | 23 | | /// A host that calls <c>UseRouting</c> explicitly can pass <paramref name="requireEndpointRoutingRegistered" /> to |
| | | 24 | | /// turn that ordering requirement into a startup failure. It is off by default because it cannot be checked |
| | | 25 | | /// reliably: a Minimal API host that never calls <c>UseRouting</c> still routes correctly, because |
| | | 26 | | /// <c>WebApplication</c> inserts routing at the front of the pipeline when endpoints are mapped, and in that case |
| | | 27 | | /// the routing marker is legitimately absent when this method runs. |
| | | 28 | | /// </para> |
| | | 29 | | /// </remarks> |
| | | 30 | | /// <param name="app">The application builder.</param> |
| | | 31 | | /// <param name="requireEndpointRoutingRegistered"> |
| | | 32 | | /// When <see langword="true" />, throws if endpoint routing has not been registered before this call. Only use this |
| | | 33 | | /// in hosts that call <c>UseRouting</c> explicitly. |
| | | 34 | | /// </param> |
| | | 35 | | /// <returns>The same application builder so calls can be chained.</returns> |
| | | 36 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="app" /> is <see langword="null" />.</excepti |
| | | 37 | | /// <exception cref="InvalidOperationException"> |
| | | 38 | | /// Thrown when <paramref name="requireEndpointRoutingRegistered" /> is <see langword="true" /> and endpoint routing |
| | | 39 | | /// has not been registered before this call. |
| | | 40 | | /// </exception> |
| | | 41 | | public static IApplicationBuilder UseAsiBackboneEndpointGovernance( |
| | | 42 | | this IApplicationBuilder app, |
| | | 43 | | bool requireEndpointRoutingRegistered = false) |
| | | 44 | | { |
| | 5 | 45 | | ArgumentNullException.ThrowIfNull(app); |
| | | 46 | | |
| | 4 | 47 | | return requireEndpointRoutingRegistered && !app.Properties.ContainsKey(EndpointRouteBuilderKey) |
| | 4 | 48 | | ? throw new InvalidOperationException( |
| | 4 | 49 | | "AsiBackbone endpoint governance was registered before endpoint routing. Endpoint governance reads the s |
| | 4 | 50 | | : app.UseMiddleware<EndpointGovernanceMiddleware>(); |
| | | 51 | | } |
| | | 52 | | } |