Azure Functions Dependency Injection: Autofac On Functions nuget Package

When starting to play around with Azure Functions, the lack of dependency injection support was pretty annoying. To overcome that issue I created a small library, Autofac On Functions, based on Azure Functions 1.x. Unfortunately it is not possible to generate a nuget package from these sources.

Azure On Function with nuget package available

Yesterday I started to play around with Azure Functions v2. It is available as beta version supporting dotnet standard and .net core. Updating was pretty relaxed beside the fact, that there had been some incompatible changes.

The good news is, packaging assemblies with reference to the Azure Functions SDK is now possible. So here it is:

Nuget Autofac On Functions

This is a quite early state, but it is fully functional and provides exactly the functionality that is necessary. You can also download the sources on github.

What you need to get it running

What do you need to enjoy Azure Functions with dependency injection?

You can just download the sample also from Github here. Nuget package 0.1.5 is referenced. Make sure, you have the latest Azure Functions that supports .net standard on your machine.

Create an Azure Functions project

Simple open Visual Studio, and create a new project. Select “Azure Functions”.


Select type of Azure Function

Azure Functions v2 offers a little more infrastructure, also the dialog to add Azure Functions looks a little different. I didn’t select a storage account as for the sample it is simply not necessary. An http trigger function is going to be created on base of .net standard.


Add dependency to AutofacOnFunctions

Right click your project and select “manage nuget packages”. Search for AutofacOnFunctions under “browse”. Install current version. Again, be aware that it “only” supports .net standard.


Add a test service

With above steps, Visual Studio created an Azure Function. Nuget package is there. What do I need to do to use Dependency Injection now?

Create a simple service to be injected into your http triggered Azure Function and the interface:

[pastacode lang=”java” user=”holgerleichsenring” repos=”autofaconfunctions” path_id=”AutofacOnFunctions.Samples.NetStandard/Services/Functions/TestIt.cs” revision=”” highlight=”” lines=”” provider=”github”/]

[pastacode lang=”java” user=”holgerleichsenring” repos=”autofaconfunctions” path_id=”AutofacOnFunctions.Samples.NetStandard/Services/Functions/ITestIt.cs” revision=”” highlight=”” lines=”” provider=”github”/]

Add a Bootstrapper

The bootstrapper will tell the framework which modules need to be loaded, thus which services shall be registered.

[pastacode lang=”java” user=”holgerleichsenring” repos=”autofaconfunctions” path_id=”AutofacOnFunctions.Samples.NetStandard/Bootstrap/Bootstrapper.cs” revision=”” highlight=”” lines=”” provider=”github”/]

Certainly, the “ServiceModule” contains your newly created test service:

[pastacode lang=”java” user=”holgerleichsenring” repos=”autofaconfunctions” path_id=”AutofacOnFunctions.Samples.NetStandard/Services/Modules/ServicesModule.cs” revision=”” highlight=”” lines=”” provider=”github”/]

Inject your service in Azure Functions

Use the InjectAttribute to let your service being injected by Autofac.

[pastacode lang=”java” user=”holgerleichsenring” repos=”autofaconfunctions” path_id=”AutofacOnFunctions.Samples.NetStandard/Services/Functions/Function1.cs” revision=”” highlight=”” lines=”” provider=”github”/]

See the Results

Just hit f5 and let’s see the results.


Custom provider for the InjectAttribute had been loaded. The provider will only be loaded, when there is at least one Inject attribute used. If an inject attribute is used, but there is no bootstrapper available, the framework will raise an exception as the Azure Function will not be able to start.

When you copy the marked url above and paste it in the browser of your choice, you should see this result:


Finally find all sources in this repository. Enjoy!

Final thoughts

I had an very interesting chat about property vs method injection within Azure Functions with Justin Yoo. You can follow the chat in his latest post Dependency Injection on Azure Functions. The actual question is: Does it make sense due to the static nature of Azure Functions to use method injection? Constructor injection certainly is not sensibly possible, would property injection be the better choice?

Actually I do like Justin’s approach with the FunctionFactory. To keep changes on method signatures low and therefore keep the effort in maintaining unit tests low, I would prefer to just inject the FunctionFactory instead of having a different approach.

I am going to set up a sample and let you guys know.

