1. 程式人生 > >使用Azure API Management, Functions, Power Apps和Logic App構建應用

使用Azure API Management, Functions, Power Apps和Logic App構建應用

ASP.NET OpenAPI 可以非常方便的將我們的Web API專案自動文件化,除了自動文件化以外,我們還可以利用Azure API Management將Open API自動文件化了的Web API整合到當下最火的低程式碼應用當中,在這篇文章中,我將向您展示如何使用Azure API Management將Azure Functions,Power Apps和Logic Apps等等Azure Service 串聯在一起。Azure API Management能夠利用已文件化的API定義來公開我們的API 端點,這些端點可以輕鬆使用在低程式碼的應用程式中。來看看這有多簡單:

 

將您的API匯入Azure API Management

隨著Visual Studio 2019 16.9的釋出,我們增加了對釋出到現有Azure API Management服務例項以及建立Azure API Management的新消費模式例項的支援,所以只要您是Azure的客戶,您就可以在需要時使用API Management的監視,安全和整合功能。

 

當您釋出一個已經整合了Swashbukle.AspNetCore(在ASP.NET Core 5 Web API專案中為預設整合選項)的.NET Core API專案到Azure App Service的時候,Azure API Management 選項卡就會出現在釋出對話方塊中,你可以選擇一個現成的Azure API Management例項,也可以新建一個例項。

 

 

釋出完成後,您將能夠直接在Azure API Management門戶中測試您的API。在這裡,您還可以監測流量,控制訪問許可權並將API匯出到其他服務(本文的下一部分中將介紹如何將API匯出到其他服務)。

注:Visual Studio中只能新建Azure API Management的消費模式例項。這是Azure API Management的輕量級,無伺服器版本,按執行次數收費,每月提供100萬次免費呼叫。如果有現有的Azure API Management例項,也可以將其匯入其中。該功能可用於Azure API Management的任何服務級別。  

 

如果您不熟悉Azure API Management,您可以在docs.microsoft.com上了解更多有關使用Visual Studio或Visual Studio Code將API釋出到Azure API Management中的資訊。    

Azure API Management:

https://translate.google.com/translate?hl=zh-CN&prev=_t&sl=en&tl=zh-CN&u=https://azure.microsoft.com/services/api-management/%3FWT.mc_id%3Ddotnet-13135-bradyg

Azure API Management的輕量級,無伺服器版本,按執行次數收費,每月提供100萬次免費呼叫:

https://azure.microsoft.com/pricing/details/api-management/?WT.mc_id=dotnet-13135-bradyg

Visual Studio:

https://translate.google.com/translate?hl=zh-CN&prev=_t&sl=en&tl=zh-CN&u=https://docs.microsoft.com/aspnet/core/tutorials/publish-to-azure-api-management-using-vs%3Fview%3Daspnetcore-5.0%26WT.mc_id%3Ddotnet-13135-bradyg

Visual Studio Code:

https://translate.google.com/translate?hl=zh-CN&prev=_t&sl=en&tl=zh-CN&u=https://docs.microsoft.com/azure/api-management/visual-studio-code-tutorial%3FWT.mc_id%3Ddotnet-13135-bradyg

使用您的API和Logic Apps構建工作流程

釋出包含OpenAPI的Web API或Azure Functions(包含在本文後附的示例程式碼庫)之後,就可以使用Azure Logic Apps將它們用於業務工作流,定時任務或事件觸發的流程中。Logic Apps設計器會自動選擇匯入現有Azure API Management服務例項中的所有API,從而輕鬆確定可用的API。

由於Logic Apps設計器知道如何為我在API Management中匯入的API繪製OpenAPI文件化的定義,因此很容易弄清楚我需要呼叫哪個API才能建立重複的庫存檢查流程。

使用Azure Functions擴充套件工作流

通過引入Azure Functions的OpenAPI擴充套件,您能像在WebAPI Controllers中利用NSwag和Swashbukle匯出OpenAPI的描述一樣, 從Azure Functions中匯出你的OpenAPI描述。在示例程式碼中,您會發現一個通知功能,該功能可以構建一個Adaptive Cards,總結低檔產品的庫存狀態。使用該OpenAPI的擴充套件的功能屬性,Azure Function就可以使用OpenAPI進行文件化描述並匯入到Azure API Management了。 

