1

I have a working Azure WebJob with queue binding like this:

public static class Function1
{
    [FunctionName("Function1")]
    public static void Run([QueueTrigger("%input-queue%")]string myQueueItem, ILogger log)
    {
        log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
    }
}

I'm trying to convert it to an Azure Function. It works fine if I hard-code the queue name like [QueueTrigger("input")] but I want to make it configurable. Debugging locally works fine if I add the configuration to my local.settings.json file:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "input-queue": "debug-input"
  },
}

The problem is, I add "input-queue": "input" to my appsettings.json but the deployed function won't trigger. Am I doing something wrong here?

I have following FunctionStartup:

using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using System.IO;

[assembly: FunctionsStartup(typeof(FunctionApp2.Startup))]

namespace FunctionApp2
{
    public class Startup : FunctionsStartup
    {
        public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
        {
            FunctionsHostBuilderContext context = builder.GetContext();

            builder.ConfigurationBuilder
                .AddJsonFile(Path.Combine(context.ApplicationRootPath, "appsettings.json"), optional: false, reloadOnChange: false)
                .AddJsonFile(Path.Combine(context.ApplicationRootPath, "local.settings.json"), optional: true, reloadOnChange: false)
                .AddEnvironmentVariables()
                .Build();
        }

        public override void Configure(IFunctionsHostBuilder builder)
        {
        }
    }
}

Here are my function app dependencies:

<ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="3.0.4" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.1.8" />
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.1.8" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.8" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.9" />
  </ItemGroup>
Connell.O'Donnell
  • 3,603
  • 11
  • 27
  • 61
  • have you tried without your custom Startup.cs? I.e. without loading anything from KeyVault etc. – silent Mar 01 '21 at 16:25
  • 1) Do you see this App setting in Portal? Or CLI (`az functionapp config appsettings list --name --resource-group `) or anything. 2) Do you see any logs anywhere that indicate that Azure is polling the Q on behalf of your function? Follow the log stream in Portal or using CLI and set level to verbose. – Kashyap Mar 01 '21 at 16:53
  • @silent The custom startup class is necessary for my purposes. Nevertheless, I have tried a deployment without it but it hasn't solved the problem. I have removed the key vault part from the question is it's not necessary for a minimal example. – Connell.O'Donnell Mar 02 '21 at 11:50
  • @Kashyap The setting isn't visible in the portal. It should be coming from the `appsettings.json` file whereas the portal settings will be environment variables. I don't see any polling at all in the logs. – Connell.O'Donnell Mar 02 '21 at 11:54
  • ok, is there a reason why you are not using the Functions AppSettings? That is the recommended way to do it, not reading the config from a file – silent Mar 02 '21 at 12:12
  • @silent Thanks, that has solved it. I removed it from `appsettings.json` and added to the Function's `AppSettings` and now it works. If you want to create an answer with this suggestion, I'll mark it as accepted. – Connell.O'Donnell Mar 02 '21 at 12:20
  • glad to hear it. Will do – silent Mar 02 '21 at 12:33

3 Answers3

3

As discussed in the comments:

The appsettings.json file is not being read in when running in Azure Functions. Reading the local.settings.json when running locally, is part of the debugging experience of the Functions core tools.

Instead, when running in Azure, you should use the App Settings of Functions. This provides additional features such as Key Vault references.

silent
  • 14,494
  • 4
  • 46
  • 86
0

Two things to point:

1, You didn't override abstract void Configure(IFunctionsHostBuilder builder), Your code will throw out error(And please make sure your function app start from Startup.cs by using assembly, have a look of this.).

2, Your code has no matter about add appsettings.json to environment variables. You read the value of the environment variable, but where is the setting?

Below code should work:

[assembly: FunctionsStartup(typeof(FunctionApp81.Startup))]
namespace FunctionApp81
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            var text = File.ReadAllText("appsettings.json");
            var mydictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(text);
            foreach (var d in mydictionary)
            {
                Environment.SetEnvironmentVariable(d.Key,d.Value);
            }
        }

    }
}
Cindy Pau
  • 13,085
  • 1
  • 15
  • 27
  • 1
    Thanks for your response. Unfortunately it hasn't resolved the issue. I was already overriding the `Configure` method but I had left it out of the code extract. I have tried your suggestion of writing the settings to env variables but it still doesn't trigger. All other configuration in my `appsettings.json` file are working correctly, just not trigger binding. – Connell.O'Donnell Mar 02 '21 at 12:01
  • @Connell.O'Donnell The value in trigger and binding is getting from the environment variable, so if you put it to the env var, it should be able to get. Can you work fine on local? Or just can not work on azure? – Cindy Pau Mar 02 '21 at 12:04
  • 1
    Yeah, it works fine in local, getting the setting from `local.settings.json`. – Connell.O'Donnell Mar 02 '21 at 12:07
  • 1
    Interestingly, if I don't overwrite the setting in `local.settings.json`, it binds fine to `appsettings.json` without converting to env variables when running locally. – Connell.O'Donnell Mar 02 '21 at 12:12
  • 1
    Yes, because the local.settings.json reading is part of the Function debug runtime. The mechanism for running in the cloud is to use the Functions AppSettings – silent Mar 02 '21 at 12:13
  • @Connell.O'Donnell Okay, I know the situation you are facing. I will try to reproduce your problem tomorrow. Now I have something to do. Thank you for your reply.:) – Cindy Pau Mar 02 '21 at 12:30
0

@Kashyap The setting isn't visible in the portal. It should be coming from the appsettings.json file whereas the portal settings will be environment variables.

Looks like the way you publish/deploy your app to cloud is not publishing the app settings.

You can publish your settings in many ways. I prefer CLI, so two options are:

  • az functionapp config appsettings
  • func azure functionapp publish <app_name> --publish-settings-only --overwrite-settings: This one published settings from your local.settings.json to cloud.

There are many other options, including Portal.

Kashyap
  • 15,354
  • 13
  • 64
  • 103