簡體   English   中英

從 C# 為運行時版本 3.x 禁用 Azure Function?

[英]Disable Azure Function from C# for runtime version 3.x?

如何使用 C# 中的代碼禁用 Azure Function?

我正在使用 Azure 函數運行時版本 3.x

我正在實施一個分布式斷路器,靈感來自於帶有 Durable Entity 的無服務器斷路器 當電路打開時,我需要禁用隊列觸發的 Azure Function,而不是停止整個 function 應用程序。

我從How to disable functions in Azure Functions中看到,禁用 function 的推薦方法是設置AzureWebJobs.<FUNCTION_NAME>.Disabled應用程序設置。 但是我還沒有在 C# 中找到用於執行此操作的 API。 我希望我可以從我的 C# 代碼中調用與 Azure CLI 的az functionapp config appsettings set命令等效的內容。

I saw similar questions on SO like: azure set environment variable programmatically to disable an azure function and How to Enable/Disable Azure Function programmatically But those have answers from back in 2017 that use kudu APIs to change the disabled property in the function.json file ,我希望現在有更好的方法來做到這一點。 特別是因為如何在 Azure 功能中禁用功能的文檔說:

為 class 庫 function 生成的 function.json 文件不能直接編輯。 如果您編輯該文件,則對 disabled 屬性所做的任何操作都將無效。

不幸的是,我找不到任何這樣的文檔。 我得到的最接近的是

https://docs.microsoft.com/en-us/rest/api/appservice/webapps/createfunction

For instance to create the function: https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{name}/functions/{functionName}?api-version =2019-08-01

但是,此文檔也沒有使我接近您更新配置文件的要求。 或者我可能忽略了幾個模塊。 要求您在執行以下步驟之前進一步檢查

So here's is what I did, I was kind of trying to reverse engineer, I ran the commands in Azure CLI and captured the traces - my thought process - the Azure CLI internally run on python and issues the API request to the Azure.

運行以下命令並捕獲 Fiddler:

az functionapp config appsettings set --name <myFunctionApp> \
--resource-group <myResourceGroup> \
--settings AzureWebJobs.QueueTrigger.Disabled=true

是的 ! python 進程正在向https://management.azure.Z4D236D9A2D102C50FE6AD1C504更新 appsetting:

在此處輸入圖像描述

設置屬性在請求正文中發送:

在此處輸入圖像描述

我們可以硬編碼屬性或動態獲取它。

所以我運行了下面的 Azure CLI 命令

az functionapp config appsettings list --name <> --resource-group <>

我能夠看到隨 PUT 請求傳遞的上述屬性在此處輸入圖像描述

為上述命令取了提琴手

看到以下端點有一個 POST 請求: https://management.azure.com/subscriptions//resourceGroups//providers/Microsoft.Web/sites//config/appsettings/list?api-version=2019-08- 01

在此處輸入圖像描述

這些是同一組屬性包,它們作為 PUT 中的請求主體發送以設置屬性。

因此,在您的情況下,您將不得不請求上述端點來獲取屬性列表。 它是 json output。 將 AzureWebJobs.QueueTrigger.Disabled 的值更新為 True。

使用 PUT 方法發布更新的屬性以及諸如 Bearer Token & Content-Type: application/json; 字符集=utf-8

請求 URI: https://management.azure.com/subscriptions//resourceGroups//providers/Microsoft.Web/sites//config/appsettings?api-version=2019-08-01

Headers: Authorization: Bearer <> Content-Type: application/json; 字符集=utf-8`

請求正文:

{"kind": "<class 'str'>", "properties": }

我希望你能達到你的要求。

我希望這可以幫助你:)

我不推薦這個作為你的產品。 請嘗試在您的開發環境中進行監控。

暫無
暫無

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

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