[英]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()表示法)。
[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:然后,我的控制台应用程序调用第二个项目中的一个方法,然后我尝试使用服务提供商获取一个 IRepository,如下所示:
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.但是对于DbContext和其他属性,回购是空的,其中包含 null。 If I call this method using my WebApp project it runs perfectly.如果我使用我的 WebApp 项目调用此方法,它会完美运行。
The GetScope() method in the SecondProjectModule is very simple: SecondProjectModule 中的 GetScope() 方法非常简单:
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.不确定这里的问题出在哪里,因为我的 ConsoleApp 项目的依赖项似乎没问题。
The implementation of repository is located under EfCore or MongoDB layers of your second module.存储库的实现位于第二个模块的EfCore或MongoDB层下。
Web application works fine because it has dependency for db integration layer (EfCore or MongoDB) which contains the implementation of repositories. Web 应用程序工作正常,因为它依赖于包含存储库实现的数据库集成层(EfCore 或 MongoDB)。
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.假设您的SecondProjectModule是一个应用程序服务,您需要引用并依赖于该模块(EfCore 或 MongoDB)的数据库集成层。
You can check the docs about layering and notice that application service doesn't have any dependency to EfCore or MongoDB.您可以查看有关分层的文档,并注意到应用程序服务与 EfCore 或 MongoDB 没有任何依赖关系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.