Blaise
Blaise

Reputation: 22212

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?

Upvotes: 20

Views: 32586

Answers (3)

Blaise
Blaise

Reputation: 22212

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

Upvotes: 20

Aishwarya Mathavan
Aishwarya Mathavan

Reputation: 51

You could add the IConfiguration instance to the service collection as a singleton object in ConfigureServices:

public void ConfigureServices(IServiceCollection service)
{ 
    services.AddSingleton<IConfiguration>(Configuration);  
    //...
}

This allows you to inject IConfiguration in any controllers or classes:

public class UserController
{
  public UserController(IConfiguration configuration)   
  //Use IConfiguration instance here
}

Upvotes: 5

Marko
Marko

Reputation: 13243

As far as I can tell, they replaced it with a concrete instance of ConfigurationManager class which is a property of the WebApplicationBuilder.

In your Program.cs class there is a line of code:

 var builder = WebApplication.CreateBuilder(args);

You can then access the configuration from this builder instance:

 builder.Configuration.GetConnectionString("DefaultConnection");

I know this does not answer your "how to inject IConfiguration" question but I'm guessing you just want to be able to access your configuration settings. There is nothing stopping you from setting up the project as it used to be in the old .net 5 code. All you need is Program.cs to be:

public class Program
{
    public static async Task Main(string[] args)
    {
        var host = CreateHostBuilder(args).Build();
        await host.RunAsync();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
                webBuilder.ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                    logging.SetMinimumLevel(LogLevel.Trace);
                });
            });
}

And then just create a class called Startup.cs with the following code:

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

    private IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        // TODO: Service configuration code here...
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        // TODO: App configuration code here...
    }
}

Upvotes: 6

Related Questions