簡體   English   中英

如何使啟動Azure功能

[英]How to make startup Azure Function

我有這樣的Azure函數

[FunctionName("Function1")]
public static void Run([ServiceBusTrigger("myqueue", AccessRights.Manage,      Connection = "AzureWebJobsServiceBus")]string myQueueItem, TraceWriter log)
    {
        log.Info($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
    }

我想在應用程序的啟動或OnInit中動態綁定myqueue和AzureWebJobServiceBus連接字符串,而無需作為上述方法的參數。 我的意思是,我想首先運行一個像WebJob中的Program.cs這樣的方法來綁定或啟動全局變量。 我可以在Azure Function中做到這一點,怎么做? 非常感謝

部署之前,這里的屬性被編譯成一個function.json文件,其中包含有關綁定所要通信的信息。 連接字符串通常會引用應用程序設置。 這些都不能在代碼本身內進行修改(因此Program.cs無法修改function.json綁定)。

您能否再分享您的方案? 如果您要監聽多個隊列,是否可以為每個隊列部署一個功能? 鑒於功能的無服務器性質,部署額外的功能沒有不利之處。 讓我知道-很高興看到我們能否為您提供所需的幫助。

編輯

以下建議不適用於Trigger ,僅適用於Binding 我們必須等待團隊在Azure Functions中支持Key Vault終結點, 請參閱此GitHub問題


我認為您正在尋找的是所謂的命令式綁定

我昨天才發現他們,也對他們有一個問題 使用這些類型的綁定,您可以動態地設置所需的綁定,因此您可以從其他位置(例如全局變量或某些初始化代碼)檢索數據並將其用於綁定中。

我使用它的目的是從Azure Key Vault檢索一些值,但是您當然也可以從其他地方檢索數據。 一些示例代碼。

// Retrieving the secret from Azure Key Vault via a helper class
var connectionString = await secret.Get("CosmosConnectionStringSecret");
// Setting the AppSetting run-time with the secret value, because the Binder needs it
ConfigurationManager.AppSettings["CosmosConnectionString"] = connectionString;

// Creating an output binding
var output = await binder.BindAsync<IAsyncCollector<MinifiedUrl>>(new DocumentDBAttribute("TablesDB", "minified-urls")
{
    CreateIfNotExists = true,
    // Specify the AppSetting key which contains the actual connection string information
    ConnectionStringSetting = "CosmosConnectionString",
});

// Create the MinifiedUrl object
var create = new CreateUrlHandler();
var minifiedUrl = create.Execute(data);

// Adding the newly created object to Cosmos DB
await output.AddAsync(minifiedUrl);

您還可以在命令式綁定中使用其他一些屬性,我相信您會在文檔(第一個鏈接)中看到它。

除了使用命令式綁定,您還可以使用應用程序設置

最佳做法是,應使用應用程序設置而不是配置文件來管理機密和連接字符串。 這限制了對這些機密的訪問,並可以安全地將function.json存儲在公共源代碼控制存儲庫中。 每當您要根據環境更改配置時,應用程序設置也很有用。 例如,在測試環境中,您可能想要監視其他隊列或Blob存儲容器。 只要用百分號括起來的值(例如%MyAppSetting%),就可以解析應用程序設置。 請注意,觸發器和綁定的連接屬性是一種特殊情況,會自動將值解析為應用程序設置。 以下示例是一個Azure隊列存儲觸發器,該觸發器使用應用程序設置%input-queue-name%定義要在其上觸發的隊列。

 { "bindings": [ { "name": "order", "type": "queueTrigger", "direction": "in", "queueName": "%input-queue-name%", "connection": "MY_STORAGE_ACCT_APP_SETTING" } ] } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM