Open Closed

PermissionDefinitionProvider - Create permissions based on values in a table #3004


User avatar
0
Spospisil created
  • ABP Framework version: v5.1.3
  • UI type: MVS
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): Tiered MVC

Is it possible to call one of my Application Services (that returns a List collection from values from the DB) within a class that inherits from the PermissionDefinitionProvider to dynamically create PermissionDefinition objects? I create an instance of my application service using the **var permissionService = context.ServiceProvider.GetRequiredService(); **statement but after the service calls the supporting repository lookup I get an "Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it (or one of its parent scopes) has already been disposed."

This same application service/method is used elsewhere in my application without any issues, so it appears to not work when I use it within a class that inherits from the PermissionDefinitionProvider class.

This exception was originally thrown at this call stack: Autofac.Core.Lifetime.LifetimeScope.CheckNotDisposed() Autofac.Core.Lifetime.LifetimeScope.BeginLifetimeScope(object) Autofac.Core.Lifetime.LifetimeScope.BeginLifetimeScope() Autofac.Extensions.DependencyInjection.AutofacServiceScopeFactory.CreateScope() Volo.Abp.Uow.UnitOfWorkInterceptor.InterceptAsync(Volo.Abp.DynamicProxy.IAbpMethodInvocation) in UnitOfWorkInterceptor.cs System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task) System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult() Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter.InterceptAsync(Castle.DynamicProxy.IInvocation, Castle.DynamicProxy.IInvocationProceedInfo, System.Func<Castle.DynamicProxy.IInvocation, Castle.DynamicProxy.IInvocationProceedInfo, System.Threading.Tasks.Task>) ... [Call Stack Truncated]


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

    Can you share your code or steps? thanks.

  • User Avatar
    0
    Spospisil created

    What is you email address and I'll share a repo with you

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    shiwei.liang@volosoft.com

  • User Avatar
    0
    Spospisil created

    I've added you to it.

    Run the dbmigrator to create and seed the DB then run the tiered layers

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Thanks, I will check it out.

  • User Avatar
    0
    Spospisil created

    Any update on this?

    Thanks

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Method Define of class PermissionDefinitionProvider is not an async method.

    You can try this:

    public override void Define(IPermissionDefinitionContext context)
    {
        var permissionService = context.ServiceProvider.GetRequiredService<IPermAppService>();
    
        var myGroup = context.AddGroup(VoloTestPermissions.GroupName);
    
        myGroup.AddPermission(VoloTestPermissions.Dashboard.Host, L("Permission:Dashboard"), MultiTenancySides.Host);
        myGroup.AddPermission(VoloTestPermissions.Dashboard.Tenant, L("Permission:Dashboard"), MultiTenancySides.Tenant);
    
        var permissions = AsyncHelper.RunSync(()=> permissionService.GetPermissionListAsync(new GetPermissionInput())) ;
        //foreach (var perm in permissions)
        //{
        //    myGroup.AddPermission($"{perm.Name}.Allow", L($"{perm.Name}.Allow"));
        //    myGroup.AddPermission($"{perm.Name}.Deny", L($"{perm.Name}.Deny"));
    
    }
    
  • User Avatar
    0
    Spospisil created

    Still does not resolve the issue. Did you try it after to changed the things you recommended?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Of course, it works for me.

  • User Avatar
    0
    Spospisil created

    Let me see if I can't figure out why it still does not work. Can you update the repo with your changes applied please?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    I did two things:

    1: Rename IPermAppService to IPermissionAppAervice 2: https://support.abp.io/QA/Questions/3004#answer-0092715c-57b9-6445-6cc3-3a0394b8e680

  • User Avatar
    0
    Spospisil created

    Thank you. That was the issue.

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11