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.

Share Improve this question edited Feb 2 at 21:01 Guru Stron 144k11 gold badges172 silver badges212 bronze badges asked Feb 2 at 20:47 Yuri MckoyYuri Mckoy 295 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 3

Error 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