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

How do I create an Azure API Management instance via C#?

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

你可以用 REST 做到这一点:
Deployments - Create Or Update

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

您必须在请求正文中将 link 传递给您的 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-Management ARM 模板

目前 Fluent api 不支持 API Management。这是关于此的 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 文档以了解其用法和示例。