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

viv*_*una 2 c# dependency-injection castle-windsor aspnetboilerplate

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

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>();
    }
}
Run Code Online (Sandbox Code Playgroud)

存储库:

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)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

错误:

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.
Run Code Online (Sandbox Code Playgroud)

aar*_*ron 9

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)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

AutoRepositoryTypes添加到您的DbContext

[AutoRepositoryTypes(
    typeof(IRepository<>),
    typeof(IRepository<,>),
    typeof(TestRepositoryBase<>),
    typeof(TestRepositoryBase<,>)
)]
public class TestDbContext : AbpDbContext
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

Target 没有实现接口 Abp.Domain.Repositories.IRepository`1[[Abp.Localization.Appl?icationLanguage, Abp.Zero.Common, Version=2.3.0.0, Culture=neutral, PublicKeyToken=null]] 参数名:target

注册TestRepository

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

解释:

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

参考:

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


缺少类型映射配置或不受支持的映射。映射类型:TestDetailsDTO -> Test 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>();
        });
    }
}
Run Code Online (Sandbox Code Playgroud)


Tyr*_*ley 8

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

我的界面被命名为 ILeadStatusesAppService

我的具体对象被命名为 LeadStatusAppService

如您所见,我有单数状态与复数状态。在给他们两个复数名称(或单数)之后,它起作用了。