如何使用 azure .net SDK 或 Fluent API 验证 ARM 模板?

How to validate ARM Template using azure .net SDK or Fluent API?

如何使用 azure .net SDK 或 Fluent 验证上传的 ARM 模板 API? 我想像 azure portal 一样使用 azure .net SDK 或 Fluent API 验证我上传的 ARM 模板? 作为参考,请参见下图,如果 ARM 模板无效,天蓝色显示消息,所以我想使用任何 .net API 或 REST API.

来实现同样的事情

@Jim 我得到以下错误:

如果你想验证你的arm模板,请参考以下步骤

  1. 创建服务主体并将贡献者角色分配给 sp
az ad sp create-for-rbac -n "MyApp"
  1. 安装包
Install-Package Microsoft.Azure.Management.ResourceManager.Fluent -Version 1.34.0
  1. 代码
 string clientId = "23****9c";
            string clientSecret = "?s****/k";
            string tenantDomain = "";
            string subscription = "";
            var creds= SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId, clientSecret, tenantDomain, AzureEnvironment.AzureGlobalCloud);
            var restClient = RestClient.Configure()
                .WithEnvironment(AzureEnvironment.AzureGlobalCloud)
                .WithCredentials(creds)
                .WithLogLevel(HttpLoggingDelegatingHandler.Level.BodyAndHeaders)
                .Build();


            ResourceManagementClient managementClient = new ResourceManagementClient(restClient);
            managementClient.SubscriptionId = subscription;

            //Validates whether the specified template is syntactically correct and will be accepted by Azure Resource Manager..
            DeploymentValidateResultInner res = await managementClient.Deployments.ValidateAsync("<groupName>", "<deployName>", new DeploymentInner()
            {
                Location = "",
                Properties = new DeploymentProperties()
                {
                    ParametersLink = new ParametersLink("uri"),
                    TemplateLink = new TemplateLink("")
                }
            });

            Console.WriteLine(res.Error.Message);

            // get changes that will be made by the deployment if executed at the scope of resource group
            WhatIfOperationResultInner res1 = await  managementClient.Deployments.WhatIfAsync("<groupName>", "<deployName>", new DeploymentWhatIf() { 
                  Location="",
                   Properties= new DeploymentWhatIfProperties() {
                       ParametersLink = new ParametersLink("uri"),
                       TemplateLink = new TemplateLink("")
                   }
            });

            foreach (var change in res1.Changes) {
               // 
            }