0

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?

Rogerio Schmitt
  • 1,055
  • 1
  • 15
  • 35
  • 1
    Your code looks a bit off to me. Building the configuration is something that's done in startup. Why are you trying to build it in some method during runtime? – Dennis VW May 22 '20 at 23:59

1 Answers1

1

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;
Marko Radivojević
  • 458
  • 2
  • 7
  • 11
  • No game, when I do this `GetCurrentDirectory()` returns my tests directory and not the one where my `json` is located. – Rogerio Schmitt May 23 '20 at 00:49
  • 1
    According to this: https://stackoverflow.com/questions/39791634/read-appsettings-json-values-in-net-core-test-project. There is a specific issue about using `GetCurrentDirectory()` on a test project, it is recommended that I keep a copy of my `json` file in the test project. I see a big issue here as I would have to assure equality in two different `json` files. – Rogerio Schmitt May 23 '20 at 01:10
  • 1
    @RogerioSchmitt This might help you: https://stackoverflow.com/questions/674857/should-i-use-appdomain-currentdomain-basedirectory-or-system-environment-current. *I see a big issue here as I would have to assure equality in two different json files.* I agree with you, however, it is a valid everyday solution. – Marko Radivojević May 23 '20 at 01:27
  • 1
    @RogerioSchmitt I've added another way of accessing `Value`s in `UserNotificatorConfig` section, if previous solution doesn't help. – Marko Radivojević May 23 '20 at 01:41