Aram Yako
Aram Yako

Reputation: 61

Azure function read appsetting.json

Azure function wont read configuration in appsetting.json, it rather reads from local.settings.json

Startup

    public class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {


    }

    public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
    {
        var context = builder.GetContext();

        builder.ConfigurationBuilder
                .AddJsonFile(Path.Combine(context.ApplicationRootPath, "appsettings.json"), optional: true, reloadOnChange: false)
                .AddJsonFile(Path.Combine(context.ApplicationRootPath, $"appsettings.{context.EnvironmentName}.json"), optional: true, reloadOnChange: false)
                .AddEnvironmentVariables();
    }


}

Trigger

        [FunctionName("func")]
    public async Task Run([ServiceBusTrigger("topicName", "subName", Connection = "ConnectionString")] EmployeeMessageModel employeeMessage)
    {


    }

Error: Service Bus account connection string 'ConnectionString' does not exist. Make sure that it is a defined App Setting.

Appsettings.json

{"ConnectionString": "ConnectionString"}

If I add it to local.settings.json it will find it. Any idea what I'm missing?

Upvotes: 0

Views: 1214

Answers (1)

RithwikBojja
RithwikBojja

Reputation: 11411

In portal, if you want to add connection string, you need to add in Application settings of Configuration tab as below:

enter image description here

enter image description here

Something like this you need to do when you publish to portal.

If you want to connect a service bus in local you need add service bus connection string in Local.settings.json as below:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
      "Connection": "Connection string here"
  }
}

enter image description here

In application settings only environment variables(keys) is supported not the connection string, connection string is only supported in local settings.json So you need to use Local settings for that.

Upvotes: 2

Related Questions