Azure functions local.settings.json represented in appsettings.json for a ServiceBusTrigger
You CAN indeed read settings outside the Values
array as follows:
WeatherApiConfig.cs
public class WeatherApiConfig
{
public string WeatherApiUrl { get; set; }
public string WeatherApiKey { get; set; }
}
New for Azure Functions V2, we have an appropriate way to handle DI as shown below:
Startup.cs
[assembly: FunctionsStartup(typeof(BlazingDemo.Api.Startup))]
namespace BlazingDemo.Api
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
var apiConfig = new WeatherApiConfig();
config.Bind(nameof(WeatherApiConfig), apiConfig);
builder.Services.AddSingleton(apiConfig);
builder.Services.AddHttpClient();
}
}
}
Local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
},
"WeatherApiConfig": {
"WeatherApiUrl": "http://api.openweathermap.org/data/2.5/weather",
"WeatherApiKey": "**removed**"
}
}
Note: The key for me was to add
.SetBasePath(Directory.GetCurrentDirectory())
inStartup.cs
since it couldn't find the file without it.
In production I use the function app's Application Settings
section to configure these two properties as follows:
O'Neil Tomlinson
Updated on July 09, 2022Comments
-
O'Neil Tomlinson almost 2 years
I currently have an azure function using the ServiceBusTrigger binding
[ServiceBusTrigger("%TopicName%", "%SubscripionName%", Connection = "MyConnection")] string catclogueEventMsgs, ILogger log, ExecutionContext context)
which uses this local.settings.json file
"Values": { … "MyConnection": "Endpoint=sb://testxxxxxxxxxxxxxxxxxx "SubscriptionName": "testsubscriptionName" "TopicName": "testtopicName", }
How do I represent this in the appsettings.json file. Will it be like the below?
"Values": { "MyConnection": "Endpoint=sb://testxxxxxxxxxxxxxxxxxx "SubscriptionName": "testsubscriptionName" "TopicName": "testtopicName", }
Instead of using a “Values” object can I use eg “MySubs” object like the below?
"MySubs": { "MyConnection": "Endpoint=sb://testxxxxxxxxxxxxxxxxxx "SubscriptionName": "testsubscriptionName" "TopicName": "testtopicName", }
If its possible to use the above settings, how do I represent this in the ServiceBusTrigger binding? would i change it to this?
[ServiceBusTrigger("%MySubs.TopicName%", "%MySubs.SubscripionName%", Connection = "MySubs.MyConnection")] string catclogueEventMsgs, ILogger log, ExecutionContext context)
-
Adam Tuliper about 4 yearsThis is a neat workaround. I just want to call out this uses then a new instance of the configuration system and your dependency injection is no longer IOptions<T> based. If you want that or want that in addition to this you'll still need to configure that via builder.Services.AddOptions<>().Configure<Iconfiguration>() etc at least as of v3.