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();
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745257149a4619024.html
评论列表(0条)