我得到这个错误:
无法解析类型的服务
我正在使用.NET Core上的工作单元存储库项目。我在另一层上有一个服务,但我更改为TestServices,然后我创建了一个服务层,其中有FlightService
和IFlightService
。
项目
builder.Services.AddDbContext<DataContext>(options =>
options.UseSqlServer(builder.Configuration
.GetConnectionString("ConnectionString")));
builder.Services.AddControllers().AddJsonOptions(x =>
x.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles);
builder.Services.AddTransient<IUnitOfWork, UnitOfWork>();
builder.Services.AddScoped<IFlightRepository, FlightRepository>();
builder.Services.AddControllers();
FlightController
:
[ApiController]
[Route("Api")]
public class FlightController : Controller
{
private readonly ILogger<SearchFlightsController> _logger;
private readonly IUnitOfWork unitOfWork;
public readonly IFlightService _flightService;
public FlightController(ILogger<SearchFlightsController> logger,
IUnitOfWork unitOfWork,
IFlightService flightService)
{
_logger = logger;
this.unitOfWork = unitOfWork;
_flightService = flightService;
}
[HttpGet("GetFlightById/{flightId}")]
public async Task<IActionResult> GetFlightById(int flightId)
{
var details = await _flightService.GetFlightById(flightId);
if (details != null)
{
return Ok(details);
}
else
{
return BadRequest();
}
}
}
错误
System.InvalidOperationException:在试图激活“NewShore.Controllers. FlightController”时无法解析类型“Services.Interfaces.IFlightService”的服务。
在Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp,Type type,Type requiredBy,Boolean isDefaultParameterRequired)
at lambda_method4(Closure,IServiceProvider,Object[])
位于Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.〈〉c__DisplayClass6_0.g__CreateController|0(ControllerContext controllerContext)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next,Scope& scope,Object& state,Boolean& isCompleted)
在Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
---从上一个位置开始的堆栈跟踪结束---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__等待|20_0(ResourceInvoker invoker,Task lastTask,State next,Scope scope,Object state,Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__等待|17_0(ResourceInvoker调用程序,Task任务,IDisposable作用域)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__等待|17_0(ResourceInvoker调用程序,Task任务,IDisposable作用域)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(端点endpoint、任务requestTask、ILogger记录器)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext上下文)
在Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
在Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext,ISwaggerProvider swaggerProvider)
在Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext上下文)
我读了一些帖子,我试着把AddScoped
都换成AddScoped
,AddTransient
换成IFlightRepository
。我看到了Program.cs
上的命令或命令,但一切似乎都没问题。
你能纠正我吗?
1条答案
按热度按时间myss37ts1#
您将IFlightService注入到FlightController中,但未注册此依赖项。错误消息告诉您无法解析FlightController中的依赖项。
将此行添加到Program.cs