如何通过 C# 创建 Azure API 管理实例?

问题描述

我一直在使用 Microsoft.Azure.Management.Fluent 包来创建一个工具来构建我的环境并进行一些额外的设置。现在,我需要添加一个 API 管理实例。我在 Fluent SDK 中没有看到与 API 管理相关的任何内容。我假设它没有 SDK 包装器,我只需要自己进行 REST 调用。我正在寻找指南。

解决方法

目前,API Management 中不支持 Fluent api。这是关于此的issue

相反,还有另一个包 Microsoft.Azure.Management.ApiManagement 6.0.0-preview,您可以使用它来创建 API Management instance。代码如下:

        // you should provide the real credentialhere.
        ApiManagementClient client = new ApiManagementClient(the_credential);

        //provide the neccesary parameters to create the APIM instance.
        client.ApiManagementService.CreateOrUpdate(the_parameters);

另一种创建 API Management 的方法是使用此 API:Api Management Service - Create Or Update。您可以阅读 api 文档以了解其用法和示例。

,

您可以使用 REST 来实现:
Deployments - Create Or Update

PUT https://management.azure.com/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.Resources/deployments/{deploymentName}?api-version=2020-06-01

您必须在请求正文中将链接传递给您的 ARM Template

{
  "properties": {
    "templateLink": {
      "uri": "https://example.com/exampleTemplate.json"
    },"parameters": {},"mode": "Complete","onErrorDeployment": {
      "type": "SpecificDeployment","deploymentName": "name-of-deployment-to-use"
     }
   }
}

您可以将 ARM 模板存储在 Blob 存储中并在正文中引用它。

请在 GitHub - azure-quickstart-templates 上找到示例 API 管理 ARM 模板