Open Closed

Issue with `CustomDomainTenantResolveContributor` Registration in Tenant Resolution #8381


User avatar
0
pooria.shariatzadeh@gmail.com created

Description:**

I am trying to implement a custom tenant resolver by extending the DomainTenantResolveContributor in the ABP.IO framework. The goal is to resolve tenants dynamically based on the domain, using a service (ITimcheAppService) to query tenant information from a repository.

Here is the custom tenant resolver implementation:

public class CustomDomainTenantResolveContributor : DomainTenantResolveContributor
{
    private readonly ITimcheAppService _timcheAppService;

    public CustomDomainTenantResolveContributor(string domainFormat, ITimcheAppService timcheAppService)
        : base(domainFormat)
    {
        _timcheAppService = timcheAppService;
    }

    protected override async Task<string?> GetTenantIdOrNameFromHttpContextOrNullAsync(ITenantResolveContext context, HttpContext httpContext)
    {
        if (!httpContext.Request.Host.HasValue)
        {
            return null;
        }

        var domain = httpContext.Request.Host.Host;

        // Query the service for tenant information
        var tenantName = await _timcheAppService.FindTenantNameByDomainAsync(domain);
        if (tenantName == null)
        {
            return null; // No tenant resolved
        }

        context.Handled = true;
        return tenantName; // Return tenant name for ABP to resolve
    }
}

I attempted to register this custom contributor in the ConfigureServices method as follows:

Configure<AbpTenantResolveOptions>(options =>
{
    options.AddContributor(serviceProvider =>
    {
        var timcheAppService = serviceProvider.GetRequiredService<ITimcheAppService>();

        // Add the CustomDomainTenantResolveContributor
        return new CustomDomainTenantResolveContributor("{0}", timcheAppService);
    });
});

However, the resolver is not working as expected. Tenant resolution does not trigger this contributor, and I suspect there might be an issue with how DomainTenantResolveContributor is being used as a base class.


Steps to Reproduce:

  1. Create a custom contributor by extending DomainTenantResolveContributor.
  2. Use a service to query tenant information dynamically based on the domain.
  3. Register the custom contributor in Configure<AbpTenantResolveOptions>.
  4. Test tenant resolution with various domains.

Expected Behavior:

The CustomDomainTenantResolveContributor should be invoked during tenant resolution, dynamically fetching tenant information from the service and resolving the tenant correctly.


Actual Behavior:

The custom contributor is not triggered during tenant resolution.


Environment Details:

  • ABP Framework Version: 8.3
  • .NET Version: 8
  • UI Framework: Blazor Server

Additional Notes:

I noticed that DomainTenantResolveContributor works fine when used directly, but custom extensions or overrides seem to fail when registered using AddContributor.

Please clarify:

  1. Is it possible to extend DomainTenantResolveContributor for custom logic?
  2. If yes, what is the proper way to register such an extension in Configure<AbpTenantResolveOptions>?

1 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    You can try this

    Configure<AbpTenantResolveOptions>(options =>
    {
       options.TenantResolvers.Insert(0, new CustomDomainTenantResolveContributor("{0}"));
    });
    
    ublic class CustomDomainTenantResolveContributor : DomainTenantResolveContributor
    {
        public CustomDomainTenantResolveContributor(string domainFormat)
            : base(domainFormat)
        {
        }
    
        protected override async Task<string?> GetTenantIdOrNameFromHttpContextOrNullAsync(ITenantResolveContext context, HttpContext httpContext)
        {
            if (!httpContext.Request.Host.HasValue)
            {
                return null;
            }
    
            var domain = httpContext.Request.Host.Host;
    
            var timcheAppService = httpContext.RequestServices.GetRequiredService<ITimcheAppService>();
            // Query the service for tenant information
            var tenantName = await timcheAppService.FindTenantNameByDomainAsync(domain);
            if (tenantName == null)
            {
                return null; // No tenant resolved
            }
    
            context.Handled = true;
            return tenantName; // Return tenant name for ABP to resolve
        }
    }
    
Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09