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)
-
0
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 -
0
-
0
hi
Will an error occur when you call this in the controller? Please share your project structure.
-
0
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; }
-
0
Please share your project structure.
-
0
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.
-
0
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.
-
0
ok, Can I check it remotely? liming.ma@volosoft.com
-
0
The problem is solved, we need to reference the module that defines the features.
-
0
Thank you for your help maliming.