Azure Resource Group Set Tags with SDK?

2019-05-16 10:39发布

问题:

I have used code similar to http://blogs.infinitesquare.com/b/tranise/archives/-azure-resource-group-une-nouvelle-facon-dorganiser-ses-environnements-dans-azure#.Vr1RrfIrLgk to create a Resource Group in Azure with C#. I am now trying to extend it by putting Tags on the Resource.

Adding resourceGroupGetResult.ResourceGroup.Tags.Add("a","1") works, but a subsequent CreateOrUpdateAsync call fails with

"InvalidRequestContent: The request content was invalid and could not be deserialized: 'Could not find member 'provisioningState' on object of type 'ResourceGroupDefinition'. Path 'provisioningState', line 9, position 23.'."

What is the correct way to add Tags to a Resource Group? Of course I will want to add the same Tags to resources within the Group after this.

Thanks.

回答1:

I assume you're using nuget package id="Microsoft.Azure.Management.Resources" version="3.4.0-preview".

The sample code for adding tag(s) to Azure Resource Group using the ARM .NET SDK above is as below:

var tag = new Dictionary<string, string> {{tagName, tagValue}};
var rg = new ResourceGroup() { Name = rgName, Location = rgLocation, Tags = tag };
await client.ResourceGroups.CreateOrUpdateAsync(rgName, rg);

Note: This code sample should backward-compatible with older release version of the above-mentioned nuget package.

Hope this helps!