C
C#17mo ago
palapapa

❔ What's the difference between `IConfiguration.Get` and `IConfiguration.Bind`?

The docs say Get "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." And Bind "Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively." The description of IConfigurationSection.Value is only "Gets or sets the section value." What does IConfigurationSection.Value mean and what's the difference between IConfiguration.Get and IConfiguration.Bind?
26 Replies
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX17mo ago
appsettings.json:
{
"My": {
"Foo": {
"Kix": 5
}
}
}
{
"My": {
"Foo": {
"Kix": 5
}
}
}
src/Foo/FooOptions.cs:
public class FooOptions
{
public const string SectionName = "My:Foo";

public string Bar {get;set;} = "default value for bar";
public int Kix {get;set;} = -1;
public DateTime? Pouet {get;set;} = default;
}
public class FooOptions
{
public const string SectionName = "My:Foo";

public string Bar {get;set;} = "default value for bar";
public int Kix {get;set;} = -1;
public DateTime? Pouet {get;set;} = default;
}
src/Foo/FooServiceCollectionExtensions.cs:
namespace Microsoft.Extensions.DependencyInjection; // <==== recommanded for service.Add so that you don't clutter Startup file

public class FooServiceCollectionExtensions
{
public static IServiceCollection AddFoo(this IServiceCollection services) =>
services
.AddOptions<FooOptions>()
.BindConfiguration(FooOptions.SectionName)
.Validate(options => options.Kix >= 0, $"The configuration key '{FooOptions.SectionName}:{nameof(Kix)}' cannot be negative")
;

public static IServiceCollection AddFoo(this IServiceCollection services, Action<FooOptions> configure) =>
services
.AddFoo()
.Configure(configure);
namespace Microsoft.Extensions.DependencyInjection; // <==== recommanded for service.Add so that you don't clutter Startup file

public class FooServiceCollectionExtensions
{
public static IServiceCollection AddFoo(this IServiceCollection services) =>
services
.AddOptions<FooOptions>()
.BindConfiguration(FooOptions.SectionName)
.Validate(options => options.Kix >= 0, $"The configuration key '{FooOptions.SectionName}:{nameof(Kix)}' cannot be negative")
;

public static IServiceCollection AddFoo(this IServiceCollection services, Action<FooOptions> configure) =>
services
.AddFoo()
.Configure(configure);
Program.cs / Startup.cs:
services.AddFoo();
// or
services.AddFoo(fooOptions => fooOptions.Kix = 12);
services.AddFoo();
// or
services.AddFoo(fooOptions => fooOptions.Kix = 12);
Bar.cs:
public class Bar
{
private readonly FooOptions _fooOptions;

// .Value in ctor is fine only if it's always ever a non-changing value (no reload and/or no scoped resolution)
public Bar(IOptions<FooOptions> fooOptions)
=> _fooOptions = fooOptions.Value;
}
public class Bar
{
private readonly FooOptions _fooOptions;

// .Value in ctor is fine only if it's always ever a non-changing value (no reload and/or no scoped resolution)
public Bar(IOptions<FooOptions> fooOptions)
=> _fooOptions = fooOptions.Value;
}
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
palapapa
palapapaOP17mo ago
I don't understand what you meant by "a single value." I mean, if I have an options class, then does an instance of that class count as "a single value"? And the documentation never mentions this "a single value" thing, it only says that the only difference between Get and Bind is that Get "Attempts to bind the configuration instance to a new instance of type T. If this configuration section has a value, that will be used." This example didn't really answer my questions though, but raised more. What's BindConfiguration and what's the difference between it and IOptionsBuilder.Bind? I also still don't understand what IConfigurationSection.Value means. If it's a section, then isn't it supposed to contain multiple values? Then why would it have a value itself?
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
palapapa
palapapaOP17mo ago
Sorry, but I looked all over and the docs only contained the minimal amount of information and that's why I asked here.
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
palapapa
palapapaOP17mo ago
For most of these methods the docs only has a single sentence describing them
palapapa
palapapaOP17mo ago
For example the docs for Bind
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
palapapa
palapapaOP17mo ago
ASP.NET documentation
Learn to use ASP.NET Core to create web apps and services that are fast, secure, cross-platform, and cloud-based. Browse tutorials, sample code, fundamentals, API reference and more.
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
palapapa
palapapaOP17mo ago
Thank you. I'll read them and come back if I have more questions.
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
palapapa
palapapaOP17mo ago
Because you would have to inject the whole IConfiguration?
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
palapapa
palapapaOP17mo ago
Accidentally used a super reaction lmao
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
palapapa
palapapaOP17mo ago
What's "bonne"? Did you mean bind?
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
palapapa
palapapaOP17mo ago
Tbh I didn't read them because MS "article documentations" have always made me think they are bad
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
palapapa
palapapaOP17mo ago
So is this an API doc or a doc?
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server