Base solution for your next web application
Open Closed

SetTenantId doesn't work first time #1182


User avatar
0
nikschuiling created

Hi,

I have the following code:

[UnitOfWork]
        public async Task GetFinancialConnector(AdministrationIdentifier administration)
        {
            var results1 = _administrationRepository.GetAllList(); // Returns all 8 administrations --> correct

            UnitOfWorkManager.Current.SetTenantId(administration.TenantId);
            
            var results2 = _administrationRepository.GetAllList(); // Returns 0 administrations --> incorrect!

            UnitOfWorkManager.Current.SetTenantId(administration.TenantId);

            var results3 = _administrationRepository.GetAllList(); // Returns all 8 administrations --> correct
            
        }

Administration implements IMustHaveTenant. This method is called with a non null value for administration.TenantId, e.g. 1.

After calling SetTenantId the first time _administrationRepository.GetAllList() doesn't return any results. At this moment FilterParameter MustHaveTenant has no parameters, which is strange because SetTenantId was called before with a tenant id. After calling SetTenantId the second time _administrationRepository.GetAllList() returns the correct results. At this moment the parameter of MustHaveTenant is correctly set.

Can someone clarify why calling SetTenantId once doesn't give the desired result?

Note: this code is running in a console app. AbpSession.TenantId is null. If I register a custom IAbpSession and set TenantId to a non null value, calling SetTenantId once works perfectly.

Thanks.

Kind regards,

Nik


6 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    Is your GetFinancialConnector method called over an iterface? How do you inject it while using. Also, is it application service? Did you try to make this method virtual?

  • User Avatar
    0
    nikschuiling created

    Is your GetFinancialConnector method called over an iterface?

    Yes it is.

    How do you inject it while using.

    In the console app I use AbpBootstrapper. To resolve IFinancialConnectorService I use:

    IFinancialConnectorService financialConnectorService = IocManager.Instance.Resolve<IFinancialConnectorService>();
    

    Also, is it application service?

    No, the base class of FinancialConnectorService is MyAppServiceBase. FinancialConnectorService furthermore implements ITransientDependency.

    Did you try to make this method virtual?

    Yes, this doesn't change anything.

  • User Avatar
    0
    hikalkan created
    Support Team

    Thank you for your answers. Let us deeply check and test it.

  • User Avatar
    0
    hikalkan created
    Support Team

    Can you try to enable filter first:

    unitOfWorkManager.Current.EnableFilter(AbpDataFilters.MustHaveTenant);

    Write this before first SetTenantId()

  • User Avatar
    0
    nikschuiling created

    This works correctly, thank you. Should I always call UnitOfWorkManager.Current.EnableFilter(AbpDataFilters.MustHaveTenant) before calling UnitOfWorkManager.Current.SetTenantId or is the observed behaviour considered a bug?

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    Normally, yes, you should enable a filter before setting it's value.

    But I made it automatically: When you set it to a non-null value, MustHaveTenant filter will be automatically enabled. This will be released with ABP v0.9.2.

    Thanks.