Reputation: 5375
I have an Azure Function application. The secrets are stored in local.settings.json and also in secrets.json:
{
"Values": {
"BASIC_AUTH_USERNAME": "<USERNAME>",
"BASIC_AUTH_PASSWORD": "<PASSWORD>"
}
}
The Azure function code that consumes the secrets:
string username = Environment.GetEnvironmentVariable("BASIC_AUTH_USERNAME", EnvironmentVariableTarget.Process);
string password = Environment.GetEnvironmentVariable("BASIC_AUTH_PASSWORD", EnvironmentVariableTarget.Process);
Here https://www.koskila.net/how-to-configure-azure-functions-startup/ I found how to add startup.cs file with the Startup class and Configure function:
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Hosting;
using Microsoft.Extensions.Configuration;
[assembly: WebJobsStartup(typeof(Startup))]
namespace LATICRETE.IdentityFunctions
{
public class Startup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
var config = new ConfigurationBuilder()
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
}
}
}
But the GetEnvironmentVariable() method returns only null, and the Startup.Configure() method is never executed. What am I missing?
Upvotes: 1
Views: 682
Reputation: 26
try to update your code as per below snippet;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Hosting;
using Microsoft.Azure.WebJobs.Host.Bindings;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using System.Linq;
[assembly: WebJobsStartup(typeof(LATICRETE.IdentityFunctions.Startup))]
namespace LATICRETE.IdentityFunctions
{
public class Startup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
var executionContextOptions = builder.Services.BuildServiceProvider().GetService<IOptions<ExecutionContextOptions>>().Value;
var appDirectory = executionContextOptions.AppDirectory;
var config = new ConfigurationBuilder()
.SetBasePath(appDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange:true).AddEnvironmentVariables()
.Build();
builder.Services.Replace(ServiceDescriptor.Singleton(typeof(IConfiguration),
config));
}
}
}
And this code will be executed only the very first time when we run the project or when we deploy the code. You can then inject the IConfiguration in which ever class you want and the config settings can be accessed from it's instance.
using Microsoft.Extensions.Configuration
public class TestClass{
private readonly IConfiguration _config;
public TestClass(IConfiguration config)
{
_config = config;
}
public void DoSomeThing()
{
var localSettingA = _config["localSettingA"];
}
}
Need to import Microsoft.Extensions.Configuration to use IConfiguration. Foe more information, have a look at this article - https://medium.com/@padala.suresh.ece/loading-keyvault-secrets-and-appsettings-in-to-application-configuration-in-azure-function-apps-c1798a8fd93f
Upvotes: 1