Azure函数中的Autofac依赖注入
答
我觉得现在你需要做喜欢的事,丑陋:
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
}
答
虽然Azure的功能,不支持DI开箱即用,它可以添加这通过新的扩展API。您可以使用IExtensionConfigProvider实现注册容器。您可以在Azure https://blog.wille-zone.de/post/azure-functions-proper-dependency-injection/中找到一个完整的示例DI解决方案。
答
您可以使用自定义[inject]属性来完成此操作。看到这里的例子https://blog.wille-zone.de/post/azure-functions-proper-dependency-injection/
答
我写了一个博客文章,用Azure函数中的Autofac进行依赖注入。看看这里: Azure Function Dependency Injection with AutoFac: Autofac on Functions
它遵循类似的方法,如上述鲍里斯威廉姆斯所描述的。 其实还有另一种基于Boris Wilhelms代码的方法。你可以在github上找到它:An Autofac based implementation of Dependency Injection based on Boris Wilhelms Azure Function Dependency Injection Project
这是我现在使用的(尽管是脏的感觉)技术。不确定是否有任何其他选项,直到函数提供了一个可扩展点来注入自己的值 –