【发布时间】:2023-03-02 22:21:01
【问题描述】:
我正在尝试获取虚拟机的主 IP,但出现错误 'virtualMachine.GetPrimaryPublicIPAddress().IPAddress' threw an exception of type 'System.NullReferenceException'
我在 Postman 中使用 Rest-API 成功获取了 IP 例如 Postman GET 方法
https://management.azure.com/subscriptions/{{subid}}/resourceGroups/{{rg}}/providers/Microsoft.Network/networkInterfaces/{{vm_name}}?api-version=2020-11-01
我在 .Net 中的代码
var credentials = SdkContext.AzureCredentialsFactory
.FromServicePrincipal(clientId,
clientSecret,
tenantId,
AzureEnvironment.AzureGlobalCloud);
var azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithSubscription(subscriptionId);
foreach (var virtualMachine in azure.VirtualMachines.ListByResourceGroup(resourceGroupName))
{
var name = virtualMachine.Name;
var os_type = virtualMachine.OSType;
var size = virtualMachine.OSDiskSize;
var ip = virtualMachine.GetPrimaryPublicIPAddress().IPAddress; //Error
}
感谢您的帮助
【问题讨论】:
-
你解决了吗?您介意接受我的回答供其他人参考吗?
标签: c# azure azure-virtual-machine