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