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

posted @ 2021-04-01 17:11  MicrosoftReactor  阅读(438)  评论(1编辑  收藏  举报