C
C#12mo 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 User12mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX12mo 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 User12mo ago
Message Not Public
Sign In & Join Server To View
palapapa
palapapa12mo 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 User12mo ago
Message Not Public
Sign In & Join Server To View
palapapa
palapapa12mo 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 User12mo ago
Message Not Public
Sign In & Join Server To View
palapapa
palapapa12mo ago
For most of these methods the docs only has a single sentence describing them
palapapa
palapapa12mo ago
For example the docs for Bind
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
palapapa
palapapa12mo 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 User12mo ago
Message Not Public
Sign In & Join Server To View
palapapa
palapapa12mo ago
Thank you. I'll read them and come back if I have more questions.
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
palapapa
palapapa12mo ago
Because you would have to inject the whole IConfiguration?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
palapapa
palapapa12mo ago
Accidentally used a super reaction lmao
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
palapapa
palapapa12mo ago
What's "bonne"? Did you mean bind?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
palapapa
palapapa12mo ago
Tbh I didn't read them because MS "article documentations" have always made me think they are bad
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
palapapa
palapapa12mo ago
So is this an API doc or a doc?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Accord
Accord12mo 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
More Posts
❔ Best place to store a flag globally on ASP.NET CoreHello everyone. What I'm trying to achieve is given a list of users that each have access to 1...N ❔ C# app on LinuxHello , I'm trying to create a C# app on Linux but I think it's just not possible. I tried multiple ❔ Pattern matching on multiple cases with the same-typed membersHello, I am currently stuck with code like this: ```cs public void Foo(Bar bar) { if (bar is Bar1❔ Hosting CoreWCF service in AspNet Core (Kestrel). How do I validate the service is up?The output contains notifications that 1) the Kestrel server is listening on the port I specified an✅ What does "Windows Desktop" as the version in ASP.NET's documentation mean?https://learn.microsoft.com/en-us/dotnet/api/system.configuration?view=windowsdesktop-7.0❔ Number guessing game.I'm trying to make a program that generates a random number between 1-100, let's the user guess till❔ how would I got about making a discord bot.I am not new to coding but completely new to discord bot creation so need some pointers✅ Cannot figure out what I am doing wrong.I am currently taking classes for C#, and im kind of throwing things around with casts and things, i❔ Cannot find "New Project" for Hello WorldHey, I'm terribly sorry if this isn't in the right place. After all this time I'm still not used to How do I write text, then a int, then more text in the same line?Trying to have it print "You have sum years left till pension." Console.WriteLine("What's your nam