【发布时间】:2021-03-14 03:00:35
【问题描述】:
我是 .NET 的初学者,我学习按照教程进行操作,但运行程序时出现此消息错误
System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: pwiapi.Data.ILineRepo Lifetime: Scoped ImplementationType: pwiapi.Data.SqlLineRepo': Unable to resolve service for type 'pwiapi.Data.LineContext' while attempting to activate 'pwiapi.Data.SqlLineRepo'.
---> System.InvalidOperationException: Unable to resolve service for type 'pwiapi.Data.LineContext' while attempting to activate 'pwiapi.Data.SqlLineRepo'.
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(ServiceDescriptor serviceDescriptor, CallSiteChain callSiteChain)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.ValidateService(ServiceDescriptor descriptor)
--- End of inner exception stack trace ---
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.ValidateService(ServiceDescriptor descriptor)
at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable`1 serviceDescriptors, IServiceProviderEngine engine, ServiceProviderOptions options)
这是我的代码 startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ConfigurationContext>(options => { options.UseSqlServer(Configuration.GetConnectionString("ConnectionOne"));
});
services.AddControllers();
services.AddScoped<ILineRepo,SqlLineRepo>();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "pwiapi", Version = "v1" });
});
}
所以这是我的数据 (SqlLineRepo.cs):
public class SqlLineRepo : ILineRepo
{
private readonly LineContext _context;
public SqlLineRepo(LineContext context)
{
_context = context;
}
public Line GetLineByNo(string lineNo)
{
return _context.Lines.FirstOrDefault(p => p.LINE_NO == lineNo);
}
public IEnumerable<Line> GetLines()
{
return _context.Lines.ToList();
}
}
这是我的界面 (ILineRepo.cs)
using pwiapi.Models;
using System.Collections.Generic;
namespace pwiapi.Data
{
public interface ILineRepo
{
IEnumerable<Line> GetLines();
Line GetLineByNo(string lineNo);
}
}
这是我的控制器:
namespace pwiapi.Controllers
{
[Route("api/commands")]
[ApiController]
public class LineController : ControllerBase
{
private readonly ILineRepo _repository;
public LineController(ILineRepo repository) {
_repository = repository;
}
//private readonly MockLineRepo _repository = new MockLineRepo();
[HttpGet]
public ActionResult<IEnumerable<Line>> GetAllLines() {
var lineItems = _repository.GetLines();
return Ok(lineItems);
}
[HttpGet("{lineno}")]
public ActionResult<Line> GetLineByNo(string lineno) {
var lineItem = _repository.GetLineByNo(lineno);
return Ok(lineItem);
}
}
}
任何机构都可以帮助解决和解释这里发生的事情? 我正在寻找如何解决这个问题,但程序仍然错误,我无法理解
【问题讨论】:
-
startup.cs 中有
services.AddDbContext<LineContext>吗? -
哦找到了问题,在启动时我使用 configurationcontext 但在 sqlRepo 我使用 LineCONtext ...谢谢老兄
标签: asp.net asp.net-core asp.net-core-webapi