Azure Function 在启动时运行代码

编程入门 行业动态 更新时间:2024-10-27 04:26:45
本文介绍了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 在启动时运行代码

本文发布于:2023-11-05 06:22:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560137.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:启动时   代码   Azure   Function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!