C
C#12mo ago
Dropps

options pattern not working

migrated not so long ago .net 8 did they change something with the options pattern? it seems to not work anymore for some reason that i cant explain myself see code below:
builder.Services.AddOptions<JwtOptions>(JwtOptions.SectionName)
.Bind(config.GetSection(JwtOptions.SectionName));

builder.Services.AddOptions<DatabaseConnectionOptions>(DatabaseConnectionOptions.SectionName)
.Bind(config.GetSection(DatabaseConnectionOptions.SectionName));

builder.Services.AddOptions<JwtOptions>(JwtOptions.SectionName)
.Bind(config.GetSection(JwtOptions.SectionName));

builder.Services.AddOptions<DatabaseConnectionOptions>(DatabaseConnectionOptions.SectionName)
.Bind(config.GetSection(DatabaseConnectionOptions.SectionName));

inside here it should retrive the stuff from the appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnection": "Server=db;Database=postgres;User ID=admin;Password=admin;"
},
"Jwt": {
"Key": "HIDDEN_API_KEY"
},
"AllowedHosts": "*"
}
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnection": "Server=db;Database=postgres;User ID=admin;Password=admin;"
},
"Jwt": {
"Key": "HIDDEN_API_KEY"
},
"AllowedHosts": "*"
}
but whenever i hit my AddInfrastructure point i get an error that it would be null see her:
...
// Database
var options = serviceCollection.BuildServiceProvider().GetRequiredService<IOptions<DatabaseConnectionOptions>>();

if (string.IsNullOrWhiteSpace(options.Value.DefaultConnection))
{
throw new InvalidOperationException($"{DatabaseConnectionOptions.SectionName}:{nameof(DatabaseConnectionOptions.DefaultConnection)} cannot be null or whitespace");
}

serviceCollection.AddDbContext<AppDbContext>(dbOptions =>
{
dbOptions.UseNpgsql(options.Value.DefaultConnection);
});

serviceCollection.AddScoped<IUnitOfWork>(serviceProvider => serviceProvider.GetRequiredService<AppDbContext>());

return serviceCollection;
...
// Database
var options = serviceCollection.BuildServiceProvider().GetRequiredService<IOptions<DatabaseConnectionOptions>>();

if (string.IsNullOrWhiteSpace(options.Value.DefaultConnection))
{
throw new InvalidOperationException($"{DatabaseConnectionOptions.SectionName}:{nameof(DatabaseConnectionOptions.DefaultConnection)} cannot be null or whitespace");
}

serviceCollection.AddDbContext<AppDbContext>(dbOptions =>
{
dbOptions.UseNpgsql(options.Value.DefaultConnection);
});

serviceCollection.AddScoped<IUnitOfWork>(serviceProvider => serviceProvider.GetRequiredService<AppDbContext>());

return serviceCollection;
options classes have similar schema each:
public class DatabaseConnectionOptions
{
public const string SectionName = "ConnectionStrings";

public string? DefaultConnection { get; set; }
}
public class DatabaseConnectionOptions
{
public const string SectionName = "ConnectionStrings";

public string? DefaultConnection { get; set; }
}
someone knows how to fix this?
12 Replies
RD Dev
RD Dev12mo ago
you might be missing the configuration setup for the sections in your appsettings.json
Dropps
DroppsOP12mo ago
what you mean by that?
RD Dev
RD Dev12mo ago
I mean, ensure that the structure of your appsettings.json file matches the section names used in your options classes (JwtOptions and DatabaseConnectionOptions).
Dropps
DroppsOP12mo ago
it does match already as you see? or am i missing something
RD Dev
RD Dev12mo ago
Make sure you are loading the configuration properly using the appropriate code at the beginning of your application.
Dropps
DroppsOP12mo ago
its what iam doing above it
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
ConfigurationManager config = builder.Configuration;

builder.Services.AddOptions<JwtOptions>()
.Bind(config.GetSection(JwtOptions.SectionName));

builder.Services.AddOptions<DatabaseConnectionOptions>()
.Bind(config.GetSection(DatabaseConnectionOptions.SectionName));
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
ConfigurationManager config = builder.Configuration;

builder.Services.AddOptions<JwtOptions>()
.Bind(config.GetSection(JwtOptions.SectionName));

builder.Services.AddOptions<DatabaseConnectionOptions>()
.Bind(config.GetSection(DatabaseConnectionOptions.SectionName));
.net8 web api
RD Dev
RD Dev12mo ago
Your configuration binding for JwtOptions and DatabaseConnectionOptions appears correct in the provided .NET code snippet for a web API I don't have time to help you. Hopefully I gave you enough information or wait for someone else
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Dropps
DroppsOP12mo ago
how does the validation then take place? does it throw an exception or?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Dropps
DroppsOP12mo ago
then we are in a "double" concern issue state cause i register everything using clean architecture and extension methods for each layer but thanks
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server