Open Closed

IFeatureChecker does not work in a background process #2543


User avatar
0
bozkan created

I have a background service project, which I need to check if a custom feature is enabled. But the IFeatureChecker.IsEnabledAsync throws

Volo.Abp.AbpException: 'Undefined feature: YYY.XXX'

exception even if I use the _currentPrincipalAccessor.Change technique as stated in https://support.abp.io/QA/Questions/1201/FeatureChecker-is-not-working-when-changing-Tenant-by-code

Here is my code in background worker:

    public override async Task Execute(IJobExecutionContext context)
    {
        var tenants = await _tenantRepository.GetListAsync();

        for (int i = 0; i < tenants.Count; i++)
        {
            var tenant = tenants[i];

            using (_currentTenant.Change(tenant.Id))
            {
                var tenantEditionId = tenant.EditionId;

                var principalWithEditionIdClaim = new ClaimsPrincipal(
                    new ClaimsIdentity(
                        new Claim[]
                        {
                            new(AbpClaimTypes.EditionId, tenantEditionId.ToString()),
                        }));

                using (_currentPrincipalAccessor.Change(principalWithEditionIdClaim))
                {
                    var isFeatureEnabled = **await _featureChecker.IsEnabledAsync(FeatureConstants.FeatureXXX);**
                    if (!isFeatureEnabled)
                    {
                        continue;
                    }
                }

            }
        }
    }
  • ABP Framework version: v4.3.0
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes
  • Exception message and stack trace: Volo.Abp.AbpException: 'Undefined feature: YYY.XXX
  • Steps to reproduce the issue:"

10 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team Co-Founder

    IFeatureChecker is only for checking features for the current user and has limitations due to its design and purpose.

    Use IFeatureManager as documented here: https://docs.abp.io/en/abp/latest/Modules/Feature-Management#ifeaturemanager

  • User Avatar
    0
    bozkan created

    Thank you for your reply.

    I tried IFeatureManager but it didn't work either:

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Will an error occur when you call this in the controller? Please share your project structure.

  • User Avatar
    0
    bozkan created

    Hi Maliming,

    I am trying to access it inside a background hosted service project and inside a QuartzBackgroundWorkerBase derived class. (This is some background project for scheduled tasks.)

    Btw, as I look at the source code, both IFeatureChecker and IFeatureManager uses line:

    var featureDefinition = FeatureDefinitionManager.Get(name);

    and I think this line throws the "Undefined feature" exception. But I don't know why.

        public virtual FeatureDefinition Get(string name)
        {
            Check.NotNull(name, nameof(name));
    
            var feature = GetOrNull(name);
    
            if (feature == null)
            {
                throw new AbpException("Undefined feature: " + name);
            }
    
            return feature;
        }
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Please share your project structure.

  • User Avatar
    0
    bozkan created

    As this is a commercial project, I would rather not share the project structure. But I can give detail about what you are trying to learn.

    In addition, when I try to access the feature (using IFeatureChecker) in a plain controller or app service method it works.

  • User Avatar
    0
    bozkan created

    By the way, as we are an Enterprise License customer, I have also sent an email to the volosoft contact email address about the issue. (But didn't have a response yet) If we communicate over there maybe I can give more info as it will not be public.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    ok, Can I check it remotely? liming.ma@volosoft.com

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    The problem is solved, we need to reference the module that defines the features.

  • User Avatar
    0
    bozkan created

    Thank you for your help maliming.

Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09