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.

Shawn
  • 2,355
  • 14
  • 48
  • 98
  • 2
    Possible duplicate of [Is it possible to run code when a Azure function's Hub is starting?](https://stackoverflow.com/questions/45494953/is-it-possible-to-run-code-when-a-azure-functions-hub-is-starting) – Mikhail Shilkov Oct 03 '17 at 21:37

2 Answers2

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.
     }
  }
}
Boris Wilhelms
  • 621
  • 6
  • 9
  • 6
    Important thing to note is that `IExtensionConfigProvider` won't be loaded unless you are using custom bindings in one of the functions, see [this answer](https://stackoverflow.com/a/47548457/1171619) – Mikhail Shilkov Nov 29 '17 at 08:46
  • 3
    I wrote a post about the logic, when and how `IExtensionConfigProvider` are detected and instantiated: https://blog.wille-zone.de/post/azure-functions-iextensionconfigprovider-resolution-and-instantiates/ – Boris Wilhelms May 17 '18 at 12:08
  • 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. – Kayes Aug 24 '18 at 13:09
  • 3
    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: https://stackoverflow.com/questions/52123538/iextensionconfigprovider-not-initializing-or-binding-with-microsoft-azure-webjob – DenverCoder9 Jan 31 '19 at 03:48
  • 1
    How would you solve this if you were using NodeJs based (javascript) Azure functions? – codeshinobi Apr 08 '20 at 19:04
  • @codeshinobi the answer to running startup code on Node can be found here: https://stackoverflow.com/questions/48815835/azure-function-run-code-on-startup-for-node – alfreema Oct 13 '22 at 18:56
  • Anyone knows how to achieve this for a java function? Azure really needs to improve their docs... – SoulKa Jan 25 '23 at 07:29
  • How can I run `async` code inside the `Initialize`? – Falcon Stakepool Mar 16 '23 at 00:17
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

user3459730
  • 369
  • 5
  • 6