1

I've written web job as .NET Core Console Application (exe), that has appsettings.json.

How do I configure the WebJob in Azure? Basically I want to share some settings like connection string with the web app, that is configured trough App Service's Application Settings.

Liero
  • 25,216
  • 29
  • 151
  • 297

3 Answers3

2

The way to get these settings from our ASP.NET Core is accessing to the injected environment variables.

Hence we have to load these environment variables into our Configuration in the Startup.cs file:

public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        Configuration = builder.Build();
    }

An example of appsettings.json file would be:

enter image description here

If you want to get the connection string named "Redis" defined in the appsettings.json file we could get it through our Configuration:

Configuration["ConnectionStrings:Redis"].

You could set this Configuration in Appsettings in webapp on azure portal:

enter image description here

Also we can use Configuration.GetConnectionString("Redis") to get a development connection string from our appsettings.json file and override it setting a different one in the Connection String panel of our Web App when the application is deployed and running in Azure.

For more detail, you could refer to this article.

Joey Cai
  • 18,968
  • 1
  • 20
  • 30
  • I don't understand how command line is related to environment variables? Instead of `AddCommandLine` I would use `AddEnvironmentVariables` – Liero Mar 14 '18 at 06:59
  • @Liero, sorry for some mistakes, I have updated my reply, you could have a look. – Joey Cai Mar 14 '18 at 07:12
  • Just out of curiosity, why did you set connection string in Application settings -> "ConnectionStrings:Redis", instead of ConnectionsStrings -> "Redis" in azure portal? – Liero Mar 14 '18 at 09:53
  • I am not clear it which is designed by developers. To override nested keys in the App Settings section we can define a variable using the full path Authentication:AzureAd:ClientId as name or using double underscore Authentication__AzureAd__ClientId – Joey Cai Mar 15 '18 at 01:42
  • 2
    It's a WebJob console app not an ASP.NET web app. There is no StartUp(IHostingEnvironment env) method. How does one get hold of the env? – Peter Morris Oct 15 '18 at 09:23
  • 2
    @PeterMorris: You don't need IHostingEnvironment. Just call `new ConfigurationBuilder().AddEnvironmentVariables()` – Liero Oct 15 '18 at 12:59
1

I prefer doing this through setting environment varibles in launchSettings.json in my local project and setting the same ones in Azure App Service settings.

Advantage is that your application always uses environment variables and, more important one, no keys end up in your source control since you don't have to deploy launchSettings.json.

dee zg
  • 13,793
  • 10
  • 42
  • 82
0

The CloudConfigurationManager class is perfect for this situation as it will read configuration settings from all environments (the web jobs config and the base app config).

Install-Package Microsoft.WindowsAzure.ConfigurationManager

Then use it like this

var val = CloudConfigurationManager.GetSetting("your_appsetting_key");

The only downside is that it is only possible to read from the appSettings sections and not the connectionstring section with the CloudConfigurationManager. If you want to share connectionsting between the web job and the base web app, then I would define the connectionstring in the appsetting section of the web app with an unique key.

Marcus Höglund
  • 16,172
  • 11
  • 47
  • 69
  • What is the advantage over environment variables? – Liero Mar 14 '18 at 09:51
  • @Liero you will have one place to store your settings for all environments. The appsettings are also exposed in the portal which make it easy to maintain if you need to change something. – Marcus Höglund Mar 14 '18 at 10:01
  • Well, as far as I understood from the other answer, azure portal settings are reflected in environment variables – Liero Mar 14 '18 at 10:12
  • Oh, CloudConfigurationManager is probably meant for web.config in classic asp.net – Liero Mar 14 '18 at 10:16
  • 1
    @Liero CloudConfigurationManager will work with both classic asp.net .config files and appsettings.json, thats the good part of it. I use it in .net core applications where web jobs have there own appsettings.json and the main app have its own appsettings.json. then the CloudConfigurationManager helps me read config values from main app which is shared accross the environments – Marcus Höglund Mar 14 '18 at 10:22
  • In Azure App Service, the "main app" and the WebJob share the same environment variables. Moreover, Application Settings specified in the azure portal are reflected into Environment variables for all deployment slots (if that's what you mean by environment), so you only need to specify AppSettings once in appsettings.json and eventually override in Azure Application Settings once for all apps and webjobs withing App Service, all deployments slots. Maybe I'm missing something... – Liero Mar 14 '18 at 13:30
  • That package is now deprecated – Anders Rune Jensen Apr 04 '23 at 12:59