Azure Function 在启动时运行代码 [英] Azure Function run code on startup

查看:36
本文介绍了Azure Function 在启动时运行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种方法来在我的 Azure 函数启动时一次性运行一些代码(我在其中设置连接字符串、DI 和其他配置).所以现在,它在生成的 function.json 中调用一个 Run 方法作为入口点:

I am trying to find a way to run some code one time (where I set connection strings, DI, and other configs) when my Azure function starts. So right now, it calls a Run method as the entrypoint with this in the generated function.json:

"entryPoint": "MyFunctionApp.MessageReceiver.Run"

此 Run 方法使用 EventHubTrigger 并像这样处理传入的消息:

This Run method uses an EventHubTrigger and processes incoming messages like so:

[FunctionName("MessageReceiver")]
        public static void Run([EventHubTrigger("eventHubName", Connection = "eventHubConnection")]string message, TraceWriter log)
        {
            if (string.IsNullOrWhiteSpace(message))
            {
                log.Info($"C# Event Hub trigger function processed a message: {message}");
            }
        }

有没有一种方法可以在调用此 Run 方法之前在初始启动时运行一些代码?或者有没有办法声明一个我可以在这个类之前调用​​的入口点,然后调用 Run() 并以某种方式传入触发器?我正在尝试找到一种方法来避免像设置布尔属性以查看应用程序是否已启动之类的骇人听闻的东西.

Is there a way that I can run some code on the initial startup before this Run method is called? Or is there a way to declare an entrypoint that I can call before this class and then call Run() and somehow pass in the trigger? I am trying to find a way that avoids hackish stuff like setting boolean properties to see if the app has started.

推荐答案

你可以实现一个IExtensionConfigProvider.这些将被扫描并在启动"时执行.

You can implement an IExtensionConfigProvider. Those will be scanned and execute on "Startup".

using Microsoft.Azure.WebJobs.Host.Config;
namespace MyFunctionApp
{
  public class Startup : IExtensionConfigProvider
  {
     public void Initialize(ExtensionConfigContext context)
     {
        // Put your intialization code here.
     }
  }
}

这篇关于Azure Function 在启动时运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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