Base solution for your next web application
Open Closed

How do I register my IAsyncActionFilter so that I have access to the database? #8985


User avatar
0
adam.langley created

I have written an IAsyncActionFilter which needs to access the UOW after the Controller has executed its action.

My ActionFilter consumes IDbContextProvider<ProductDbContext> so that I can call "GetDbContext" after the inner action has executed.

When I do so however, I get:

System.ArgumentNullException: Value cannot be null. (Parameter 'unitOfWork') at Abp.EntityFrameworkCore.Uow.UnitOfWorkExtensions.GetDbContext[TDbContext](IActiveUnitOfWork unitOfWork, Nullable`1 multiTenancySide, String name)

I expect this is before my filter is scheduled to "wrap" the UOW filter - meaning that the UOW is not available when my filter starts, and the UOW has already been disposed by the time my filter regains control.

How do I get my filter to execute in the correct order?

Thanks,


7 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You can register it in services.AddAbp. How do you register it ?

  • User Avatar
    0
    adam.langley created

    Hi,

    Can you be more specific please? Your suggestion is similar to what I saw in ABP docs, however the only reference I found, similar to "AddAbp" was in my Web.Host/Startup.cs:

    //Configure Abp and Dependency Injection
    return services.AddAbp<WmwWebHostModule>(options =>
    {
    

    However, the "options" being passed here does not have a "Filters" property, or anything where I can call "Filters.Add" or "AddService".

    So, the following 2 snippets are what I tried (both had the same results, the filter ran, but could not obtain a DbContext)

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Sorry, can you try like below right before services.AddAbp you have found in Startup.cs;

    services.Configure<MvcOptions>(options =>
                {
                    options.Filters.Add(typeof(YourFilter));
                });
    
  • User Avatar
    0
    adam.langley created

    Hi there,

    Thanks for the extra details. The error still occurs. The following code cannot be called inside an IAsyncActionFilter.OnActionExecutionAsync without generating a null ref error.

    var _dbContext = _dbContextProvider.GetDbContext();

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Could you create an UnitOfWork inside your filter like below and try agian ?

    
    using (var uow = _unitOfWorkManager.Begin(TransactionScopeOption.Suppress))
                {
                     ///your code here...
                    uow.Complete();
                }
    

    You can also take a look at https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.AspNetCore/AspNetCore/Mvc/Auditing/AbpAuditActionFilter.cs as a sample filter.

  • User Avatar
    0
    adam.langley created

    Hi,

    I got this to work, now I want to package this into a Module. How now do I get access to the "Services" collection from inside an AbpModule to I can register the filter?

    // how can I get access to "services" within AbpModule.Initialize, or PostInitialize... etc? services.Configure<MvcOptions>(options => { options.Filters.Add(typeof(YourFilter)); });

    Thanks,

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You can create an extension method like this and call it from your App's Startup.cs.