DevTestLabs VirtualNetwork-Subnet:门户和 .net sdk 之间的不一致

DevTestLabs VirtualNetwork-Subnet: inconsistency between portal and .net sdk

我正在尝试以编程方式将 Use in virtual machine creationAllow public IP creation 设置为 Yes。我找到了正确的方法,这是我的代码:

var fragment = new VirtualNetworkFragment(
    subnetOverrides: virtualNetwork.Subnets.Select(s =>
        new SubnetOverrideFragment(
            labSubnetName: s.Key,
            resourceId: s.Value.Inner.Id,
            useInVmCreationPermission: "Allow",
            usePublicIpAddressPermission: "Allow")).ToList());

var vnet = await BaseClientFactory.CreateDevTestLabClient(azureClient)
    .VirtualNetworks
    .UpdateWithHttpMessagesAsync(dbLab.ResourceGroupName, dbLab.LabId.ToString(), virtualNetworkId, fragment);

不幸的是,代码不起作用,但它也没有抛出任何异常。当我序列化 fragment 变量以将其与在 Azure 上手动执行此操作时发送的请求进行比较时,存在细微差别。

这是使用 NewtonsoftJson 序列化时代码中主体的样子(我从 Watch window 中获取):

以下是通过 Azure 发送的正文:

唯一的两个变化是我的代码生成了一个请求,其中 allowedSubnetsnull 并且(我认为更重要的是)结构在我的代码生成 properties.subnetOverrides 而不是正确的 json 结构。我不确定这是错误还是我做错了什么 - 有什么想法吗?

根据我的研究,update action just can be used to modify tags of virtual networks and all other properties will be ignored. If you want to modify the properties of virtual networks, please use Create Or Update 动作。关于如何用c#sdk实现Microsoft.Azure.Management.DevTestLabs,请参考以下步骤

  1. Create a service principal and assign role to the sp

  2. 代码

            string clientId = " sp appId";
            string clientSecret = "sp password";
            string tenantId = "";
            string subscriptionId = "";
            var credentials = SdkContext.AzureCredentialsFactory
                .FromServicePrincipal(clientId,
                    clientSecret,
                    tenantId,
                    AzureEnvironment.AzureGlobalCloud);

            DevTestLabsClient client = new DevTestLabsClient(credentials);
            client.SubscriptionId = subscriptionId;
            VirtualNetwork vnet = await client.VirtualNetworks.GetAsync("testdevlab", "test", "Dtltest");
             // modify the vent's properties 
             vnet.SubnetOverrides = vnet.SubnetOverrides.Select(s => { s.UseInVmCreationPermission = "Allow"; s.UsePublicIpAddressPermission = "Allow"; return s; }).ToList();
            //update vnet
            var res =await client.VirtualNetworks.CreateOrUpdateAsync("testdevlab", "test", "Dtltest", vnet);


在运行代码之前

运行代码后