我正在使用Autofac.extras.moq
,当我使用Generic
IReadOnlyRepository<T> _campaignRepository;
IReadOnlyRepository<DMA> _dmaRepository;
public DigitalCampaignService(IUnitOfWork unitOfWork) : base(unitOfWork)
{
using (var mock = AutoMock.GetLoose())
{
mock.Provide(mock.Create<IReadOnlyRepository<T>>());
_dmaRepository = mock.Provide<IReadOnlyRepository<DMA>>(new GenericReadOnlyRepository<DMA>(Constants.CONNECTION_STRING));
}
}
函数 IReadOnlyRepository 需要是实例
namespace Kolabs.Infrastructure
{
public interface IReadOnlyRepository<T> where T : BaseEntity
{
IEnumerable<T> Find(Expression<Func<T, bool>> predicate, int? numberOfItems = default(int?));
IEnumerable<T> Find(Expression<Func<T, bool>> predicate, string orderBy, int? numberOfItems = default(int?));
IEnumerable<T> Find(Expression<Func<T, bool>> predicate, [Dynamic(new[] { false, false, false, true })] Expression<Func<T, dynamic>> orderBy, OrderDirection orderDirection = OrderDirection.Ascending, int? numberOfItems = default(int?));
IEnumerable<T> FindAll(int? numberOfItems = default(int?));
IEnumerable<T> FindAll(string orderBy, int? numberOfItems = default(int?));
T FindById(int id);
T FindOne(Expression<Func<T, bool>> predicate);
}
}
运行代码
时收到错误消息Autofac.dll中出现“Autofac.Core.Registration.ComponentNotRegisteredException”类型的异常,但未在用户代码中处理
其他信息:所请求的服务'Kolabs.Infrastructure.IReadOnlyRepository`1 [[Maestro.Model.Campaigns.FacebookCampaign,Maestro.Model,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]'尚未注册。要避免此异常,请注册组件以提供服务,使用IsRegistered()检查服务注册,或使用ResolveOptional()方法解析可选依赖项。
答案 0 :(得分:3)
更新软件包后我遇到了同样的错误。恢复到旧版本就行了。
不工作:
工作: