0

In my Azure function, the event name comes from an environment variable called EVENT_HUB_NAME (notice the var in the following code it's wrapped between % to represent this as environment variable).

    public class BasicAzureFunction
    {

        [FunctionName("BasicAzureFunction")]
        public async Task Run(
            [EventHubTrigger("%EVENT_HUB_NAME%", Connection = "EVENT_HUB_CONNECTION_STRING")] EventData events)
        {
            // TODO: Business logic
        }
    }

However, if customer does not provide this setting through the Azure console, I want to set a default value for this event hub variable. Otherwise, the Azure function won't be able to load. So, I found this workaround using Environment class APIs:

public class Startup : FunctionsStartup
{
    private const string EVENT_HUB_NAME_DEFAULT = "aas-modeldatachanged-evh";

    public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
    {
        if (Environment.GetEnvironmentVariable("EVENT_HUB_NAME") == null) {
            Environment.SetEnvironmentVariable("EVENT_HUB_NAME", EVENT_HUB_NAME_DEFAULT);
        }
    }
}

Are there any better approaches on how to inject a default value to an environment variable? Any help or advice is appreciated.

As side note, I found the next link but it does not seem to inject the variable properly: https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#customizing-configuration-sources

oscar.fimbres
  • 1,145
  • 1
  • 13
  • 24

1 Answers1

0

I believe, if you use the Environment.SetEnvironmentVariable() method, this is limited to current process where it is not added to the application settings of Azure portal Function App Configuration:

enter image description here

As suggested by @HuryShen, that the variables will be gone after restarting the function when using SetEnvironmentVariable, but this will run the requirement to use the desired configuration setting in runtime.