public static class InventoryNotifier
{
    [OpenApiOperation(operationId: nameof(SendLowStockNotification),
      Visibility = OpenApiVisibilityType.Important)
]
    [OpenApiRequestBody(contentType: "application/json",
      bodyType: typeof(Product[]),
      Required = true)
    ]
    [FunctionName(nameof(SendLowStockNotification))]
    public static async Task<IActionResult> SendLowStockNotification(
        [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
        ILogger log)
    {
      // function code...
    }
}

匯入到Azure API Management後,我們就可以在複雜的Workflow比如Logic App中使用這些Azure Function了。比如我可以在Azure Portal中設計一個workflow, 這個workflow可以混合使用純Web API和利用OpenAPI匯出到Azure API Management的Azure Function。

示例程式碼庫:

https://translate.google.com/translate?hl=zh-CN&prev=_t&sl=en&tl=zh-CN&u=https://github.com/bradygaster/Contoso.Online.Orders%3FWT.mc_id%3Ddotnet-13135-bradyg

OpenAPI:

https://translate.google.com/translate?hl=zh-CN&prev=_t&sl=en&tl=zh-CN&u=https://github.com/Azure/azure-functions-openapi-extension%3FWT.mc_id%3Ddotnet-13135-bradyg

Adaptive Cards:

https://translate.google.com/translate?hl=zh-CN&prev=_t&sl=en&tl=zh-CN&u=https://adaptivecards.io/

 

使用您的API構建Power Apps

如果要在使用Office 365和Power Apps的環境中構建ASP.NET Core Web API和Azure Functions,Azure API Management可以充分發揮您的HTTP API開發才能。API Management APIs 可以輕鬆地作為自定義聯結器匯出到Power Platform。

在這裡,我已經開始構建一個可以分發給Microsoft 365 tenant中的任何使用者的移動應用程式。因為剛開始,所以我快速地添加了剛剛建立的自定義聯結器,該自定義聯結器會將Power Platform連線到Azure中託管的API。當我新增新的“Refresh”按鈕後,滑鼠放上去會出現一個公式輸入框,我在這個公式輸入框裡呼叫ClearCollect方法以清除集合變數productsCollection ,然後使用StoreAPIs.GetProducts操作重新填充集合。

Power Apps的優點在於,即使在設計模式下,它也可以進行資料繫結。因此,當我開發這個應用程式時,不必一遍又一遍地重新執行,資料會一直繫結到正確的控制元件上。

 

如果您對如何使用OpenAPI將您的API文件化並不瞭解,您可以參考我們之前釋出的內容:  

  • 使用ASP.NET Core 5 Web API建立可被發現的HTTP API

    https://translate.google.com/translate?hl=zh-CN&prev=_t&sl=en&tl=zh-CN&u=https://devblogs.microsoft.com/aspnet/creating-discoverable-http-apis-with-asp-net-core-5-web-api/%3FWT.mc_id%3Ddotnet-13135-bradyg

  • 開源HTTP API軟體包和工具

    https://translate.google.com/translate?hl=zh-CN&prev=_t&sl=en&tl=zh-CN&u=https://devblogs.microsoft.com/aspnet/open-source-http-api-packages-and-tools/%3FWT.mc_id%3Ddotnet-13135-bradyg

  • 使用Visual Studio Connected Services生成HTTP API客戶端

    https://translate.google.com/translate?hl=zh-CN&prev=_t&sl=en&tl=zh-CN&u=https://devblogs.microsoft.com/aspnet/generating-http-api-clients-using-visual-studio-connected-services/%3FWT.mc_id%3Ddotnet-13135-bradyg

  • 使用Azure API Management,Functions,Power Apps和Logic Apps構建應用程式(本文)

 


總結        

在這一系列文章中,我們帶您從一些確保正確使用OpenAPI對API進行文件化描述的通用指南開始,繼而討論了各種能夠幫助您更輕鬆地設計,構建和測試API的開源專案和工具。最後,向您展示瞭如何在各種應用程式構建和整合方案中使用這些已經文件化描述的API。希望這些能夠帶給您一些新的技巧,並給您一些靈感,幫助您在未來的專案中更愉快地構建API和應用程式。


下一步是什麼?    

我們很高興能夠與您在本系列中分享整個端到端.NET HTTP API開發人員的經驗。希望您已經看到.NET可以幫助您構建與行業標準規範,社群專案以及Microsoft工具(例如Visual Studio和Azure)良好整合的高質量API。

   

如果您是經驗豐富的.NET開發人員,則可以在.NET文件中深入研究一些更高階的主題,也可以檢視此部落格系列中的原始碼。  

.NET文件:

https://translate.google.com/translate?hl=zh-CN&prev=_t&sl=en&tl=zh-CN&u=https://docs.microsoft.com/aspnet/core/web-api/%3Fview%3Daspnetcore-5.0%26WT.mc_id%3Ddotnet-13135-bradyg

此部落格系列中:

https://translate.google.com/translate?hl=zh-CN&prev=_t&sl=en&tl=zh-CN&u=https://github.com/bradygaster/Contoso.Online.Orders%3FWT.mc_id%3Ddotnet-13135-bradyg

原始碼:

https://translate.google.com/translate?hl=zh-CN&prev=_t&sl=en&tl=zh-CN&u=https://github.com/bradygaster/Contoso.Online.Orders%3FWT.mc_id%3Ddotnet-13135-bradyg