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)
json file, and Remote displays a current setting value in the function app in Azure. Choose Add setting to create a new app setting. Use the Insert value from Local link to copy a setting value to the Remote field. Pending changes are written to the local settings file and the function app when you select OK.
The local. settings. json file is where you can define the values for your project in your developer environment. This file must not be added to your source control because it's specific of your own machine.
json file contains runtime-specific configurations and is in the root folder of the function app.
The host. json metadata file contains configuration options that affect all functions in a function app instance. This article lists the settings that are available starting with version 2. x of the Azure Functions runtime.
You CAN indeed read settings outside the Values
array as follows:
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:
[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();
}
}
}
{
"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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With