-1

Project type: asp.net core 2.2 (console application) web api I'm using Dependency Injection of microsoft.Extensions.Options IOptions to get configuration appsettings.json into the controller. It is working fine on my local development machine. However - after deploying it - the settings are not injected to the controller. I've added a logger to check the environment which the code was deployed to. When investigating the config variable in the controller constructor, I can see that it has a value, but the properties which should be read from the appsettings is null (see in the code) In the published folder I can see the appsettings.json file with all the relevant settings. What am I missing here ?

Here is my code:

public class MyController : controllerBase
{
    private ILogger<MyController> _logger;
    private readonly IOptions<Myconfig> config;
    
    public MyController(IOptions<Myconfig> config, ILogger<MyController> logger)
    {
        _logger = logger;
        if (config == null)
        {
             logger.LogError("config is not being injected into the controller");
        }
        else if (config.Value != null) //this is the selected option but config.Value.ApiUri is null although it has value in the appsettings 
        {
            logger.LogInformation($"config.Value.ApiUri:{config.Value.ApiUri}"); 
        }
        this.config = config; 
        ....
    }
}

In the Startup.cs, Configuring to get the relevant appsetting entry:

public void ConfigureService(IServiceCollection services)   
{
    services.AddCors();
    ...
    services.AddOptions();
    services.AddHostedService<RepositoryManagerInitializer>();
    services.Configure<Myconfig>(Configuration.GetSection("<relevant section key in the app settings>"));
}    
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Guy E
  • 1,775
  • 2
  • 27
  • 55
  • *It seems like the DI config is not injected* - it seems like a wrong assumption here. Do you have the `appsettings.json` in your app folder after deployed? – King King May 09 '21 at 09:51
  • Yes, I can see the appsettings.json. More than this - other modules in the app can read appsettings variables, but these modules use a different way of reading them, not a DI – Guy E May 09 '21 at 12:39
  • Can you elaborate on what "the settings are not injected to the controller" exactly means? Do you get an exception? If so, what is the exception? Please post it with all its details. Otherwise, what is the behavior you are seeing, and what is it you expect? Also try to convert your question into a [MRE](https://stackoverflow.com/help/minimal-reproducible-example), because as it currently stands, your problem is not reproducible and is a good candidate for getting closed. – Steven May 09 '21 at 16:41
  • @steven- Thanks for the comment - I've edited the code and description and added additional data – Guy E May 10 '21 at 07:24
  • Please do not edit solution announcements into the question. Accept (i.e. click the "tick" next to it) one of the existing answer, if there are any. You can also create your own answer, and even accept it, if your solution is not yet covered by an existing answer. – Yunnosch May 10 '21 at 10:42

1 Answers1

0

The problem was that when reading the section in the configuration file, not like in the development env., need to include the all "path", meaning - if I would like to read the "system" node, and it is inside "AppSettings" node, then I would need to write Configuration.GetSection("AppSettings:system") and not just Configuration.GetSection("system") as it was (worked in development)

Guy E
  • 1,775
  • 2
  • 27
  • 55