无法创建组件,因为它在 ASP.NET 样板中需要满足依赖关系
Can't create component as it has dependencies to be satisfied in ASP.NET Boilerplate
我在 运行 测试时遇到错误。我该如何解决?
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.
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.
在您的 DbContext
中添加 DbSet<Test>
:
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 does not implement interface Abp.Domain.Repositories.IRepository`1[[Abp.Localization.ApplicationLanguage, Abp.Zero.Common, Version=2.3.0.0, Culture=neutral, PublicKeyToken=null]] Parameter name: target
注册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>
。因此,您需要通过注册您的具体 class.
来替换 IRepository<Test, Int>
参考:
https://aspnetboilerplate.com/Pages/Documents/Entity-Framework-Core#replacing-default-repositories
Missing type map configuration or unsupported mapping. Mapping types: TestDetailsDTO -> Test Abc.Xyz.Business.Dto.Tests.TestDetailsDTO -> Abc.Xyz.Business.Model.Taxes.Test
在 TestDetailsDTO
上添加 [AutoMap(Test)]
属性。
如果这不起作用,请尝试手动配置映射:
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>();
});
}
}
我通过更正错字解决了这个问题。 Windsor 依赖注入似乎遵循严格的命名约定。
我的界面被命名为 ILeadStatusesAppService
我的具体对象被命名为 LeadStatusAppService
如您所见,我有单数状态和复数状态。在给他们两个复数名称(或单数)后它起作用了。
我遵循了 ABP 推荐的命名约定,但没有成功。我尝试再次重构代码,但没有成功。奇怪的是,一切都在调试中运行良好,但当我尝试发布或构建发布时,错误又回来了。
经过几天的调试和重构,我决定将Release文件夹与Debug进行对比。我注意到 Release 中的 Entity Framework Class Library 与 Debug 不同。日期不对。然后它点击了,它无法创建存储库的原因是它实际上不可用,因为 EF Class 库很旧并且在 Release 中没有,但在 Debug 中有。因此,我只是再次清理了所有内容,并确保 EF 在 Release 中构建了一个新的 DLL 并且它可以正常工作。
这似乎是一个容易解决的问题,但花了一些时间才弄清楚:)
我遇到了类似的问题,结果发现存在循环依赖情况。两种服务都被注入彼此的实例并等待解决。
我更正了这个错误并解决了。
我有同样的问题,但我意识到我只是忘记实现我创建的应用程序服务接口,当我从控制器调用它时,我得到了这个错误
我在 运行 测试时遇到错误。我该如何解决?
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.
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.
在您的 DbContext
中添加 DbSet<Test>
:
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 does not implement interface Abp.Domain.Repositories.IRepository`1[[Abp.Localization.ApplicationLanguage, Abp.Zero.Common, Version=2.3.0.0, Culture=neutral, PublicKeyToken=null]] Parameter name: target
注册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>
。因此,您需要通过注册您的具体 class.
IRepository<Test, Int>
参考:
https://aspnetboilerplate.com/Pages/Documents/Entity-Framework-Core#replacing-default-repositories
Missing type map configuration or unsupported mapping. Mapping types: TestDetailsDTO -> Test Abc.Xyz.Business.Dto.Tests.TestDetailsDTO -> Abc.Xyz.Business.Model.Taxes.Test
在 TestDetailsDTO
上添加 [AutoMap(Test)]
属性。
如果这不起作用,请尝试手动配置映射:
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>();
});
}
}
我通过更正错字解决了这个问题。 Windsor 依赖注入似乎遵循严格的命名约定。
我的界面被命名为 ILeadStatusesAppService
我的具体对象被命名为 LeadStatusAppService
如您所见,我有单数状态和复数状态。在给他们两个复数名称(或单数)后它起作用了。
我遵循了 ABP 推荐的命名约定,但没有成功。我尝试再次重构代码,但没有成功。奇怪的是,一切都在调试中运行良好,但当我尝试发布或构建发布时,错误又回来了。
经过几天的调试和重构,我决定将Release文件夹与Debug进行对比。我注意到 Release 中的 Entity Framework Class Library 与 Debug 不同。日期不对。然后它点击了,它无法创建存储库的原因是它实际上不可用,因为 EF Class 库很旧并且在 Release 中没有,但在 Debug 中有。因此,我只是再次清理了所有内容,并确保 EF 在 Release 中构建了一个新的 DLL 并且它可以正常工作。
这似乎是一个容易解决的问题,但花了一些时间才弄清楚:)
我遇到了类似的问题,结果发现存在循环依赖情况。两种服务都被注入彼此的实例并等待解决。
我更正了这个错误并解决了。
我有同样的问题,但我意识到我只是忘记实现我创建的应用程序服务接口,当我从控制器调用它时,我得到了这个错误