admin管理员组文章数量:1417070
I am doing a tutorial and I keep getting this error:
Unhandled exception. System.InvalidOperationException: Unable to find the required services. Please add all the required services by calling 'IServiceCollection.AddControllers' inside the call to 'ConfigureServices(...)' in the application startup code.
at Microsoft.AspNetCore.Builder.ControllerEndpointRouteBuilderExtensions.EnsureControllerServices(IEndpointRouteBuilder endpoints)
at Microsoft.AspNetCore.Builder.ControllerEndpointRouteBuilderExtensions.MapControllers(IEndpointRouteBuilder endpoints)
at Program.$(String[] args) in C:\Users\Yuri\web-projects\restore\api\Program.cs:line 16
I believe it's asking me to add something to my program.cs file but I'm not sure what.
program.cs
:
using API.Data;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddOpenApi();
builder.Services.AddDbContext<StoreContext>(opt =>
{
opt.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection"));
});
var app = builder.Build();
app.MapControllers();
Dbinitializer.InitDb(app);
app.Run();
At first I thought Dbinitializer.InitDb(app);
was the problem, so I commented it out, but the error didn't go away.
I am doing a tutorial and I keep getting this error:
Unhandled exception. System.InvalidOperationException: Unable to find the required services. Please add all the required services by calling 'IServiceCollection.AddControllers' inside the call to 'ConfigureServices(...)' in the application startup code.
at Microsoft.AspNetCore.Builder.ControllerEndpointRouteBuilderExtensions.EnsureControllerServices(IEndpointRouteBuilder endpoints)
at Microsoft.AspNetCore.Builder.ControllerEndpointRouteBuilderExtensions.MapControllers(IEndpointRouteBuilder endpoints)
at Program.$(String[] args) in C:\Users\Yuri\web-projects\restore\api\Program.cs:line 16
I believe it's asking me to add something to my program.cs file but I'm not sure what.
program.cs
:
using API.Data;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddOpenApi();
builder.Services.AddDbContext<StoreContext>(opt =>
{
opt.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection"));
});
var app = builder.Build();
app.MapControllers();
Dbinitializer.InitDb(app);
app.Run();
At first I thought Dbinitializer.InitDb(app);
was the problem, so I commented it out, but the error didn't go away.
2 Answers
Reset to default 3Error message is arguably self-explanatory:
Please add all the required services by calling 'IServiceCollection.AddControllers' inside the call to 'ConfigureServices(...)' in the application startup code.
You are calling app.MapControllers();
to expose endpoints defined in your controllers and this requires some internal services to be registered (which are not needed for cases like Minimal APIs).
So add the following call to your code:
builder.Services.AddControllers();
You're seeing this error because the necessary controller services aren't registered with the dependency injection container. When you call app.MapControllers()
, ASP.NET Core expects the required services (like routing, controller activation, etc.) to have been added.
The Fix:
builder.Services.AddControllers();
本文标签: cUnhandled exception SystemInvalidOperationExceptionStack Overflow
版权声明:本文标题:c# - Unhandled exception. System.InvalidOperationException - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745257148a2650169.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论