关于 azure-resource-manager-durabletask-dotnet
azure-resource-manager-durabletask-dotnet 是用于通过 Azure Resource Manager 管理 Durable Task Scheduler 资源的 .NET 管理平面 SDK,面向配置 Azure Durable Task 环境的开发者。它涵盖调度器、任务中心和保留策略的资源操作,不负责启动或查询编排实例。
主要功能
- 01创建、更新和删除调度器
- 02管理任务中心
- 03配置保留策略
- 04列出并查询调度器
- 05支持 Dedicated 与 Consumption SKU
适用场景
- 01为 Durable Task 部署创建调度器
- 02调整任务中心和调度器配置
- 03为编排运行配置资源保留策略
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ sickn33/ agentic-awesome-skills --skill azure-resource-manager-durabletask-dotnet安装后在 Claude Code 中输入/azure-resource-manager-durabletask-dotnet确认已加载。
详细介绍
Azure.ResourceManager.DurableTask(.NET)
用于通过 Azure Resource Manager 预配和管理 Azure Durable Task Scheduler 资源的管理平面 SDK。
⚠️ 管理平面与数据平面
- 此 SDK(Azure.ResourceManager.DurableTask):创建调度程序和任务中心,配置保留策略
- 数据平面 SDK(Microsoft.DurableTask.Client.AzureManaged):启动编排、查询实例、发送事件
安装
dotnet add package Azure.ResourceManager.DurableTask
dotnet add package Azure.Identity
这些代码片段针对的是历史上的 1.0 API 形式,并不代表最新版本。固定使用经过审核的程序包版本,并在调整示例前,依据 Microsoft 的 SDK 参考检查其生成的类型和 API 版本。
环境变量
AZURE_SUBSCRIPTION_ID=<your-subscription-id>
AZURE_RESOURCE_GROUP=<your-resource-group>
# For service principal auth (optional)
AZURE_TENANT_ID=<tenant-id>
AZURE_CLIENT_ID=<client-id>
AZURE_CLIENT_SECRET=<client-secret>
身份验证
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.DurableTask;
// Always use DefaultAzureCredential
var credential = new DefaultAzureCredential();
var armClient = new ArmClient(credential);
// Get subscription
var subscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID");
var subscription = armClient.GetSubscriptionResource(
new ResourceIdentifier($"/subscriptions/{subscriptionId}"));
资源层级结构
ArmClient
└── SubscriptionResource
└── ResourceGroupResource
└── DurableTaskSchedulerResource
├── DurableTaskHubResource
└── DurableTaskRetentionPolicyResource
核心工作流
1. 创建 Durable Task Scheduler
using Azure.ResourceManager.DurableTask;
using Azure.ResourceManager.DurableTask.Models;
// Get resource group
var resourceGroup = await subscription
.GetResourceGroupAsync("my-resource-group");
// Define scheduler with Dedicated SKU
var schedulerData = new DurableTaskSchedulerData(AzureLocation.EastUS)
{
Properties = new DurableTaskSchedulerProperties
{
Sku = new DurableTaskSchedulerSku(DurableTaskSchedulerSkuName.Dedicated)
{
Capacity = 1 // Number of instances
},
// Optional: IP allowlist for network security
IPAllowlist = { "10.0.0.0/24", "192.168.1.0/24" }
}
};
// Create scheduler (long-running operation)
var schedulerCollection = resourceGroup.Value.GetDurableTaskSchedulers();
var operation = await schedulerCollection.CreateOrUpdateAsync(
WaitUntil.Completed,
"my-scheduler",
schedulerData);
DurableTaskSchedulerResource scheduler = operation.Value;
Console.WriteLine($"Scheduler created: {scheduler.Data.Name}");
Console.WriteLine($"Endpoint: {scheduler.Data.Properties.Endpoint}");
2. 创建采用 Consumption SKU 的调度程序
// Consumption SKU (serverless)
var consumptionSchedulerData = new DurableTaskSchedulerData(AzureLocation.EastUS)
{
Properties = new DurableTaskSchedulerProperties
{
Sku = new DurableTaskSchedulerSku(DurableTaskSchedulerSkuName.Consumption)
// No capacity needed for consumption
}
};
var operation = await schedulerCollection.CreateOrUpdateAsync(
WaitUntil.Completed,
"my-serverless-scheduler",
consumptionSchedulerData);
3. 创建任务中心
// Task hubs are created under a scheduler
var taskHubData = new DurableTaskHubData
{
// Properties are optional for basic task hub
};
var taskHubCollection = scheduler.GetDurableTaskHubs();
var hubOperation = await taskHubCollection.CreateOrUpdateAsync(
WaitUntil.Completed,
"my-taskhub",
taskHubData);
DurableTaskHubResource taskHub = hubOperation.Value;
Console.WriteLine($"Task Hub created: {taskHub.Data.Name}");
4. 列出调度程序
// List all schedulers in subscription
await foreach (var sched in subscription.GetDurableTaskSchedulersAsync())
{
Console.WriteLine($"Scheduler: {sched.Data.Name}");
Console.WriteLine($" Location: {sched.Data.Location}");
Console.WriteLine($" SKU: {sched.Data.Properties.Sku?.Name}");
Console.WriteLine($" Endpoint: {sched.Data.Properties.Endpoint}");
}
// List schedulers in resource group
var schedulers = resourceGroup.Value.GetDurableTaskSchedulers();
await foreach (var sched in schedulers.GetAllAsync())
{
Console.WriteLine($"Scheduler: {sched.Data.Name}");
}
5. 按名称获取调度程序
// Get existing scheduler
var existingScheduler = await schedulerCollection.GetAsync("my-scheduler");
Console.WriteLine($"Found: {existingScheduler.Value.Data.Name}");
// Or use extension method
var schedulerResource = armClient.GetDurableTaskSchedulerResource(
DurableTaskSchedulerResource.CreateResourceIdentifier(
subscriptionId,
"my-resource-group",
"my-scheduler"));
var scheduler = await schedulerResource.GetAsync();
6. 更新调度程序
// Get current scheduler
var scheduler = await schedulerCollection.GetAsync("my-scheduler");
// Update with new configuration
var updateData = new DurableTaskSchedulerData(scheduler.Value.Data.Location)
{
Properties = new DurableTaskSchedulerProperties
{
Sku = new DurableTaskSchedulerSku(DurableTaskSchedulerSkuName.Dedicated)
{
Capacity = 2 // Scale up
},
IPAllowlist = { "10.0.0.0/16" } // Update IP allowlist
}
};
var updateOperation = await schedulerCollection.CreateOrUpdateAsync(
WaitUntil.Completed,
"my-scheduler",
updateData);
7. 删除资源
// Delete task hub first
var taskHub = await scheduler.GetDurableTaskHubs().GetAsync("my-taskhub");
await taskHub.Value.DeleteAsync(WaitUntil.Completed);
// Then delete scheduler
await scheduler.DeleteAsync(WaitUntil.Completed);
8. 管理保留策略
// Get retention policy collection
var retentionPolicies = scheduler.GetDurableTaskRetentionPolicies();
// Create or update retention policy
var retentionData = new DurableTaskRetentionPolicyData
{
Properties = new DurableTaskRetentionPolicyProperties
{
// Configure retention settings
}
};
var retentionOperation = await retentionPolicies.CreateOrUpdateAsync(
WaitUntil.Completed,
"default", // Policy name
retentionData);
关键类型参考
| 类型 | 用途 |
|---|---|
ArmClient | 所有 ARM 操作的入口点 |
DurableTaskSchedulerResource | 表示 Durable Task Scheduler |
DurableTaskSchedulerCollection | 用于调度程序 CRUD 操作的集合 |
DurableTaskSchedulerData | 调度程序创建/更新负载 |
DurableTaskSchedulerProperties | 调度程序配置(SKU、IPAllowlist) |
DurableTaskSchedulerSku | SKU 配置(Name、Capacity、RedundancyState) |
DurableTaskSchedulerSkuName | SKU 选项:Dedicated、Consumption |
DurableTaskHubResource | 表示任务中心 |
DurableTaskHubCollection | 用于任务中心 CRUD 操作的集合 |
DurableTaskHubData | 任务中心创建负载 |
DurableTaskRetentionPolicyResource | 保留策略管理 |
DurableTaskRetentionPolicyData | 保留策略配置 |
DurableTaskExtensions | ARM 客户端的扩展方法 |
SKU 选项
| SKU | 说明 | 使用场景 |
|---|---|---|
Dedicated | 固定容量,可配置实例 | 生产工作负载、性能可预测 |
Consumption | 无服务器、自动缩放 | 开发、工作负载变化较大的场景 |
扩展方法
SDK 为 SubscriptionResource 和 ResourceGroupResource 提供扩展方法:
// On SubscriptionResource
subscription.GetDurableTaskSchedulers(); // List all in subscription
subscription.GetDurableTaskSchedulersAsync(); // Async enumerable
// On ResourceGroupResource
resourceGroup.GetDurableTaskSchedulers(); // Get collection
resourceGroup.GetDurableTaskSchedulerAsync(name); // Get by name
// On ArmClient
armClient.GetDurableTaskSchedulerResource(id); // Get by resource ID
armClient.GetDurableTaskHubResource(id); // Get task hub by ID
最佳实践
- 对必须完成后才能继续的操作使用
WaitUntil.Completed - 需要手动轮询或并行运行操作时,使用
WaitUntil.Started - 始终使用
DefaultAzureCredential——切勿将密钥硬编码 - 处理 ARM API 错误时使用
RequestFailedException - 使用
CreateOrUpdateAsync执行幂等操作 - 先删除任务中心,再删除调度程序——包含任务中心的调度程序无法删除
- 在生产环境中使用 IP 允许列表来保障网络安全
错误处理
using Azure;
try
{
var operation = await schedulerCollection.CreateOrUpdateAsync(
WaitUntil.Completed, schedulerName, schedulerData);
}
catch (RequestFailedException ex) when (ex.Status == 409)
{
Console.WriteLine($"Conflict: {ex.ErrorCode}; inspect the actual resource/operation state.");
}
catch (RequestFailedException ex) when (ex.Status == 404)
{
Console.WriteLine($"Not found: {ex.ErrorCode}; inspect the target resource ID.");
}
catch (RequestFailedException ex)
{
Console.WriteLine($"ARM Error: {ex.Status} - {ex.ErrorCode}: {ex.Message}");
}
完整示例
using Azure;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.DurableTask;
using Azure.ResourceManager.DurableTask.Models;
using Azure.ResourceManager.Resources;
// Setup
var credential = new DefaultAzureCredential();
var armClient = new ArmClient(credential);
var subscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID")!;
var resourceGroupName = Environment.GetEnvironmentVariable("AZURE_RESOURCE_GROUP")!;
var subscription = armClient.GetSubscriptionResource(
new ResourceIdentifier($"/subscriptions/{subscriptionId}"));
var resourceGroup = await subscription.GetResourceGroupAsync(resourceGroupName);
// Create scheduler
var schedulerData = new DurableTaskSchedulerData(AzureLocation.EastUS)
{
Properties = new DurableTaskSchedulerProperties
{
Sku = new DurableTaskSchedulerSku(DurableTaskSchedulerSkuName.Dedicated)
{
Capacity = 1
}
}
};
var schedulerCollection = resourceGroup.Value.GetDurableTaskSchedulers();
var schedulerOp = await schedulerCollection.CreateOrUpdateAsync(
WaitUntil.Completed, "my-scheduler", schedulerData);
var scheduler = schedulerOp.Value;
Console.WriteLine($"Scheduler endpoint: {scheduler.Data.Properties.Endpoint}");
// Create task hub
var taskHubData = new DurableTaskHubData();
var taskHubOp = await scheduler.GetDurableTaskHubs().CreateOrUpdateAsync(
WaitUntil.Completed, "my-taskhub", taskHubData);
var taskHub = taskHubOp.Value;
Console.WriteLine($"Task Hub: {taskHub.Data.Name}");
// Optional cleanup is intentionally separate: delete only resources created by
// this task, after verifying their IDs and obtaining the required authorization.
相关 SDK
| SDK | 用途 | 安装 |
|---|---|---|
Azure.ResourceManager.DurableTask | 管理平面(此 SDK) | dotnet add package Azure.ResourceManager.DurableTask |
Microsoft.DurableTask.Client.AzureManaged | 数据平面(编排、活动) | dotnet add package Microsoft.DurableTask.Client.AzureManaged |
Microsoft.DurableTask.Worker.AzureManaged | 用于运行编排的工作进程 | dotnet add package Microsoft.DurableTask.Worker.AzureManaged |
Azure.Identity | 身份验证 | dotnet add package Azure.Identity |
Azure.ResourceManager | 基础 ARM SDK | dotnet add package Azure.ResourceManager |
源参考
适用场景
当需要执行概述中描述的工作流或操作时,此技能适用。
限制
- 仅当任务明确符合上述范围时,才使用此技能。
- 不要将输出视为环境特定验证、测试或专家审查的替代品。
- 如果缺少必要的输入、权限、安全边界或成功标准,请停止并要求澄清。
评论
支持 Markdown