简体   繁体   English

VS2017中Azure函数项目中的应用程序初始化15.3.4?

[英]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. 在带有最新更新的Visual Studio 2017中,对于azure函数模板,我想要一些我可以在webjobs模板中初始化program.cs的东西。

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? 如果是,那怎么样?

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. Function App将根据您在部署时的绑定创建订阅。

For other scenarios (eg 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. 静态构造函数将在第一次函数调用时运行。

for azure functions template, I want something where I can initialize like program.cs in webjobs template. 对于azure函数模板,我想要一些我可以在webjobs模板中初始化program.cs的东西。

As far as I know, Azure functions do not have good support for this right now. 据我所知,Azure功能现在没有很好的支持。 You can find a similar question : 你可以找到一个类似的问题

Question: 题:

I have a C# function and want to know if there is any Initialization point. 我有一个C#函数,想知道是否有任何初始化点。 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. 请参阅开放的问题在这里我们回购这何处讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM