简体   繁体   中英

ABP IRepository<TEntity> from Service Provider is empty

I have a Console app that depends on different projects (I use DependsOn() notation) in my console module.

[DependsOn(
   typeof(AbpAutofacModule),
   typeof(SecondProjectModule),
   // Other dependencies
)]
public class MyConsoleAppModule...

Then, my console app calls a method in the second project and there I try to get an IRepository using the service provider like this:

using (var scope = SecondProjectModule.GetScope())
{
    using (var uow = scope.ServiceProvider.GetService<IUnitOfWorkManager>().Begin())
    {
        var repo = scope.ServiceProvider.GetService<IReadOnlyRepository<MyEntity>>();
        return ... // LINQ here
    }
}

But the repo is empty with null for DbContext and other properties. If I call this method using my WebApp project it runs perfectly.

The GetScope() method in the SecondProjectModule is very simple:

public static IServiceScope GetScope(IServiceProvider serviceProvider = null)
{
    var provider = serviceProvider ?? _serviceProvider;

    return provider?
       .GetRequiredService<IHybridServiceScopeFactory>()
       .CreateScope();
}

Not sure where is the issue here since the dependencies from my ConsoleApp projects seems to be fine.

The implementation of repository is located under EfCore or MongoDB layers of your second module.

Web application works fine because it has dependency for db integration layer (EfCore or MongoDB) which contains the implementation of repositories.

Assuming your SecondProjectModule is an Application Service, you need a reference to, and depends on db integration layer of that module (EfCore or MongoDB) aswell.

You can check the docs about layering and notice that application service doesn't have any dependency to EfCore or MongoDB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM