elsa-core/src/modules/Elsa.Common/Multitenancy/Models/TenantScope.cs
Sipke Schoorstra bdaae64e72 Add base path providers and standardize tenant accessor property
Introduce DefaultHttpEndpointBasePathProvider and TenantPrefixHttpEndpointBasePathProvider to manage HTTP endpoint base paths. Update ITenantAccessor property from CurrentTenant to Tenant for consistency across the codebase.
2024-10-17 19:47:55 +02:00

30 lines
952 B
C#

using Microsoft.Extensions.DependencyInjection;
namespace Elsa.Common.Multitenancy;
/// <summary>
/// Represents a tenant scope, which sets the current tenant for the duration of the scope.
/// After the scope is disposed, the original tenant is restored.
/// </summary>
public class TenantScope : IDisposable
{
private readonly IServiceScope _serviceScope;
private readonly ITenantAccessor _tenantAccessor;
private readonly Tenant? _originalTenant;
public TenantScope(IServiceScope serviceScope, ITenantAccessor tenantAccessor, Tenant? tenant)
{
_serviceScope = serviceScope;
_tenantAccessor = tenantAccessor;
_originalTenant = tenantAccessor.Tenant;
_tenantAccessor.Tenant = tenant;
}
public IServiceProvider ServiceProvider => _serviceScope.ServiceProvider;
public void Dispose()
{
_tenantAccessor.Tenant = _originalTenant;
_serviceScope.Dispose();
}
}