Load balancer creation/management sample code?
See original GitHub issueLibrary name and version
Azure.ResourceManager.Network 1.0.0-beta.7
Query/Question
I’ve been struggling with creating a load balancer using the Azure.ResourceManager.Network 1.0.0-beta.7 SDK and haven’t found any SDK specific sample code or examples.
Question 1: Is there any sample code for managing load balancers with the .NET Azure ARM SDK?
I’ve done a lot of Googling and haven’t found anything but samples for the Azure CLI, PowerShell, and older SDKs. I did find a couple samples here but these didn’t cover load balancers.
Question 2: What’s the process for adding a NIC to a backend address pool?
I’ve tried the pseudo code below but I’m unable to initialize a LoadBalancerBackendAddress
because setting the NetworkInterfaceIPConfigurationId
property throws a NullReferenceException
. This happens because the default LoadBalancerBackendAddress()
constructor doesn’t initialize the internal NetworkInterfaceIPConfiguration
property.
var azure = new ArmClient(new DefaultAzureCredential());
var location = new AzureLocation("centralus");
var resourceGroupCollection = azure.GetResourceGroupCollection();
var resourceGroup = (await resourceGroupCollection.GetAsync("my-resources")).Value;
var loadBalancerCollection = resourceGroup.GetLoadBalancers();
var publicAddress = /* set to an existing public IP address resource */
var nic = /* set to the primary (and only) NIC resource for a deployed VM */
// Create a load balancer with a single frontend and and an empty backend.
var loadBalancerData =
new LoadBalancerData()
{
Location = location,
Sku = new LoadBalancerSku() { Name = "Standard", Tier = "Regional" }
};
loadBalancerData.FrontendIPConfigurations.Add(
new FrontendIPConfigurationData()
{
Name = "my-frontend",
PublicIPAddress = publicAddress.Data,
});
loadBalancerData.BackendAddressPools.Clear();
loadBalancerData.BackendAddressPools.Add(
new BackendAddressPoolData()
{
Name = "my-backend"
});
loadBalancer = (await loadBalancerCollection.CreateOrUpdateAsync(WaitUntil.Completed, "my-loadbalancer", WithNetworkTags(loadBalancerData))).Value;
// Add a NIC to the backend and update the load balancer.
var nicIpConfigId = nic.GetNetworkInterfaceIPConfigurations().First().Id;
loadBalancer.Data.BackendAddressPools.First().Add(
new LoadBalancerBackendAddress()
{
Name = "my-address",
NetworkInterfaceIPConfigurationId = nicIpConfigId // <--- Throws a NullReferenceException because the R/O backing property is NULL
});
loadBalancer = (await loadBalancerCollection.CreateOrUpdate(WaitUntil.Completed, loadBalancer.Name, loadBalancer.Data)).Value;
For fun, I tried using reflection to initialize the internal NetworkInterfaceIPConfiguration
property’s backing property to a new WritableSubResource
. This fixed the nullref exception, but the load balancer’s backend address pool was still empty after the update.
Environment
.NET SDK (reflecting any global.json): Version: 6.0.300 Commit: 8473146e7d
Runtime Environment: OS Name: Windows OS Version: 10.0.19044 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\6.0.300\
Host (useful for support): Version: 6.0.5 Commit: 70ae3df4a6
.NET SDKs installed: 3.1.419 [C:\Program Files\dotnet\sdk] 5.0.407 [C:\Program Files\dotnet\sdk] 5.0.408 [C:\Program Files\dotnet\sdk] 6.0.101 [C:\Program Files\dotnet\sdk] 6.0.105 [C:\Program Files\dotnet\sdk] 6.0.300 [C:\Program Files\dotnet\sdk]
.NET runtimes installed: Microsoft.AspNetCore.App 3.1.24 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 3.1.24 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.25 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.24 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.25 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.16 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
I figured this out: you need to add a NIC to a backend address pool from the NIC side, something like:
What threw me here, was that the
BackendAddressPoolData.LoadBalancerBackendAddresses
property is a read/writeIList
instead of aIReadOnlyList
. This led me to believe that I could manage the NIC references here. I suppose this is R/W so that developers can add static IP addresses rather then adding NICs.So, I guess this issue has turned into a request for load balancer sample code.
@jefflill thank you for reaching us, and provide this useful code, we will have a sampe for this scenario. @fengzhou-msft will follow up.