Introduction

With the new Isolated mode for Azure Functions it is all based off a host builder.

So this got me thinking. A HostBuilder is the similar concept to the how ASP.NET Core creates an “IHost” so does it have the same conventions in the Azure Functions world and can I use a different Ioc system with it?

TL;DR - Yes!

But let’s find out more!

Show me the code

The example code for this can be found in the basic branch.

Registering an Ioc Container

With the advent of ASP.NET Core 3.1 and above there was this concept of a ServiceProviderFactory that you can register with your host builder to replace the inbuilt one. This then allows using a ConfigureContainer<T> method to use Ioc container specific concepts outside of the base service registrations.

To do this with AutoFac first you have to reference the correct nuget package.

<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.2.0-preview.1" />

Current version is in preview but the basics have been the same for a number of versions now. Referencing the DependencyInjection package also brings in the base AutoFac package so you don’t have to reference it separately.

Once the package is referenced we then next need to specify the AutofacServiceProviderFactory as the service provider factory of choice with the host builder. This is done through the UseServiceProviderFactory registration method.

var host = new HostBuilder()
    .UseServiceProviderFactory(new AutofacServiceProviderFactory())

Now the service provider factory is registered we can not only register dependencies through the main ConfigureServices method and use the IServoceCollection but we can also use the ConfigureContainer method which has a generic constraint on the type of “container builder” you are using. In this instance we want to have access to the Autofac ContainerBuilder and this can done with the following method call.

var host = new HostBuilder()
    .UseServiceProviderFactory(new AutofacServiceProviderFactory())
    .ConfigureContainer<ContainerBuilder>(builder =>
    {
    })
    .ConfigureServices(services =>
    {
    })
    .ConfigureFunctionsWorkerDefaults()
    .Build();

Now the world is your oyster as they say. You can do simple registration through the ConfigureServices method or you can do more complicated registrations in the ConfigureContainer method.

Conclusion

In this post we have shown how to use a different Ioc Container with the Azure Functions host builder. The entry points on where to register your service dependencies and whether you need AutoFac specific registration details or base line Transient, Scopped or Singleton registrations which can be done through the ConfigureServices method. This opens up a lot of potential possibilities for using dependency injection in Azure Functions which the in process model didn’t offer and I’m interested in explorering more.

What are you thoughts about using a different container than the built in one in Azure Functions? Is it overhead or welcomed configurability? Let me know on Twitter @WestDiscGolf as I’d be interested in hearing your thoughts.