Azure Function run code on startup
Asked Answered
G

2

16

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"

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}");
            }
        }

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.

Guttapercha answered 3/10, 2017 at 21:25 Comment(1)
Possible duplicate of Is it possible to run code when a Azure function's Hub is starting?Bacteria
W
14

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.
     }
  }
}
What answered 18/10, 2017 at 7:58 Comment(8)
Important thing to note is that IExtensionConfigProvider won't be loaded unless you are using custom bindings in one of the functions, see this answerBacteria
I wrote a post about the logic, when and how IExtensionConfigProvider are detected and instantiated: blog.wille-zone.de/post/…What
Thanks @Boris for your post. It resolved one of my issues. Is there any way to avoid putting the IExtensionConfigProvider implementation in the same assembly as the BindingAttribute? So that I can reuse the BindingAttribute in different other Azure Function projects. I have only one IExtensionConfigProvider so an 'extensions.json' wouldn't work in my case.Tundra
Note that if you are, like us, using V3 of the WebJobs runtime, then the above will not work and you need to follow the accepted answer to this question: #52124038Babylon
How would you solve this if you were using NodeJs based (javascript) Azure functions?Bombardon
@Bombardon the answer to running startup code on Node can be found here: #48816335Nesta
Anyone knows how to achieve this for a java function? Azure really needs to improve their docs...Kuopio
How can I run async code inside the Initialize?Moreen
N
9

At the 2019 Build conference, Microsoft released the functionality to have a callable method when the Azure Function app starts up. This can be used for registering DI classes, creating static DB connections, etc.

The documentation for these new features can be found at Azure Function Dependency Injection

Nuclei answered 10/5, 2019 at 17:14 Comment(1)
this only valid for V2 azure functionsCasanova

© 2022 - 2024 — McMap. All rights reserved.