< Summary

Information
Class: Elsa.Tenants.AspNetCore.Middleware.TenantResolutionMiddleware
Assembly: Elsa.Tenants.AspNetCore
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Tenants.AspNetCore/Middleware/TenantResolutionMiddleware.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 15
Coverable lines: 15
Total lines: 43
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
InvokeAsync()0%7280%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Tenants.AspNetCore/Middleware/TenantResolutionMiddleware.cs

#LineLine coverage
 1using Elsa.Common.Multitenancy;
 2using JetBrains.Annotations;
 3using Microsoft.AspNetCore.Http;
 4
 5namespace Elsa.Tenants.AspNetCore.Middleware;
 6
 7/// <summary>
 8/// Middleware to initialize the tenant for each incoming HTTP request.
 9/// </summary>
 10[UsedImplicitly]
 011public class TenantResolutionMiddleware(RequestDelegate next, ITenantScopeFactory tenantScopeFactory)
 12{
 13    /// <summary>
 14    /// Invokes the middleware to ensure the tenant is initialized.
 15    /// </summary>
 16    /// <param name="context">The current HTTP context.</param>
 17    /// <param name="tenantResolverPipelineInvoker"></param>
 18    public async Task InvokeAsync(HttpContext context, ITenantResolverPipelineInvoker tenantResolverPipelineInvoker)
 19    {
 020        var tenant = await tenantResolverPipelineInvoker.InvokePipelineAsync();
 21
 022        if (tenant != null)
 23        {
 024            var tenantPrefix = tenant.GetRoutePrefix();
 25
 026            if (!string.IsNullOrWhiteSpace(tenantPrefix))
 27            {
 028                var tenantPath = $"/{tenantPrefix}";
 029                if (context.Request.Path.StartsWithSegments(tenantPath))
 30                {
 031                    context.Request.PathBase = tenantPath;
 032                    context.Request.Path = context.Request.Path.Value![tenantPath.Length..];
 33                }
 34            }
 35        }
 36
 037        await using var tenantScope = tenantScopeFactory.CreateScope(tenant);
 038        var originalServiceProvider = context.RequestServices;
 039        context.RequestServices = tenantScope.ServiceProvider;
 040        await next(context);
 041        context.RequestServices = originalServiceProvider;
 042    }
 43}