Reputation: 1275
I'm trying to get a path url from the following notificatorConfig.json
file:
{
"UserNotificatorConfig": {
"URL": "http://localhost:8001",
"BasePath": "/send-message/android"
}
}
Using a ConfigurationBuilder()
as follows:
public async Task Send(string message)
{
var config = new ConfigurationBuilder()
.AddJsonFile("notificatorConfig.json", true)
.Build();
var url = config.GetSection("UserNotificatorConfig:URL").Value;
var basePath = config.GetSection("UserNotificatorConfig:BasePath").Value;
await _rest.PostAsync<Notification>(url, basePath, message);
}
Both my json
file and the file where my Send()
method is located, are in the same folder.
But every time I try to debug this method on unit tests I get null
values for both parameters url
and basePath
.
What am I missing here?
Upvotes: 0
Views: 2868
Reputation: 448
You need to add SetBasePath(Directory.GetCurrentDirectory())
:
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("notificatorConfig.json", true)
.Build();
And, as @Dennis1679 said, you should build configuration in startup.
Edit:
If this doesn't help, access Value
inside Section
this way:
var userNotificatorConfig = config.GetSection("UserNotificatorConfig");
var url = userNotificatorConfig.GetValue<string>("URL");
var basePath = userNotificatorConfig.GetValue<string>("BasePath");
Instead of this way:
var url = config.GetSection("UserNotificatorConfig:URL").Value;
var basePath = config.GetSection("UserNotificatorConfig:BasePath").Value;
Upvotes: 1