Reputation:
Is there a way to change local.settings.json from code?
I want to perform integration test on azure function which calls some external services, urls of those services are saved in local.settings.json.
I need to create some dummy services which will simulate the real ones.
So my question is how can I (only for testing time) changed settings so azure function will point to those dummy services instead the real ones?
Upvotes: 0
Views: 1653
Reputation: 11621
I am not 100% sure what you want to achieve but I try to give you a solution.
As you are talking about local.settings.json I assume you are talking about having your Azure Function running locally. Multiple local.settings files do not work (e.g. using something like local.settings.Development.json similar to appsettings.Development.json). What you can do is using Dependency Injection (see also Use dependency injection in .NET Azure Functions):
1) Create an entity class with the relevant environment specific properties (like dev or int). E.g.
namespace TestAF.Entities
{
public class EnvConfig
{
public string TestKey { get; set; }
public string TestKey1 { get; set; }
}
}
2) Adjust local.settings.json to keep values per environments. E.g.
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"Environment": "dev",
"dev:TestKey": "devvalue",
"dev:TestKey1": "devvalue1",
"int:TestKey": "intvalue",
"int:TestKey1": "intvalue1"
}
}
Nested properties within the section "Values" seem not to work, therefore the semicolons that act as logical separator.
3) Add the class Startup.cs like described here: Register services
4) In this class in the Configure method initialize the IOptions object. E.g.
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using TestAF.Entities;
[assembly: FunctionsStartup(typeof(TestAF.Startup))]
namespace TestAF
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddOptions<EnvConfig>()
.Configure<IConfiguration>((settings, configuration) =>
{
if (configuration.GetValue(typeof(string), "Environment").ToString() == "dev")
{
configuration.GetSection("dev").Bind(settings);
}
if (configuration.GetValue(typeof(string), "Environment").ToString() == "int")
{
configuration.GetSection("int").Bind(settings);
}
});
}
}
}
6) Adjust main of the Azure Function to use the config values. E.g.
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using TestAF.Entities;
namespace TestAF
{
public class Function1
{
private static EnvConfig EnvConfig;
public Function1(IOptions<EnvConfig> envConfig)
{
EnvConfig = envConfig.Value;
}
[FunctionName("Function1")]
public void Run([TimerTrigger("* /5 * * * *")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
log.LogInformation(EnvConfig.TestKey);
log.LogInformation(EnvConfig.TestKey1);
}
}
}
By setting the Key Values.Environment you can now control which keys to use (you could of course also set this value externally e.g. via an environment variable).
Upvotes: 1