I have a service that I need to inject and use inside the project pipeline
//Register Service
builder.Services.AddScoped<IDbInitializer, DbInitializer>();
//Build App and Inject Service
var app = builder.Build();
var dbInitializer = app.Services.GetService<IDbInitializer>();
//Use
dbInitializer.Initialize();
I will update the following problem when injecting this service (or any other service)
>Solution :
In order to require a scoped service, you need to initiate a scope inside which it will live: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-7.0#resolve-a-service-at-app-start-up
//Register Service
builder.Services.AddScoped<IDbInitializer, DbInitializer>();
//Build App and Inject Service
var app = builder.Build();
using (IServiceScope scope = app.Services.CreateScope())
{
IDbInitializer dbInitializer = scope.ServiceProvider.GetRequiredService<IDbInitializer>();
//Use
dbInitializer.Initialize();
}