Open DiogoLuisBarros opened 1 week ago
Proposed fix:
public static MultiTenantBuilder<TTenantInfo> WithBasePathStrategy<TTenantInfo>(
this MultiTenantBuilder<TTenantInfo> builder, Action<BasePathStrategyOptions> configureOptions)
where TTenantInfo : class, ITenantInfo, new()
{
builder.Services.Configure(configureOptions);
builder.Services.Configure<MultiTenantOptions>(options =>
{
var origOnTenantResolved = options.Events.OnTenantResolved;
options.Events.OnTenantResolved = tenantResolvedContext =>
{
if (tenantResolvedContext.StrategyType == typeof(BasePathStrategy))
{
var httpContext = tenantResolvedContext.Context as HttpContext ??
throw new MultiTenantException("BasePathStrategy expects HttpContext.");
if (httpContext.RequestServices.GetRequiredService<IOptions<BasePathStrategyOptions>>().Value
.RebaseAspNetCorePathBase)
{
httpContext.Request.Path.StartsWithSegments($"/{tenantResolvedContext.TenantInfo?.Identifier}",
out var matched, out var
newPath);
httpContext.Request.PathBase = httpContext.Request.PathBase.Add(matched);
httpContext.Request.Path = newPath;
}
}
return origOnTenantResolved(tenantResolvedContext);
};
});
return builder.WithStrategy<BasePathStrategy>(ServiceLifetime.Singleton);
}
It worked for me and seems perfectly fine!
Steps to reproduce
BasePathStrategy
with theWithBasePathStrategy
builder methodMultiTenantException
with the following message: "BasePathStrategy expects HttpContext."My code:
Calling WithBasePathStrategy will not respect the chain of strategies because there are some checks regarding HttpContext that are being done, even if the previous strategy is resolved (in this case it is my custom ForcedTenantStrategy).
Please, check the following code: https://github.com/Finbuckle/Finbuckle.MultiTenant/blob/main/src/Finbuckle.MultiTenant.AspNetCore/Extensions/MultiTenantBuilderExtensions.cs#L224C35-L224C106
Expected behaviour
These checks that are made because of the
BasePathStrategy
register process (during theOnTenantResolved
delegate registration), but should should only be made if the strategy type isBasePathStrategy