【问题标题】:How to add Azure Functions extension如何添加 Azure Functions 扩展
【发布时间】:2017-11-29 06:45:03
【问题描述】:

我正在尝试使用 Azure Functions 中的扩展,如 this question 所示,但无法使其正常工作。

我的代码是这样的:(预编译,消费计划)

public static class FirstFunction
{
    [FunctionName("FirstFunction"),]
    public static void Run([TimerTrigger("0 */5 * * * *", RunOnStartup = true)]TimerInfo myTimer, TraceWriter log)
    {
        log.Info($"Started = { TestExtension.Started }");
        log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
    }
}

public class TestExtension : IExtensionConfigProvider
{
    public static bool Started = false;

    public void Initialize(ExtensionConfigContext context) {
        Started = true;
        Console.WriteLine("TestExtensionConsole");
        context.Trace.Error("TestExtension");

        throw new Exception("TextExtensionException");
    }
}

但是在运行时什么都没有发生。我看到了来自计时器 Started = false 的日志,但没有别的。

我需要启用扩展或其他什么吗?

【问题讨论】:

    标签: azure-functions precompiled


    【解决方案1】:

    在启动时运行代码并不是IExtensionConfigProvider 的目的。它被设计为用户创建自定义绑定的扩展点。

    因此,除非您的函数使用自定义绑定,否则IExtensionConfigProvider 的实现不会由运行时主机自动加载,请参阅code

    您所指的答案的作者是使用他对implement Dependency Injection 的扩展和自定义绑定,所以它适用于他。

    如果您打算将扩展程序用于自定义绑定,请在函数中使用该绑定,扩展程序将开始加载。

    【讨论】:

    • 感谢您的回答。我打算将它用于 DI,但首先我做了一个小测试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    • 2012-01-31
    • 1970-01-01
    相关资源
    最近更新 更多