无法创建组件,因为它具有要在ASP.NET Boilerplate中满足的依赖关系

问题描述 投票:0回答:4

运行测试时出现错误。我该如何解决?

public class TestAppService : TestAppServiceBase, ITestAppService
{
    private readonly ITestRepository _testRepository;
    public TestAppService(ITestRepository testRepository)
    {
        _testRepository = testRepository;
    }
}

public class TestAppService_Test : TestAppServiceTestBase
{
    private readonly ITestAppService _testAppService;
    public TestAppService_Test()
    {
        _testAppService = Resolve<ITestAppService>();
    }
}

存储库:

public class TestRepository : TestRepositoryBase<Test, int>, ITestRepository
{
    private readonly IActiveTransactionProvider _transactionProvider;
    public TestRepository(IDbContextProvider<TestDbContext> dbContextProvider)
        : base(dbContextProvider)
    {
    }

    public TestRepository(IDbContextProvider<TestDbContext> dbContextProvider,
        IActiveTransactionProvider transactionProvider, IObjectMapper objectMapper)
    : base(dbContextProvider)
    {
        _transactionProvider = transactionProvider;
        ObjectMapper = objectMapper;
    }
}

public interface ITestRepository : IRepository<Test, int>
{
}

public class TestRepositoryBase<TEntity, TPrimaryKey> : EfCoreRepositoryBase<TestDbContext, TEntity, TPrimaryKey>
    where TEntity : class, IEntity<TPrimaryKey>
{
    public IObjectMapper ObjectMapper;
    public TestRepositoryBase(IDbContextProvider<TestDbContext> dbContextProvider)
        : base(dbContextProvider)
    {
    }
}

/// <summary>
/// Base class for custom repositories of the application.
/// This is a shortcut of <see cref="TestRepositoryBase{TEntity,TPrimaryKey}"/> for <see cref="int"/> primary key.
/// </summary>
/// <typeparam name="TEntity">Entity type</typeparam>
public class TestRepositoryBase<TEntity> : TestRepositoryBase<TEntity, int>
    where TEntity : class, IEntity<int>
{
    public TestRepositoryBase(IDbContextProvider<TestDbContext> dbContextProvider)
        : base(dbContextProvider)
    {
    }
}

错误:

Test Name:

ABC.XYZ.Tests.PQR.TestAppService_Test.Should_Create_Test_With_Valid_Arguments
Test FullName:  ABC.XYZ.Tests.PQR.TestAppService_Test.Should_Create_Test_With_Valid_Arguments (20f54c54e5d9f077f4cb38b988ecb8e63e07d190)
Test Source:    C:\Users\viveknuna\source\repos\XYZ\aspnet-core\test\ABC.XYZ.Tests\PQR\TestAppService_test.cs : line 25
Test Outcome:   Failed
Test Duration:  0:00:00.001

Result StackTrace:
at Castle.MicroKernel.Handlers.DefaultHandler.AssertNotWaitingForDependency()
   at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, Burden& burden)
   at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context, Boolean instanceRequired)
   at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service, IDictionary additionalArguments, IReleasePolicy policy)
   at Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(Type service, IDictionary arguments, IReleasePolicy policy)
   at Castle.Windsor.WindsorContainer.Resolve[T]()
   at ABC.XYZ.Tests.PQR.TestAppServiceTestBase..ctor() in C:\Users\viveknuna\source\repos\XYZ\aspnet-core\test\ABC.XYZ.Tests\PQR\TestAppServiceTestBase.cs:line 14
   at ABC.XYZ.Tests.PQR.TestAppService_Test..ctor() in C:\Users\viveknuna\source\repos\XYZ\aspnet-core\test\ABC.XYZ.Tests\PQR\TestAppService_test.cs:line 17
Result Message:
Castle.MicroKernel.Handlers.HandlerException : Can't create component 'ABC.XYZ.Business.Services.PQR.TestAppService' as it has dependencies to be satisfied.

'ABC.XYZ.Business.Services.PQR.TestAppService' is waiting for the following dependencies:
- Service 'ABC.XYZ.Business.Repositories.Interfaces.ITestRepository' which was not registered.
c# dependency-injection castle-windsor aspnetboilerplate
4个回答
5
投票

Castle.MicroKernel.Handlers.HandlerException:无法创建组件'ABC.XYZ.Business.Services.PQR.TestAppService',因为它具有要满足的依赖关系。

'ABC.XYZ.Business.Services.PQR.TestAppService'正在等待以下依赖项:-未注册的服务“ ABC.XYZ.Business.Repositories.Interfaces.ITestRepository”。

DbSet<Test>中添加DbContext

public class TestDbContext : AbpDbContext
{
    public DbSet<Test> Tests { get; set; }

    public TestDbContext(DbContextOptions<TestDbContext> options)
        : base(options)
    {
    }
}

AutoRepositoryTypes]添加到DbContext

[AutoRepositoryTypes(
    typeof(IRepository<>),
    typeof(IRepository<,>),
    typeof(TestRepositoryBase<>),
    typeof(TestRepositoryBase<,>)
)]
public class TestDbContext : AbpDbContext
{
    ...
}

目标未实现接口Abp.Domain.Repositories.IRepository`1 [[Abp.Localization.ApplicationLanguage,Abp.Zero.Common,Version = 2.3.0.0,Culture = neutral,PublicKeyToken = null]]参数名称:目标

寄存器TestRepository

Configuration.ReplaceService<IRepository<Test, int>>(() =>
{
    IocManager.IocContainer.Register(
        Component.For<IRepository<Test, int>, ITestRepository, TestRepository>()
            .ImplementedBy<TestRepository>()
            .LifestyleTransient()
    );
});

说明:

[注入ITestRepository时,它将尝试实例化定义的IRepository<Test, Int>。但是,您的TestRepository实现了自定义TestRepositoryBase<Test, int>。因此,您需要通过注册具体的类来替换IRepository<Test, Int>

参考:

https://aspnetboilerplate.com/Pages/Documents/Entity-Framework-Core#replacing-default-repositories


缺少类型映射配置或不支持的映射。映射类型:TestDetailsDTO->测试Abc.Xyz.Business.Dto.Tests.TestDetailsDTO-> Abc.Xyz.Business.Model.Taxes.Test

[AutoMap(Test)]上添加TestDetailsDTO属性。

如果不起作用,请尝试手动配置映射:

public class MyTestModule : AbpModule
{
    public override void PreInitialize()
    {
        Configuration.Modules.AbpAutoMapper().UseStaticMapper = false;
        Configuration.Modules.AbpAutoMapper().Configurators.Add(config =>
        {
            config.CreateMap<TestDetailsDTO, Test>();
            config.CreateMap<Test, TestDetailsDTO>();
        });
    }
}

2
投票

我通过修正我的错字解决了这个问题。似乎Windsor依赖项注入遵循严格的命名约定。


0
投票

我遵循ABP建议的命名约定,但没有运气。我尝试再次重构代码,但是没有运气。奇怪的是,一切都在调试中运行良好,但是当我尝试发布或构建发布版本时,错误又回来了。


0
投票

我面临着类似的问题,原来有一个循环依赖方案。两种服务都正在注入彼此的实例,并正在等待解决。

© www.soinside.com 2019 - 2024. All rights reserved.