How to add Azure Functions extension
Asked Answered
M

1

4

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?

Madelinemadella answered 29/11, 2017 at 6:45 Comment(0)
P
10

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.

Phonography answered 29/11, 2017 at 8:44 Comment(1)
Thank you for your answer. I plan to use it for DI but first I did a small test.Madelinemadella

© 2022 - 2024 — McMap. All rights reserved.