Open Closed

Can't inject repositories on BackgroundWorker #2016


User avatar
0
LuisSpotOn created

Hello,

I am building a background worker by following the docs and for some reason i can't inject any repository. When i try to inject the IUserRepository i get the exception on this message and when i try to inject a repository created with ABP Suite i get that the DbContext has already been disposed.

If you're creating a bug/problem report, please include followings:

  • ABP Framework version: v4.4.2

  • UI type: MVC

  • DB provider: EF Core

  • Tiered (MVC) or Identity Server Separated (Angular): no

  • Exception message and stack trace:

Autofac.Core.Registration.ComponentNotRegisteredException: The requested service 'Volo.Abp.Users.IUserRepository`1[[Test.Users.AppUser, Test.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.
   at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType)
   at Autofac.Extensions.DependencyInjection.AutofacServiceProvider.GetRequiredService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Test.BackgroundWorkers.ExportInvoiceItemsToStripeBackgroundWorker.DoWorkAsync(PeriodicBackgroundWorkerContext workerContext) in C:\Dev\Test\src\Test.Application\BackgroundWorkers\ExportInvoiceItemsToStripeBackgroundWorker.cs:line 23
  • Steps to reproduce the issue:"
public class TestBackgroundWorker : AsyncPeriodicBackgroundWorkerBase
{
 public TestBackgroundWorker(AbpAsyncTimer timer, IServiceScopeFactory serviceScopeFactory) : base(timer, serviceScopeFactory)
 {
    Timer.Period = (int) TimeSpan.FromMinutes(1).TotalMilliseconds;
 }

 protected override async Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext)
 {
    var userRepository = workerContext.ServiceProvider.GetRequiredService<IUserRepository<AppUser>>();
 }
}
Markdown supported.
Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)

3 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    The requested service 'Volo.Abp.Users.IUserRepository`1[[Test.Users.AppUser, Test.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' has not been registered

    Can you resolve IUserRepository< AppUser> service in App Service?

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    LuisSpotOn created

    Hello,

    Sorry, IUserRepository<AppUser> does not work even on services but IRepository<AppUser, Guid> does work, but on background worker it says that the DBContext has already been disposed.

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    I guess you are using the Unify DbContext, So you should using IRepository<IdentityUser, Guid>.

    https://community.abp.io/articles/unifying-dbcontexts-for-ef-core-removing-the-ef-core-migrations-project-nsyhrtna

    but on background worker it says that the DBContext has already been disposed.

    Please try to start a new uow in your background worker method.

    https://docs.abp.io/en/abp/latest/Unit-Of-Work#begin-a-new-unit-of-work

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.8.0-preview. Updated on September 09, 2026, 11:56
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.