If the business logic of your application relies heavily on the function app settings, it is recommended to inject the settings object as part of the Microsoft IoC container.
-
Make sure to add your local settings or function app settings as you would regularly do.
-
Create the settings class and add
IConfigurationparam into its constructor.using Microsoft.Extensions.Configuration; public class StreamingDataFlowSettings : `IStreamingDataFlowSettings` { public StreamingDataFlowSettings(IConfiguration config) { // extract setting values this.EventHubName = config.GetValue<string>("EVENT_HUB_NAME"); this.StorageAccountContainer = config.GetValue<string>("STORAGE_ACCOUNT_CONTAINER"); this.Timeout = config.GetValue<int>("TIMEOUT", 60); // throw exceptions if required fields are not met Guard.ThrowIfNull("EVENT_HUB_NAME", this.EventHubName); Guard.ThrowIfNull("STORAGE_ACCOUNT_CONTAINER", this.StorageAccountContainer); } public string EventHubName { get; private set; } public string StorageAccountContainer { get; private set; } public int Timeout { get; private set; } }
You may use the
Guardclass to validate your input and notify if a required field is missing during runtime. -
Register the settings class into the DI
using Microsoft.Azure.Functions.StreamingDataFlow.Extensions; public class Startup : FunctionsStartup { public override void Configure(IFunctionsHostBuilder builder) { var settings = new StreamingDataFlowSettings(builder.GetContext().Configuration); builder.Services.AddSingleton<IStreamingDataFlowSettings>(settings); builder.Services.RegisterServices(builder.GetContext().Configuration); } }
-
You can now inject this dependency object into your service constructors. For example:
[DependencyInjection(typeof(IIntegrationEventHandlerAsync<StreamingDataChanged>), ServiceType.Scoped, 1)] public class StreamingDataChangedEventHandlerAsync : IIntegrationEventHandlerAsync<StreamingDataChanged> { public StreamingDataChangedEventHandlerAsync( IStreamingDataFlowSettings settings) { this.settings = settings; } public async Task Handle(StreamingDataChanged eventData) { // add business logic... } }