Azure Functions Intermediate

Dependency injection and configuration

The isolated worker model uses the same hosting, dependency injection and configuration as ASP.NET Core. You register services in Program.cs, inject them through constructors and bind settings to typed options.

Where a function gets its dependencies
  1. Function classCreated per invocation with constructor injection.
  2. Your servicesRegistered in Program.cs with a lifetime.
  3. OptionsTyped settings bound from configuration and validated.
  4. Configuration sourcesApp settings, Key Vault references, App Configuration.

The same DI and configuration you know from ASP.NET Core.

Program.cs

Everything is set up before the host starts, just like a web app.

C#
using Microsoft.Azure.Functions.Worker.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = FunctionsApplication.CreateBuilder(args);
builder.ConfigureFunctionsWebApplication();

builder.Services.AddHttpClient<IWeatherClient, WeatherClient>(c =>
    c.BaseAddress = new Uri("https://api.example.com/"));
builder.Services.AddSingleton<IClock, SystemClock>();
builder.Services.AddScoped<IOrderStore, SqlOrderStore>();
builder.Services.AddOptions<EmailOptions>()
    .Bind(builder.Configuration.GetSection("Email"))
    .ValidateDataAnnotations()
    .ValidateOnStart();

builder.Build().Run();

Lifetimes in a function app

  • Singleton: one instance for the app's life; use for clients that are safe to share, such as BlobServiceClient or ServiceBusClient.
  • Scoped: one instance per function invocation, the Functions equivalent of a request scope; good for a DbContext.
  • Transient: a new instance each time it is resolved.
  • Function classes themselves are created per invocation, so constructor injection is cheap.

Settings and secrets

App settings become environment variables. A double underscore means a nested key, so Email__From maps to Email:From in IConfiguration, and that works on Linux too.

For secrets, use a Key Vault reference as the setting's value, such as @Microsoft.KeyVault(SecretUri=https://my-vault.vault.azure.net/secrets/SmtpPassword/). The app reads it through its managed identity and code just sees the value.

For settings shared by many apps, or feature flags, add Azure App Configuration as a configuration source.

C#
public class EmailOptions
{
    [Required, EmailAddress] public string From { get; set; } = "";
    [Required] public string SmtpHost { get; set; } = "";
}

public class SendReceipt(IOptions<EmailOptions> options) { /* options.Value.From */ }

Interview question

How does dependency injection work in isolated worker Azure Functions?

The worker runs on the .NET generic host, so I register services in Program.cs on builder.Services and inject them through the function class constructor. Singleton, scoped and transient lifetimes work as in ASP.NET Core, with scoped meaning one function invocation. Configuration comes from app settings, local.settings.json and other providers, and I bind it to options classes.

Your progress is saved only in this browser.

Want the full details? Read the official Microsoft documentation for this topic.