Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autofac Dependency Injection in Azure Function

I am trying to implement DI using Autofac IOC in Azure function. I need to build the container, but not sure where to put the code to build the container

like image 672
Ramkumar Singh Avatar asked Jun 06 '17 18:06

Ramkumar Singh


People also ask

What is Autofac dependency injection?

Autofac is an open-source dependency injection (DI) or inversion of control (IoC) container developed on Google Code. Autofac differs from many related technologies in that it sticks as close to bare-metal C# programming as possible.

What is dependency injection in Azure functions?

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. Dependency injection in Azure Functions is built on the . NET Core Dependency Injection features.

What is the use of Autofac in MVC?

AutoFac provides better integration for the ASP.NET MVC framework and is developed using Google code. AutoFac manages the dependencies of classes so that the application may be easy to change when it is scaled up in size and complexity.


4 Answers

I've written a different answer to the main question, with a different solution, totally tied to the main question.

Previous solutions were either manually initializing a DI or using the decorator way of doing it. My idea was to tie the DI to the Functions Builder in the same way we do with aspnet, without decorators.

I don't know why my post got deleted by @MartinPieters, it seems that it was not even read.

I found no way to officially disagree with that decision, so I kindly ask that the moderator read my answer again and undelete it.

like image 141
Marcos Junior Avatar answered Oct 10 '22 17:10

Marcos Junior


I did write a blog entry for doing dependency injection with Autofac in Azure Functions. Have a look here: Azure Function Dependency Injection with AutoFac: Autofac on Functions

It follows a similar approach like the one by Boris Wilhelms. Another implementation based on Boris' approach can be found on github: autofac dependency injection

-- update ---

With Azure Function v2 it is possible to create nuget packages based on .net standard. Have a look onto Azure Functions Dependency Injection with Autofac: Autofac on Functions nuget Package

like image 25
Holger Leichsenring Avatar answered Oct 10 '22 17:10

Holger Leichsenring


Azure Functions doesn't support dependency injection yet. Follow this issue for the feature request https://github.com/Azure/Azure-Functions/issues/299

like image 24
ahmelsayed Avatar answered Oct 10 '22 16:10

ahmelsayed


While Azure Functions does not support DI out of the box, it is possible to add this via the new Extension API. You can register the container using an IExtensionConfigProvider implementation. You can find a full example DI solution in Azure here https://blog.wille-zone.de/post/azure-functions-proper-dependency-injection/.

like image 30
Boris Wilhelms Avatar answered Oct 10 '22 15:10

Boris Wilhelms