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>"));
}