c# - Unhandled exception. System.InvalidOperationException - Stack Overflow

I am doing a tutorial and I keep getting this error:Unhandled exception. System.InvalidOperationExcepti

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();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745257149a4619024.html

相关推荐

  • c# - Unhandled exception. System.InvalidOperationException - Stack Overflow

    I am doing a tutorial and I keep getting this error:Unhandled exception. System.InvalidOperationExcepti

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信