Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind IConfiguration to C# Record Type

I would like to bind configuration to record type.

This is definition of configuration type (it is without parameterless constructor):

public record AppConfiguration(string ConnectionString);

This is sample Main method:

public static void Main(string[] args)
{
    var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
    AppConfiguration appConfig = new(); // error
    configuration.GetSection("app").Bind(appConfig);
}

If I convert definition to this:

public record AppConfiguration
{
    public string ConnectionString {get; init;}
}

it works as expected, but I would rather use "single line" definition of the record. Are records right way for this use case?

like image 770
Rok Avatar asked Jan 23 '21 12:01

Rok


People also ask

What is IConfiguration C#?

Get<T>(IConfiguration) Attempts to bind the configuration instance to a new instance of type T. If this configuration section has a value, that will be used. Otherwise binding by matching property names against configuration keys recursively.

What does configuration bind do?

Bind(IConfiguration, String, Object) Attempts to bind the given object instance to the configuration section specified by the key by matching property names against configuration keys recursively.

How do I configure services in NET Core?

Application configuration in ASP.NET Core is performed using one or more configuration providers. Configuration providers read configuration data from key-value pairs using a variety of configuration sources: Settings files, such as appsettings. json.

How do I bind a configuration to a specific type?

Attempts to bind the configuration instance to a new instance of type T. If this configuration section has a value, that will be used. Otherwise binding by matching property names against configuration keys recursively. Extracts the value with the specified key and converts it to the specified type.

What is iconfiguration bind?

Bind(IConfiguration, String, Object) Bind(IConfiguration, String, Object) Bind(IConfiguration, String, Object) Attempts to bind the given object instance to the configuration section specified by the key by matching property names against configuration keys recursively.

What is a binder in Microsoft Configuration Manager?

Configuration Binder. Bind Method Microsoft. Extensions. Configuration Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively. Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.

How to set the values inside the configuration file?

There are different ways to set the values inside the configuration file and read their values, which are based on the defined keys. We define those values inside the configuration section, which might be needed to make it more secure. It can be some secret keys or the value, which should be received frequently.


Video Answer


1 Answers

The problem with your first approach is that with the single-line declaration you have automatically defined the primary constructor, which

causes the implicitly declared default class constructor, if present, to be suppressed.

In the second case the primary constructor is the parameterless constructor, so it works as expected. Just to clear up any doubts, the init accessor is backwards compatible, so even if ConnectionString is not directly initialized, it takes the value null. The Bind method will correctly fill it using reflection, I guess.

like image 115
Marco Luzzara Avatar answered Sep 22 '22 12:09

Marco Luzzara