我正在尝试找到一种方法来在我的 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 在启动时运行代码
发布评论