C
C#11mo ago
SWEETPONY

How to check options for null?

I have this:
var options = Configuration.GetSection(nameof(Patterns));
services.Configure<Patterns>(options);
var options = Configuration.GetSection(nameof(Patterns));
services.Configure<Patterns>(options);
in controller:
public ZeebeController(
KafkaSettings kafkaSettings,
IZeebeClient zeebeClient,
IOptions<Patterns> options)
{
}
public ZeebeController(
KafkaSettings kafkaSettings,
IZeebeClient zeebeClient,
IOptions<Patterns> options)
{
}
all I want is check options from startup for null but I can't just write if(options == null)
No description
11 Replies
Thinker
Thinker11mo ago
According to the docs for GetSection, it returns "The specified ConfigurationSection object, or null if the section does not exist.", however the return type is object and not object?. You can probably just ignore the warning because the return type is annotated incorrectly.
SWEETPONY
SWEETPONYOP11mo ago
hmmm okay, I'll ignore, thanks
Joschi
Joschi11mo ago
You could also bind the configuration to a class at startup and inject that class. That also allows you to do validation.
Sir Rufo
Sir Rufo11mo ago
Options pattern in ASP.NET Core
Discover how to use the options pattern to represent groups of related settings in ASP.NET Core apps.
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX11mo 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 User11mo ago
Message Not Public
Sign In & Join Server To View
Joschi
Joschi11mo ago
Didn't .NET 8 also add a Source Gen for Options Validations?
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
SWEETPONY
SWEETPONYOP11mo ago
is it possible to test that "My" section is exist?
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server