Appsettings not mapping to Options class

I could probably have worded the question better, but please bear with me. Also, this is not my code, and probably has not been set up properly, but other team members are all really busy. I see this in the Program class:
public static IWebHost CreateWebHostBuilder(string[] args)
{
var config = new ConfigurationBuilder().AddCommandLine(args).Build();
return WebHost.CreateDefaultBuilder(args)
...
.UseConfiguration(config)
.Build()
.InitializeIdentityDb<ApplicationUser, ApplicationRole>();
}
public static IWebHost CreateWebHostBuilder(string[] args)
{
var config = new ConfigurationBuilder().AddCommandLine(args).Build();
return WebHost.CreateDefaultBuilder(args)
...
.UseConfiguration(config)
.Build()
.InitializeIdentityDb<ApplicationUser, ApplicationRole>();
}
and then ` is defined as such: ```cs public static IWebHost InitializeIdentityDb<TUser, TRole>(this IWebHost host) { using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; var options = services.GetRequiredService<IOptions<CassandraOptions>>(); ... var initializer = new DbInitializer(session); initializer.Initialize<TUser, TRole>(options.Value); return host; } } ``` Then CassandraOptions is declared as: ```cs public class CassandraOptions { public List<string> ContactPoints { get; set; } public CassandraCredentials Credentials { get; set; } public string KeyspaceName { get; set; } public Dictionary<string, string> Replication { get; set; } = null; public bool DurableWrites { get; set; } = true; public CassandraQueryOptions Query { get; set; } } ``` Now at my breakpoint on initializer.Initialize<TUser, TRole>(options.Value), I see that options is not populated. Colleagues reckon it's supposed to be populated from command line arguments, but how do I define nested values like CassandraOptions` with command line params?
8 Replies
Unknown User
Unknown User7h ago
Message Not Public
Sign In & Join Server To View
Thalnos
Thalnos7h ago
unrelated to the problem but also odd that CreateWebHostBuilder returns IWebHost rather than IWebHostBuilder. Either change the method's name or don't build inside the method
Unknown User
Unknown User7h ago
Message Not Public
Sign In & Join Server To View
Yawnder
Yawnder6h ago
Where is CassandraOptions registered? I'd expect to see something like builder.Services.Configure<CassandraOptions>(builder.Configuration.GetSection("Cassandra"));
Unknown User
Unknown User6h ago
Message Not Public
Sign In & Join Server To View
Yawnder
Yawnder6h ago
You don't need the bind afaik.
Unknown User
Unknown User6h ago
Message Not Public
Sign In & Join Server To View
VoidPointer
VoidPointerOP4h ago
Sorry guys, the use of options in this project was royally screwed up be whoever tried it, so they reverted to manually reading env vars for cassandra, and mine weren't getting read. I'll close this now as a bad joke.

Did you find this page helpful?