Skip to content

Latest commit

 

History

History
93 lines (73 loc) · 4.58 KB

8-use-dependency-injection-in-dotnet-azure-functions.md

File metadata and controls

93 lines (73 loc) · 4.58 KB

Workshop: Use dependency injection in .NET Azure Functions

Introduction

Azure Functions supports the dependency injection (DI) software design pattern, which is a technique to achieve Inversion of Control (IoC) between classes and their dependencies, which means that the classes do not create or manage their dependencies, but receive them from an external source. This way, you can separate the concerns of your functions from the concerns of their dependencies, such as configuration, logging, data access, or business logic. You can also replace or mock your dependencies easily when testing or debugging your functions

Using dependency injection can help you write cleaner, more modular, and more testable code. For more information of how to leverage dependency injection to configure services lifetime, use options and settings for configurations in Azure Functions, see Dependency injection in Azure Functions.

Learning Objectives

  1. Use dependency injection in .NET Azure Functions to separate the concerns of your functions from the concerns of their dependencies, such as configuration, logging, data access, or business logic.

Challenges

  1. Update a function app to use dependency injection.
  2. Use injected ILogger in your functions.

Challenge 1: Update a function app to use dependency injection

  1. Before you can use dependency injection in your backend functionapp, you must install the following NuGet packages:

    pushd src/backend
    dotnet add package Microsoft.Extensions.DependencyInjection --version 7.0.0
    dotnet add package Microsoft.Azure.Functions.Extensions --version 1.1.0
    popd
  2. Create new file Startup.cs where you will register your services. To register services, create a method to configure and add the Healthz component to an IFunctionsHostBuilder instance. The Azure Functions host creates an instance of IFunctionsHostBuilder and passes it directly into your method.

    using Microsoft.Azure.Functions.Extensions.DependencyInjection;
    using Microsoft.Extensions.DependencyInjection;
    
    [assembly: FunctionsStartup(typeof(backend.Startup))]
    namespace backend
    {
        public class Startup : FunctionsStartup
        {
            public override void Configure(IFunctionsHostBuilder builder)
            {
    
                builder.Services.AddSingleton<Healthz>();
            }
        }
    }
  3. Update Healthz.cs function to be non-static.

  4. Run locally your backend application and validate is working as expected.

Challenge 2: Use injected ILogger in your functions

The host injects ILogger and ILoggerFactory services into constructors. However, by default these new logging filters are filtered out of the function logs. You need to modify the host.json file to opt-in to additional filters and categories.

  1. Add a constructor that accepts <ILogger> log as a parameter to Healthz.cs. The Azure Functions host creates an instance of ILogger and passes it directly into your constructor.

    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Extensions.Http;
    using Microsoft.AspNetCore.Http;
    using Microsoft.Extensions.Logging;
    
    namespace backend
    {
        public class Healthz
        {
            private readonly ILogger<Healthz> _log;
    
            public Healthz(ILogger<Healthz> log)
            {
                _log = log;
            }
    
            ...
  2. Opt-in new additional filter in hosts.json by provising Healthz class namespace and name:

    "logLevel": {
        // "default": "Information"
        "backend.Healthz": "Information"
    }
  3. Run locally your backend application and validate logs are written to console while Healthz endpoint is called.

Additional resource

Name Description
Use dependency injection in .NET Azure Functions https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection