­

使用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:

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

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

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

Visual Studio:

//translate.google.com/translate?hl=zh-CN&prev=_t&sl=en&tl=zh-CN&u=//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:

//translate.google.com/translate?hl=zh-CN&prev=_t&sl=en&tl=zh-CN&u=//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。

示例代碼庫:

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

OpenAPI:

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

Adaptive Cards:

//translate.google.com/translate?hl=zh-CN&prev=_t&sl=en&tl=zh-CN&u=//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

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

  • 開源HTTP API軟件包和工具

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

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

    //translate.google.com/translate?hl=zh-CN&prev=_t&sl=en&tl=zh-CN&u=//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文檔:

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

此博客系列中:

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

源代碼:

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