Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET 6 (stable) IConfiguration setup in Program.cs

This appears to be a similar problem but none of the answers are fitting for my code...: Read appsettings.json in Main Program.cs

This is extremely similar but does not work for me for some reason: ASP.NET Core 6 how to access Configuration during setup

I'm porting a .NET 6 beta application to stable version of .NET 6.

This means I want to port the code responsible for importing IConfiguration (yes I'm aware it's backwards compatible with the separate Startup.cs and Program.cs files, but I'm willing to use the new syntax). IConfiguration is basically the file appsettings.json in ASP.NET Core.

In old code it looks like this:

 public class Startup
    {
        internal static IConfiguration Configuration { get; private set; }
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

I tried the following:

using Microsoft.Extensions.Configuration;
// IConfiguration init test
var Configuration = builder.Configuration;

It seems that the Microsoft.Extensions.Configuration isn't even used! The syntax I got is from this blog post: https://gavilan.blog/2021/08/19/getting-the-iconfiguration-in-the-program-class-asp-net-core-6/

This problem is primitive, but I don't seem to get it right. Any help is appreciated to solve this problem being that it works in new .NET 6 minimal API.

I found this guide useful but it apparently didn't solve the problem at hand: https://www.mikesdotnetting.com/article/357/razor-pages-startup-in-net-6

In another Model I have this code which is also not valid anymore:

secret = Startup.Configuration["GoogleRecaptchaV3:Secret"];
                path = Startup.Configuration["GoogleRecaptchaV3:ApiUrl"];

I tried replacing Startup with Program but it didn't seem to work...

The errors I'm getting are:

'Program' does not contain a definition for 'Configuration'
like image 478
Munchkin Avatar asked Nov 21 '25 00:11

Munchkin


1 Answers

  1. Switching to minimal hosting model is not required, you can still use the generic hosting (the one with startup).

It seems that the Microsoft.Extensions.Configuration isn't even used!

The using Microsoft.Extensions.Configuration; directive is not needed because new feature global using directive is heavily utilized in .NET 6 templates.

3.

var Configuration = builder.Configuration;

The Program file in the new templates is using another relatively new feature (C# 9) - top-level statements, so the var Configuration is just a variable accessible only inside the generated Main method.

4.

secret = Startup.Configuration["GoogleRecaptchaV3:Secret"];

You can access this value inside the top-level statement file via:

var secret = builder.Configuration["GoogleRecaptchaV3:Secret"];

To make settings accessible outside that file (or Startup in generic hosting) I highly recommend to use DI (as shown in the docs here or here).

In case if you have A LOT of code relying on static configuration being available and you need a quick temporary fix you can add partial Program class at the end of the top-level statement file and do something like:

// ....
WebApplication app = builder.Build();
Configuration = app.Configuration;
// ...

public partial class Program
{
    internal static IConfiguration Configuration  { get; private set; }
}

But I highly recommend against that.

like image 165
Guru Stron Avatar answered Nov 23 '25 13:11

Guru Stron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!