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?
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.
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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With