Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Azure Functions extension

I'm experimenting with extensions in Azure Functions as shown in this question but can't get it to work.

My code looks like this: (pre-compiled, consumption plan)

public static class FirstFunction
{
    [FunctionName("FirstFunction"),]
    public static void Run([TimerTrigger("0 */5 * * * *", RunOnStartup = true)]TimerInfo myTimer, TraceWriter log)
    {
        log.Info($"Started = { TestExtension.Started }");
        log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
    }
}

public class TestExtension : IExtensionConfigProvider
{
    public static bool Started = false;

    public void Initialize(ExtensionConfigContext context) {
        Started = true;
        Console.WriteLine("TestExtensionConsole");
        context.Trace.Error("TestExtension");

        throw new Exception("TextExtensionException");
    }
}

But nothing happens at runtime. I see the log from the timer Started = false but nothing else.

Do I need to enable extensions or something?

like image 250
adrianm Avatar asked Jan 03 '23 03:01

adrianm


1 Answers

Running code on startup is not exactly the purpose of IExtensionConfigProvider. It's designed as extensibility point for user to create custom bindings.

So, unless your functions are using custom bindings, the implementations of IExtensionConfigProvider won't be auto-loaded by the runtime host, see code.

The author of the answer that you are referring to was using his extension to implement Dependency Injection with custom bindings, so it works for him.

If you intend to use your extension for custom binding, then use that binding in a function and the extension will start loading.

like image 95
Mikhail Shilkov Avatar answered Mar 28 '23 04:03

Mikhail Shilkov