Application Initialization in Azure Function Project in VS2017 15.3.4?

2.3k views Asked by At

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?

2

There are 2 answers

0
Fei Han On

for azure functions template, I want something where I can initialize like program.cs in webjobs template.

As far as I know, Azure functions do not have good support for this right now. You can find a similar question:

Question:

I have a C# function and want to know if there is any Initialization point. I have dependency injection containers that need initialization and want to know where to do that.

Mathew's reply

We don't have a good story for this right now. Please see open issue here in our repo where this is discussed.

6
Mikhail Shilkov On

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.