Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class TenantDBMappingMiddleware
{
private readonly RequestDelegate next;
public TenantDBMappingMiddleware(RequestDelegate next)
{
this.next = next;
}
public async Task Invoke(HttpContext httpContext)
{
string[] urlParts = null;
#if DEBUG
urlParts = httpContext.Request.Path.Value.Split(new char[] { '/' },
StringSplitOptions.RemoveEmptyEntries);
#else
urlParts = httpContext.Request.Host.Host.Split(new char[] { '.' },
StringSplitOptions.RemoveEmptyEntries);
#endif
if (urlParts != null && urlParts.Any())
{
// Request the previously configured IDbContextFactory instance
// ASP.NET Core's Dependency Injection container
httpContext.RequestServices.GetService().TenantName = urlParts[0];
}
await this.next(httpContext);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment