C
C#5mo ago
TOKYODRIFT!

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
Thinker5mo 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.
TOKYODRIFT!
TOKYODRIFT!5mo ago
hmmm okay, I'll ignore, thanks
Joschi
Joschi5mo 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 Rufo5mo 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 User5mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX5mo 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 User5mo ago
Message Not Public
Sign In & Join Server To View
Joschi
Joschi5mo ago
Didn't .NET 8 also add a Source Gen for Options Validations?
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
TOKYODRIFT!
TOKYODRIFT!5mo ago
is it possible to test that "My" section is exist?
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server
More Posts
Type Inference strategies?I'm generating Class Definition from XML. And currently I just save the Value strings. But I want stGenerating input fields for each element in an array in Blazor WASMI'm trying to generate an input field for each element in an array, and have the input field bind thBinarySerialization - need some design helpHi, this is my first foray into lower level programming and I could use some help. I am writing a D✅ why ArgumentException.ThrowIfNullOrEmpty(); is only for string?I have following: ```cs public class A { A(AnotherClass AC) { // how to cheAspNet.Core OpenTelemetry AzureMonitorLogExporter doesn't include ILogger name in AppInsight traces.ILogger log messages get written to app insights but there is some info missing compared to writing ✅ Best practices for WPF context aware documentationAll projects in the company I work at are made in WinForms .NET 4.8 and provide CHM help file documeDoes CultureInfo ever matter here?I have some code that is generating some warnings: ``` CA1305: The behavior of 'long.ToString()' coRun Query in asp.net mvcIn ASP.net mvc I have a order by descending query in a controller how would i excute the the query ✅ Desperate help needed with MSAL tokenIs there a way to add more user info into the msal token? In entra I for example have job title, pho✅ Is it possible to convert a generic interface to a generic interface?I'm trying to prevent the creation of multiple giant switches in a controller by passing the instant