Azure Functions In .Net 5 – Dependency Injection

In my previous entry, we talked about how Azure Function with .Net 5 with Isolated Process. In that article, we talked about the presence of the Startup.cs class that is the entry point of the execution of the functions and the process in charge of the execution.

In this class, we can find the Main method that is the entry point for the application. Here it is where we find the initialization of the variable HostBuiler that is executed with the Run method.

Among the extension methods that the Microsoft.Extensions.Hosting namespace offers us, we find the ConfigureServices method, which is where we can do the dependency injection. This method expects to receive a delegate of type Action for a collection of services (IServiceCollection) that allows us to configure the services we want to inject.

So this is where we can, through a lambda expression, pass the different services that we hope to inject into our functions as parameters.

.ConfigureServices(services => {
    services.AddHttpClient("Swapi", configuration => {
        configuration.BaseAddress = new Uri(Environment.GetEnvironmentVariable("SwapiUrl"));
    });
    services.AddDbContext<DemoDatabaseContext>(options => {
        options.UseSqlServer(Environment.GetEnvironmentVariable("DbConnectionString"));
    });
    services.AddTransient<IStoreBll, StoreBll>();
})

Leaving us a Main class like:

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Demo.LatinoNet.AzFunNet5.Business;
using Demo.LatinoNet.AzFunNet5.Models;
namespace Demo.LatinoNet.AzFunNet5
{
    public class Program
    {
        public static void Main()
        {
            var host = new HostBuilder()
                .ConfigureFunctionsWorkerDefaults()
                .ConfigureServices(services => {
                    services.AddHttpClient("Swapi", configuration => {
                        configuration.BaseAddress = new Uri(Environment.GetEnvironmentVariable("SwapiUrl"));
                    });
                    services.AddDbContext<DemoDatabaseContext>(options => {
                        options.UseSqlServer(Environment.GetEnvironmentVariable("DbConnectionString"));
                    });
                    services.AddTransient<IStoreBll, StoreBll>();
                })
                .Build();
            host.Run();
        }
    }
}

Once the services have been configured for our functions, it is in them where we can now have the injection of dependencies as usual, in the constructors of our classes:

public class Function1
{
    private readonly IHttpClientFactory _httpClientFactory;
    private readonly DemoDatabaseContext _context;
    private readonly IStoreBll _storeBll;
    public Function1(IHttpClientFactory httpClientFactory,
        DemoDatabaseContext context,
        IStoreBll storeBll)
    {
        _httpClientFactory = httpClientFactory;
        _context = context;
        _storeBll = storeBll;
    }
    ...


Similar Articles