简体   繁体   English

ABP IRepository<tentity> 来自服务提供商的是空的</tentity>

[英]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.存储库的实现位于第二个模块的EfCoreMongoDB层下。

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.

相关问题 无法为服务类型“IRepository”实例化实现类型 Repository“1[TDBContext]”。 - Cannot instantiate implementation type Repository`1[TDBContext]' for service type 'IRepository'.' CS1061 IOrderedEnumerable<tentity> ' 不包含 'ToListAsync' EF Core 6 的定义</tentity> - CS1061 IOrderedEnumerable<TEntity>' does not contain a definition for 'ToListAsync' EF Core 6 从 Azure Function 应用程序调用 WCF 服务 - Calling WCF Service from Azure Function App 您如何部署 ABP.IO 应用程序模板项目? - How do you deploy ABP.IO application template projects? 如何将空的 object 从 appsettings.json 加载到字典中? - How can I load an empty object into a dictionary from appsettings.json? 从 REST 服务获取 base64 中无编码/解码的字节数组 - 可能吗? - Get a byte array no encode/decode in base64 from REST Service - It's possible? 如何在我的 CORS 服务中使用 appsettings.json 中的逗号分隔字符串? - How do I use a comma seperated string from appsettings.json in my CORS service? 是否有一种动态方式将 class 名称作为属性包含在 JSON 从 web 服务返回值中 - Is there a dynamic way of including class name as property in JSON return value from web service 将服务从动态加载的程序集添加到 ServiceCollection? - Adding Service from dynamically loaded Assembly to ServiceCollection? .NET6:如何实现从 controller 到服务以及从服务到 DbContext 的依赖注入 - .NET6: How to implement Dependency Injection from controller to service and from service to DbContext
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM