Open Closed

Can't inject repositories on BackgroundWorker #2016


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>>();
 }
}

3 Answer(s)
  • 0
    maliming created
    Support Team

    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?

  • 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.

  • 0
    maliming created
    Support Team

    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