Update 24-09-2018:

  • As promised I did setup a sample for the FunctionFactory approach.  Actually I didn’t upload it anywhere as from my perspective this approach is not benefitial:
    The function itself doesn’t define the dependencies anymore. Using the factory hides this important information
  • There is actually no need for an infrastructural solution with a static “FunctionFactory” from my point of view. If the function shall consume a Factory, the InjectAttribute can be used for doing so. Just using depedency injection to access services does not undercut the overall architecture which should be primarly dependency injection usage with in best case no exception.
  • I do not like the fact, that the function itself follows a certain pattern to increase testability. A public static property that is used to change the behavior of the function is from my point of view not preferrable.

Just a sample to explain the approach. This is the code from Justin

[pastacode lang=”javascript” manual=”public%20static%20class%20CoreGitHubRepositoriesHttpTrigger%0A%7B%0A%20%20public%20static%20IFunctionFactory%20Factory%20%3D%20new%20CoreFunctionFactory(new%20CoreAppModule())%3B%0A%0A%20%20%5BFunctionName(%22CoreGitHubRepositoriesHttpTrigger%22)%5D%0A%20%20public%20static%20async%20Task%3CIActionResult%3E%20Run(%5BHttpTrigger(AuthorizationLevel.Function%2C%20%22get%22%2C%20Route%20%3D%20%22core%2Frepositories%22)%5DHttpRequest%20req%2C%20ILogger%20log)%0A%20%20%7B%0A%20%20%20%20var%20result%20%3D%20await%20Factory.Create%3CIGitHubRepositoriesFunction%3E(log)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.InvokeAsync%3CHttpRequest%2C%20object%3E(req)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.ConfigureAwait(false)%3B%0A%0A%20%20%20%20return%20new%20OkObjectResult(result)%3B%0A%20%20%7D%0A%7D” message=”” highlight=”” provider=”manual”/]

When set up like this, testing is quite straight. The only drawback is, that it is not possible to set up different container per function.

[pastacode lang=”javascript” manual=”public%20static%20class%20CoreGitHubRepositoriesHttpTrigger%0A%7B%0A%20%20%5BFunctionName(%22CoreGitHubRepositoriesHttpTrigger%22)%5D%0A%20%20public%20static%20async%20Task%3CIActionResult%3E%20Run(%5BHttpTrigger(AuthorizationLevel.Function%2C%20%22get%22%2C%20Route%20%3D%20%22core%2Frepositories%22)%5DHttpRequest%20req%2C%20ILogger%20log%2C%20%0A%09%09IFunctionFactory%20factory)%0A%20%20%7B%0A%20%20%20%20var%20result%20%3D%20await%20factory.Create%3CIGitHubRepositoriesFunction%3E(log)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.InvokeAsync%3CHttpRequest%2C%20object%3E(req)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.ConfigureAwait(false)%3B%0A%0A%20%20%20%20return%20new%20OkObjectResult(result)%3B%0A%20%20%7D%0A%7D” message=”” highlight=”” provider=”manual”/]


  1. Krzysztof said:

    Hi Holger. When I run code from first example I have exception that ILogger is not registered so can’t be injected. Is there missing something?

    February 22, 2019
    • Holger Leichsenring said:

      Hi Krzysztof,

      sorry for late answer. Can you please add some details?
      ->Which version do you use?
      ->Could you share some code?


      March 7, 2019
  2. Allan Walker said:

    Hi Holger; any progress on the hybrid approach to using the FunctionFactory?

    September 18, 2018
  3. Muhammad Afzal Qureshi said:

    Will this work with Azure Durable functions too ?

    June 25, 2018
    • Holger Leichsenring said:

      Hi Muhammad,

      actually good question, I didn’t give it a try. As durable functions is “just” orchestration, Azure functions should not be affected at all.
      Would be interesting to see, if the InjectAttribute can be directly used also in the orchestration functions, actually that should just work out.

      I will try it out and let you know.

      June 25, 2018
  4. Troy Witthoeft said:

    Nice approach. Of the various ways I’ve seen folks attempting to solve Azure Functions, DI this one feels the most polished. Thanks!

    June 13, 2018
    • Holger Leichsenring said:

      Many thanks, Troy!

      June 13, 2018

Comments are closed.