Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject IConfiguration in asp.net core 6

There is no Startup.cs in the web/api application any more.

We used to be able to inject IConfiguration into that Startup class.

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration
    }
    ...
}

Now that those add service and configuration code is moved into Program.cs, what is the correct way to access configuration from there?

like image 420
Blaise Avatar asked Nov 21 '25 04:11

Blaise


1 Answers

The IConfiguration can be accessed in the WebApplicationBuilder.

enter image description here

So no need to inject IConfiguration any more, it is now a property in the builder in Program.cs. enter image description here

var builder = WebApplication.CreateBuilder(args);
var config = builder.Configuration;

builder.Services.AddInfrastructureServices(config);
builder.Services.AddPersistenceServices(config);
like image 117
Blaise Avatar answered Nov 23 '25 20:11

Blaise