< 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
73%
Covered lines: 11
Uncovered lines: 4
Coverable lines: 15
Total lines: 43
Line coverage: 73.3%
Branch coverage
75%
Covered branches: 6
Total branches: 8
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
InvokeAsync()75%9871.42%

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]
 711public 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    {
 29120        var tenant = await tenantResolverPipelineInvoker.InvokePipelineAsync();
 21
 29122        if (tenant != null)
 23        {
 29124            var tenantPrefix = tenant.GetRoutePrefix();
 25
 29126            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
 29137        await using var tenantScope = tenantScopeFactory.CreateScope(tenant);
 29138        var originalServiceProvider = context.RequestServices;
 29139        context.RequestServices = tenantScope.ServiceProvider;
 29140        await next(context);
 29141        context.RequestServices = originalServiceProvider;
 29142    }
 43}