如何添加 Azure Functions 扩展 [英] How to add Azure Functions extension

查看:16
本文介绍了如何添加 Azure Functions 扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试验 Azure Functions 中的扩展,如 this question 但不能让它工作.

I'm experimenting with extensions in Azure Functions as shown in this question but can't get it to work.

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

My code looks like this: (pre-compiled, consumption plan)

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 的日志,但没有别的.

But nothing happens at runtime. I see the log from the timer Started = false but nothing else.

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

Do I need to enable extensions or something?

推荐答案

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

Running code on startup is not exactly the purpose of IExtensionConfigProvider. It's designed as extensibility point for user to create custom bindings.

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

So, unless your functions are using custom bindings, the implementations of IExtensionConfigProvider won't be auto-loaded by the runtime host, see code.

您所指的答案的作者正在使用他的扩展来实现依赖注入 带有自定义绑定,所以它适用于他.

The author of the answer that you are referring to was using his extension to implement Dependency Injection with custom bindings, so it works for him.

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

If you intend to use your extension for custom binding, then use that binding in a function and the extension will start loading.

这篇关于如何添加 Azure Functions 扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