0

I'm trying to create an azure durable function but it's very difficult to find some normal guides on this subject. I've setup the DI and I try to read the settings of the function but it crashes

I have setup an Azure Function project in VS 2019 and added a Durable Orchestrator Function Template. I removed all the "static" references from the class and all seem to work fine until I add the configurationbuilder in the startup file

Can anyone explain to me how this should work or give some guidance, where to find some explanation of the configuration of a durable functions ? What should I have in the host.json , local.settings.json and how this changes be when I publish it on the portal ?

My case is this. The startup file looks like this

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

[assembly: FunctionsStartup(typeof(DurableFunctions.Startup))]
namespace DurableFunctions
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            var settings = new ConfigurationBuilder()
                .AddEnvironmentVariables()
                .Build();
        }
    }
}

The host.json is like this

{
  "version": "2.0"
}

The local.settings.json

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

And the error I get when I start the debugger is this enter image description here

This is output:

[11/8/2019 10:29:04 AM] A host error has occurred during startup operation '8b80bc94-2b98-408b-895f-c5697430acfd'.
[11/8/2019 10:29:04 AM] Microsoft.Azure.WebJobs.Extensions.DurableTask: Value cannot be null.
[11/8/2019 10:29:04 AM] Parameter name: hostConfiguration.
Value cannot be null.
Parameter name: provider
Alexandru Aliu
  • 474
  • 1
  • 4
  • 17

2 Answers2

0

You want to instead override the ConfigureAppConfiguration method in your FunctionStartup class (https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#customizing-configuration-sources).

The following example takes the one provided in the documentation a step further by adding user secrets.

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

    builder.ConfigurationBuilder
        .SetBasePath(context.ApplicationRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
        .AddJsonFile($"appsettings.{context.EnvironmentName}.json", optional: true, reloadOnChange: false)
        .AddUserSecrets(Assembly.GetExecutingAssembly(), true, true)
        .AddEnvironmentVariables();
}

By default, configuration files such as appsettings.json are not automatically copied to the Azure Function output folder. Be sure to review the documentation (https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#customizing-configuration-sources) for modifications to your .csproj file. Also note that due to the way the method appends the existing providers it is necessary to always end with .AddEnvironmentVariables().

A deeper discussion on configuration in an Azure Function can be found at Using ConfigurationBuilder in FunctionsStartup

Terence Golla
  • 1,018
  • 1
  • 13
  • 12
0
Did you get a response to this?
Did you try adding the local settings from the json, before the Environment Variables?
var config = new ConfigurationBuilder()
    .SetBasePath(context.FunctionAppDirectory)
    // This gives you access to your application settings in your local development environment
    .AddJsonFile("local.settings.json", optional: true, reloadOnChange: false)
    .AddJsonFile("secret.settings.json", optional: true, reloadOnChange: false)
    // This is what actually gets you the application settings in Azure
    .AddEnvironmentVariables()
    .Build();

I am having trouble when I deploy, I have set up a variable group yet they are not being picked up.

MintyOwl
  • 71
  • 1
  • 10