There are many examples out there that show how to deserialize your app settings within the Startup.cs
class. I get that. But that's not what I'm asking. I'm working within a unit test constructor.
This question is close: Read appsettings json values in .NET Core Test Project But in reading it carefully, I'm not seeing a slam dunk answer.
appsettings.json:
{
"AppSettings": {
"myKey1": "myValue1",
"myKey2": "myValue2",
}
}
And:
public class AppSettings
{
public string myKey1 { get; set; }
public string myKey2 { get; set; }
}
I am able to get single values just fine:
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
var myValue1 = config.GetSection("AppSettings:myKey1 ").Value;
But what I'm not figuring out is how to get create an instance of AppSettings
. I've tried:
var appSettings = config.GetValue<AppSettings>("AppSettings");
But that ends up null. And I've played around with trying to create my own instance of IServiceCollection
so I could do something like you might in Startup.cs
, but I'm not making any progress.