Use dependency injection in .NET Azure Functions

Azure Functions supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies.

Azure Functions builds on top of the ASP.NET Core Dependency Injection features. You should understand services, lifetimes, and design patterns of ASP.NET Core dependency injection before using them in functions.

Installing dependency injection packages

In order to use the dependency injection features, you will need to include the NuGet package that exposes those APIs.

Install-Package Microsoft.Azure.Functions.Extensions

Registering services

To register services, you can create a configure method and add components to an IFunctionsHostBuilder instance. The Azure Functions host creates an IFunctionsHostBuilder and passes it directly into your configured method.

To register your configure method, you must add an assembly attribute that specifies the type for your configure method using the FunctionsStartup attribute.

[assembly: FunctionsStartup(typeof(MyNamespace.Startup))]

namespace MyNamespace
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddHttpClient();
            builder.Services.AddSingleton((s) => {
                return new CosmosClient(Environment.GetEnvironmentVariable("COSMOSDB_CONNECTIONSTRING"));
            });
            builder.Services.AddSingleton<ILoggerProvider, MyLoggerProvider>();
        }
    }
}

Service lifetimes

Azure Function apps provide the same service lifetimes as ASP.NET Dependency Injection, transient, scoped, and singleton.

In a function app, a scoped service lifetime matches a function execution lifetime. Scoped services are created once per execution. Later requests for that service during the execution reuse that instance. A singleton service lifetime matches the host lifetime and is reused across function executions on that instance.

Singleton lifetime services are recommended for connections and clients, for example a SqlConnection, CloudBlobClient, or HttpClient.

View or download a sample of different service lifetimes.

Logging services

If you need your own logging provider, the recommended way is to register an ILoggerProvider. For Application Insights, Functions adds Application Insights automatically for you.

Warning

Do not add AddApplicationInsightsTelemetry() to the services collection as it will register services that will conflict with what is provided by the environment.

Function app provided services

The function host will register many services itself. Below are services that are safe to take a dependency on. Other host services are not supported to register or depend on. If there are other services you want to take a dependency on, please create an issue and discussion on GitHub.

Service Type Lifetime Description
Microsoft.Extensions.Configuration.IConfiguration Singleton Runtime configuration
Microsoft.Azure.WebJobs.Host.Executors.IHostIdProvider Singleton Responsible for providing the ID of the host instance

Overriding Host Services

Overriding services provided by the host is currently not supported. If there are services you want to overriding, please create an issue and discussion on GitHub.

Next steps

For more information, see the following resources: