Azure Function在启动时运行代码

编程入门 行业动态 更新时间:2024-10-27 10:19:02
本文介绍了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:23:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560140.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:启动时   代码   Azure   Function

发布评论

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

>www.elefans.com

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