Intensity of Autofac dependencies in azure function

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

+3
dependency-injection inversion-of-control autofac azure azure-functions


source to share


5 answers


I think now you need to do something ugly like:



public static string MyAwesomeFunction(string message)
{
    if (MyService == null)
    {
        var instantiator = Initialize();
        MyService = instantiator.Resolve<IService>();
    }

    return MyService.Hello(message);
}

private static IService MyService = null;

private static IContainer Initialize()
{
    // Do your IoC magic here
}

      

+5


source to share


I wrote a blog post for performing dependency injection with Autofac in Azure Functions. Take a look here: Azure Function Dependency Injection with AutoFac: Autofile Features



This follows a similar approach, for example to Boris Wilhelms . Another implementation based on Boris's approach can be found on github: autofac dependency injection

+6


source to share


While Azure Functions does not support DI out of the box, this can be added using the new extension API. You can register the container using the IExtensionConfigProvider implementation. You can find a complete example of a DI solution in Azure here https://blog.wille-zone.de/post/azure-functions-proper-dependency-injection/ .

+3


source to share


Azure Functions does not yet support dependency injection. Follow this issue for feature request https://github.com/Azure/Azure-Functions/issues/299

+2


source to share


You can do this using the special [inject] attribute. An example is here https://blog.wille-zone.de/post/azure-functions-proper-dependency-injection/

0


source to share







All Articles
Loading...
X
Show
Funny
Dev
Pics