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

查看:187
本文介绍了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天全站免登陆