Dotnettency
Dotnettency copied to clipboard
Endpoint Routing
- Use EndpointDataSource to expose list of custom Endpoints, each one representing a Tenant in my system, more specifically that tenants RequestDelegate / middleware pipeline (lazily constructed, asynchronously, upon first execution of the endpoint)
- Associate each endpoint with its own RouteTemplate that uses a custom constraint that will only succeed if the current Tenant ID (from HttpContext.Items) matches that endpoints Tenant ID. e.g: '{**path:tenant(1)}' for the endpoint for Tenant 1, '{**path:tenant(1)}' for the endpoint for Tenant 2 etc.
- Keep the logic that runs on every request and works out what the TenantID is, in a middleware component, added at the start of the pipeline. It puts the value in HttpContext.Items. This then means the the Endpoints stuff - which is dependenct on that value should work.
Also this is worth a read: https://andrewlock.net/converting-a-terminal-middleware-to-endpoint-routing-in-aspnetcore-3/
Need to investigate whether I can use a Matcher Policy over route contraint:
- https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-3.1
- https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.routing.matcherpolicy?view=aspnetcore-3.1