I want to run code when the Azure Function's hub is starting and to be sure that's executed before any Azure function can be called in that hub. Can I do that?
Is it possible to run code when a Azure function's Hub is starting?
Asked Answered
What's Hub? Instance/server? –
Shuttlecock
Yes, instance. When the instance (the proccess) is starting... i want to run custom code, before any azure function can be executed. –
Mahalia
Given your are using precompiled C# functions, you could put your initialization code into static constructor:
public static class MyFunctionApp
{
static MyFunctionApp()
{
// Runs once when class is first loaded
}
public void Run([SomeTrigger] input)
{
// Runs on each event
}
}
Yes, i use precompiled AF with the new tooling in VS 2017. Is posible to do it whithout using static constructors? Static constructors has poblems with concurrency, and how are we sure the static constructor is called if MyFunctionApp is not instanced? –
Mahalia
It will be called exactly once and before the first function call. Any links to illustrate your concern? –
Shuttlecock
Maybe i must rephrase my question: are we sure the Azure Function Runtime create instances of ALL Azure Functions before the first Azure Function is Called? What happens if MyFunctionApp is not the first Azure function called? –
Mahalia
Imagine i have two classes. In Each one is a run method with the code of 1 Azure Function. The 2 classes are FunctionA and FunctionB. If i put the static constructor in FunctionA and the FunctionB is the first called, then the static constructor is not called first. am i wrong? –
Mahalia
I guess there's no guarantee in this case. Why not put both functions into the same class? –
Shuttlecock
What happens if the 2 Azure functions are called at the same time? I want to not use locks... –
Mahalia
If they are on the same class, static constructor is guaranteed to be called exacly once, no matter the order/timing of function calls. –
Shuttlecock
Hummm, I suppose without put all Azure Functions in the same class and without using locks, this is imposible? –
Mahalia
They don't have to be on the same class per se. They just need to each reference a class that has the static ctor. This will just work and you don't need to worry about locking. Better support is tracked by github.com/Azure/azure-webjobs-sdk-script/issues/586, but no clear ETA. –
Absorbance
© 2022 - 2024 — McMap. All rights reserved.