abp icon indicating copy to clipboard operation
abp copied to clipboard

The class MvcRemoteTenantStore always fails to return correct tenant information

Open aitting opened this issue 1 year ago • 1 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Description

abp version 8.2.0

var tenantConfiguration = await Cache.GetAsync(cacheKey);
if (tenantConfiguration?.Value == null)
{
    await TenantAppService.FindTenantByIdAsync(id);  // There is no assignment to the result here, the result always returns null
    tenantConfiguration = await Cache.GetAsync(cacheKey);
}
/

await TenantAppService.FindTenantByIdAsync(id); // There is no assignment to the result here, the result always returns null

I have tested countless times, cleared the cache countless times, and the cache address has changed. I find that every time I request a remote address, it always prompts that I cannot find the tenant. I requested a remote address separately, and everything works fine together!

Reproduction Steps

No response

Expected behavior

No response

Actual behavior

No response

Regression?

No response

Known Workarounds

No response

Version

8.2.0

User Interface

Angular

Database Provider

EF Core (Default)

Tiered or separate authentication server

Separate Auth Server

Operation System

Windows (Default)

Other information

No response

aitting avatar Jul 24 '24 07:07 aitting

hi

Reproduction Steps for a new template project?

maliming avatar Jul 24 '24 08:07 maliming

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 26 '25 06:04 stale[bot]

@maliming

I have also hit the same issue. I am using a tiered MVC/EF project and have spent the past day or so tracing this problem through.

I am using domain name tenant resolution.

The UI was reportedly not able to find the tenant despite it existing in the DB, despite the MvcRemoteTenantStore making the API call to check for it successfully (with the tenant information returned).

I found that because I had Redis disabled for development purposes, the TenantConfiguration was never passed to the front-end.

The code in the MvcRemoteTenantStore makes a successful call to the TenantAppService and discards the result (well, in one case it is assigned but never used). The Find/FindAsync method then assume that because the call was made to the API, that a value was added to the cache. However, with redis disabled this is not the case, and even though we've successfully obtained the TenantConfiguration, because it's not cached, null is returned to the callee.

I traced the issue to these lines:

https://github.com/abpframework/abp/blob/ee4dce5179613dd8c631a1e4a3039b161b43714a/framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/MvcRemoteTenantStore.cs#L47

https://github.com/abpframework/abp/blob/ee4dce5179613dd8c631a1e4a3039b161b43714a/framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/MvcRemoteTenantStore.cs#L72

https://github.com/abpframework/abp/blob/ee4dce5179613dd8c631a1e4a3039b161b43714a/framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/MvcRemoteTenantStore.cs#L102

https://github.com/abpframework/abp/blob/ee4dce5179613dd8c631a1e4a3039b161b43714a/framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/MvcRemoteTenantStore.cs#L127

I understand that this could be by design, but it could be enhanced to reflect something simiar to:

if (tenantConfiguration?.Value == null)
    {
        var configFromApi = await TenantAppService.FindTenantByIdAsync(id);
        var configFromCache = await Cache.GetAsync(cacheKey);
        tenantConfiguration = configFromCache ?? configFromApi;
    }

Thanks :)

DworrallRC avatar Aug 07 '25 09:08 DworrallRC

hi @DworrallRC

I will check it. But using distributed cache/Redis is required.

Thanks.

maliming avatar Aug 08 '25 02:08 maliming

Hi @maliming

That's fine - but if the API successfully obtains the tenant configuration then, imho, we should use the result. Alternatively, we could add a check so that if the result from the API is valid, but there is no matching value in the cache then an exception should be thrown with a useful message, to help with troubleshooting. It took me over a day to figure this out, and obviously other people have had the same issue (as I am commenting on somebody elses original comment).

For example:

if (tenantConfiguration?.Value == null)
    {
        var configFromApi = await TenantAppService.FindTenantByIdAsync(id);
        var configFromCache = await Cache.GetAsync(cacheKey);
        
        if(configFromCache == null && configFromApi.Success == true)
        {
           throw new InvalidOperationException("The tenant was found but not added to the Cache by the API. Ensure that distributed cache is configured correctly.");
        }

        tenantConfiguration = configFromCache ?? configFromApi;
    }

Thanks

DworrallRC avatar Aug 08 '25 08:08 DworrallRC