using Elsa.Common.Multitenancy;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Http;
namespace Elsa.Tenants.AspNetCore.Middleware;
///
/// Middleware to initialize the tenant for each incoming HTTP request.
///
[UsedImplicitly]
public class TenantResolutionMiddleware(RequestDelegate next, ITenantScopeFactory tenantScopeFactory)
{
///
/// Invokes the middleware to ensure the tenant is initialized.
///
/// The current HTTP context.
///
public async Task InvokeAsync(HttpContext context, ITenantResolverPipelineInvoker tenantResolverPipelineInvoker)
{
var tenant = await tenantResolverPipelineInvoker.InvokePipelineAsync();
using (tenantScopeFactory.CreateScope(tenant))
await next(context);
}
}