我正在尝试创建一个简单的网站。 现在,我最大的目标是测试我的DI方法,看看一切是否正常,但是我遇到了这个错误
尝试激活“ Data.Services.VehicleService”时,无法解析类型为“ System.Linq.IQueryable`1 [Data.Entities.VehicleMakeEntity]”的服务。
这是VehicleMakeEntity类
public class VehicleMakeEntity
{
public int Id { get; set; }
public string Name { get; set; }
public string Abrv { get; set; }
public List<VehicleModelEntity> Models { get; set; }
}
VehicleService类中的方法
public IEnumerable<VehicleMakeEntity> GetMake(int index, int count, Expression<Func<VehicleMakeEntity, int>> orderLambda)
{
var data = _makeRepository.SelectListMake(index, count, orderLambda).AsQueryable();
return data;
}
存储库类中的方法
private readonly IQueryable<VehicleMakeEntity> source;
public IEnumerable<VehicleMakeEntity> SelectListMake(int index, int count, Expression<Func<VehicleMakeEntity, int>> orderLambda)
{
return source.Skip(index * count).Take(count).OrderBy(orderLambda).AsQueryable();
}
我在DI容器中添加服务的方式
services.AddScoped<IModelRepository, ModelRepository>();
services.AddScoped<IMakeRepository, MakeRepository>();
services.AddSingleton<IVehicleService, VehicleService>();
我要测试的是该方法是否有效,所以在控制器中我有这个
public class HomeController : Controller
{
IVehicleService _vehicleService;
public HomeController(IVehicleService vehicleService)
{
_vehicleService = vehicleService;
}
public IActionResult Index()
{
var data = _vehicleService.GetMake(3, 2, (p => p.Id));
return View();
}
我找不到导致IQueryable错误的原因。
编辑:
这是服务构造函数
public VehicleService(IMakeRepository makeRepository, IModelRepository modelRepository, ProjectDbContext context)
{
_makeRepository = makeRepository;
_modelRepository = modelRepository;
}
我不小心在此处留下了IQueryable参数,这就是错误的原因。 现在我得到了这个错误
无法使用单例“ Data.Interfaces.IVehicleService”中的作用域服务“ Data.Interfaces.IMakeRepository”。
IMakeRepository
public interface IMakeRepository : IDisposable
{
void Insert(VehicleMakeEntity entity);
void Update(VehicleMakeEntity entity);
void Delete(VehicleMakeEntity entity);
IEnumerable<VehicleMakeEntity> SelectListMake(int index, int count, Expression<Func<VehicleMakeEntity, int>> orderLambda);
}
IVehicleService
public interface IVehicleService
{
IEnumerable<VehicleMakeEntity> GetMake(int index, int count, Expression<Func<VehicleMakeEntity, int>> orderLambda);
IEnumerable<VehicleModelEntity> GetModel(int index, int count, Expression<Func<VehicleModelEntity, int>> orderLambda);
}