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

时间:2017-09-29 07:36:52

标签: 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>();
    }
}

存储库:

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.

5 个答案:

答案 0 :(得分:3)

  

Castle.MicroKernel.Handlers.HandlerException:无法创建组件&#39; ABC.XYZ.Business.Services.PQR.TestAppService&#39;因为它有依赖性来满足。

     

&#39; ABC.XYZ.Business.Services.PQR.TestAppService&#39;正在等待以下依赖项:
   - 服务&#39; ABC.XYZ.Business.Repositories.Interfaces.ITestRepository&#39;没有注册。

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
{
    ...
}
  

Target没有实现接口Abp.Domain.Repositories.IRepository`1 [[Abp.Localization.Appl icationLanguage,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 - &gt;测试Abc.Xyz.Business.Dto.Tests.TestDetailsDTO - &gt; 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>();
        });
    }
}

答案 1 :(得分:0)

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

我的界面名为ILeadStatusesAppService

我的具体对象名为LeadStatusAppService

正如你所看到的,我有一个单数状态与复数状态。在给他们两个复数名称(或单数)后,它起作用了。

答案 2 :(得分:0)

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

经过几天的调试和重构,我决定将Release文件夹与Debug进行比较。我注意到发行版中的实体框架类库与调试不同。日期已过。然后它单击,它之所以不能创建存储库,是因为它实际上不可用,因为EF类库很旧,并且在Release中没有,但是在Debug中却没有。因此,我再次清理了所有内容,并确保EF在Release中构建了新的DLL,并且可以正常工作。

解决起来似乎很容易,但是花了一些时间才找到:)

答案 3 :(得分:0)

我正面临着类似的问题,事实证明,这是一个循环依赖的情况。两种服务都正在注入彼此的实例,并正在等待解决。

我已更正此错误,错误已解决。

答案 4 :(得分:0)

我遇到了同样的问题,但是我意识到我只是忘记了实现我创建的应用程序服务接口,当我从控制器调用它时,我遇到了这个错误