Azure函数中的Autofac依赖注入

Ram*_*ngh 6 dependency-injection inversion-of-control azure autofac azure-functions

我试图在Azure功能中使用Autofac IOC实现DI.我需要构建容器,但不确定将代码放在何处构建容器

Hol*_*ing 11

我写了一篇博客文章,用于在Azure Functions中使用Autofac进行依赖注入.看看这里: 使用AutoFac进行Azure功能依赖注入:功能上的Autofac

它采用类似于Boris Wilhelms的方法.基于Boris方法的另一种实现可以在github上找到:autofac依赖注入

- 更新---

使用Azure Function v2,可以基于.net标准创建nuget包.通过Autofac查看 Azure Functions依赖注入:函数nuget包上的Autofac


Mik*_*kov 5

我想现在你需要做一些丑陋的事情:

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
}
Run Code Online (Sandbox Code Playgroud)