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)
-
0
Hi,
You can register it in
services.AddAbp
. How do you register it ? -
0
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)
-
0
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)); });
-
0
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();
-
0
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.
-
0
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,