Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Function run code on startup

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.

like image 755
Shawn Avatar asked Oct 03 '17 21:10

Shawn


People also ask

How do I trigger Azure function every 5 minutes?

The TimerInfo object is passed into the function. The following example function triggers and executes every five minutes. The @TimerTrigger annotation on the function defines the schedule using the same string format as CRON expressions. The following example shows a timer trigger binding in a function.

How do I manually trigger Azure function?

Navigate to your function app in the Azure portal, select App Keys, and then the _master key. In the Edit key section, copy the key value to your clipboard, and then select OK. After copying the _master key, select Code + Test, and then select Logs.

How do I create a timer trigger in Azure function?

Create a timer triggered functionIn your function app, select Functions, and then select + Create. Select the Timer trigger template. Configure the new trigger with the settings as specified in the table below the image, and then select Create. Defines the name of your timer triggered function.

How do I trigger an Azure function?

Determine which trigger works best for your business needs. Create a timer trigger to invoke a function on a consistent schedule. Create an HTTP trigger to invoke a function when an HTTP request is received. Create a blob trigger to invoke a function when a blob is created or updated in Azure Storage.


1 Answers

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.
     }
  }
}
like image 150
Boris Wilhelms Avatar answered Sep 22 '22 19:09

Boris Wilhelms