提问人:SilentUK 提问时间:10/18/2023 最后编辑:SilentUK 更新时间:10/18/2023 访问量:115
以编程方式使用 C# 横向扩展 Azure 应用服务 Web 应用
Scale Out Azure App Service Web App with C# programmatically
问:
我们有一个 Azure 应用服务 Web 应用,我们希望根据特定条件进行横向扩展。我已经看到,使用基于2017年提出的这个问题的“Microsoft.Azure.Management.Websites”包是可能的,但这些包现在已被弃用。现在可以在 .NET 中执行此操作吗?
答:
1赞
Vivek Vaibhav Shandilya
10/18/2023
#1
我能够使用 GitHub Code 使用新包横向扩展我的应用服务计划。Azure.ResourceManager.AppService
注意:- 此代码使用 .NET 7.0 或更高版本
以下代码对我有用:
程序.cs
:
using System.Threading.Tasks;
using Azure.ResourceManager;
using Azure.ResourceManager.Models;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager.AppService;
using Azure.ResourceManager.AppService.Models;
using Azure.Identity;
using Azure.Core;
using Azure;
namespace ConsoleApp1
{
class Program
{
public static async Task Main()
{
TokenCredential credential = new DefaultAzureCredential();
ArmClient client = new ArmClient(credential);
string subscriptionId = "xxxxxxxxx-xxxx-xxxx-xxxxxxxx";
string ResourceGroupName = "resourcegroupename";
ResourceIdentifier resourceGroups = ResourceGroupResource.CreateResourceIdentifier(subscriptionId, ResourceGroupName);
ResourceGroupResource resourceGroup = client.GetResourceGroupResource(resourceGroups);
AppServicePlanCollection collection = resourceGroup.GetAppServicePlans();
string name = "asp-vivekchatgpt";
AppServicePlanData update_data = new AppServicePlanData(AzureLocation.EastUS)
{
Sku = new AppServiceSkuDescription()
{
Name = "P2V3",
Tier = "Premium v3",
Capacity = 2,
},
Kind = "app",
};
ArmOperation<AppServicePlanResource> operation = await collection.CreateOrUpdateAsync(WaitUntil.Completed, name, update_data);
AppServicePlanResource result = operation.Value;
AppServicePlanData resourceData = result.Data;
Console.WriteLine($"Succeeded on id: {resourceData.Id}");
}
}
}
ConsoleApp1.csproj
:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.10.2" />
<PackageReference Include="Azure.ResourceManager" Version="1.7.0" />
<PackageReference Include="Azure.ResourceManager.AppService" Version="1.0.2" />
</ItemGroup>
</Project>
输出
:
初始应用服务计划
执行代码后:
横向扩展应用服务计划
评论
1赞
SilentUK
10/18/2023
这很完美,谢谢。
0赞
Vivek Vaibhav Shandilya
10/18/2023
我很高兴它有帮助。:)
评论