Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application Initialization in Azure Function Project in VS2017 15.3.4?

In Visual Studio 2017 with the latest update, for azure functions template, I want something where I can initialize like program.cs in webjobs template.

I am trying to create a new subscription with new namespace Manager when application initializes so that I can listen to one service bus topic.

Is there a way to do that? If yes then how?

like image 790
Vicky Avatar asked Sep 01 '25 06:09

Vicky


1 Answers

If you intend to create a subscription that this Function will be triggered on, there is no need to do that manually. Function App will create the subscription based on your binding at deployment time.

For other scenarios (e.g. timer-triggered function), you can do the initialization in a static constructor:

public static class MyFunction1
{
    static MyFunction1()
    {
        var namespaceManager = NamespaceManager.CreateFromConnectionString(connString);
        if (!namespaceManager.SubscriptionExists("topic1", "subscription1"))
        {
            namespaceManager.CreateSubscription("topic1", "subscription1");
        }
    }

    [FunctionName("MyFunction1")]
    public static void Run(
    // ...
}

Static constructor will run at the time of the first function call.

like image 163
Mikhail Shilkov Avatar answered Sep 02 '25 22:09

Mikhail Shilkov