
Azure Event Grid SDK for .NET。用于发布和使用Azure Event Grid事件的客户端库。用于事件驱动架构、发布/订阅消息传递、CloudEvents和EventGridEvents。技能概述azure-eventgrid-dotnet 技能提供了Azure Event Grid SDK for .NET的完整使用指南。该技能帮助开发者构建事件驱动应用程序支持将事件发布到Event Grid主题、域和命名空间以及使用CloudEvents和EventGridEvents模式进行事件处理。下载地址agentic-awesome-skills/skills/azure-eventgrid-dotnet at main · sickn33/agentic-awesome-skills · GitHub主要功能事件发布: 将事件发布到Event Grid主题和域CloudEvents支持: 使用CNCF标准的CloudEvents 1.0模式EventGridEvents支持: 使用Azure原生事件模式拉取传递: 从命名空间订阅接收事件系统事件处理: 处理Azure服务的系统事件Azure Functions集成: 与Azure Functions无缝集成触发条件在以下情况下应该调用此技能:需要构建事件驱动架构实现发布/订阅消息传递使用CloudEvents或EventGridEvents集成Azure服务事件需要事件网格的拉取传递模式安装方法# 主题和域推送传递dotnet add package Azure.Messaging.EventGrid# 命名空间拉取传递dotnet add package Azure.Messaging.EventGrid.Namespaces# CloudNative CloudEvents互操作dotnet add package Microsoft.Azure.Messaging.EventGrid.CloudNativeCloudEvents客户端层次结构推送传递主题/域EventGridPublisherClient → SendEventAsync, SendEventsAsync拉取传递命名空间EventGridSenderClient → SendAsyncEventGridReceiverClient → ReceiveAsync, AcknowledgeAsync, ReleaseAsync, RejectAsync使用示例示例1: 创建客户端using Azure;using Azure.Messaging.EventGrid;// API密钥身份验证EventGridPublisherClient client new(new Uri(https://mytopic.eastus-1.eventgrid.azure.net/api/events),new AzureKeyCredential(access-key));// Microsoft Entra ID推荐using Azure.Identity;EventGridPublisherClient client new(new Uri(https://mytopic.eastus-1.eventgrid.azure.net/api/events),new DefaultAzureCredential());示例2: 发布EventGridEvents// 单个事件EventGridEvent egEvent new(subject: orders/12345,eventType: Order.Created,dataVersion: 1.0,data: new { OrderId 12345, Amount 99.99 });await client.SendEventAsync(egEvent);// 批量事件ListEventGridEvent events new(){new EventGridEvent(subject: orders/12345,eventType: Order.Created,dataVersion: 1.0,data: new OrderData { OrderId 12345, Amount 99.99 }),new EventGridEvent(subject: orders/12346,eventType: Order.Created,dataVersion: 1.0,data: new OrderData { OrderId 12346, Amount 149.99 })};await client.SendEventsAsync(events);示例3: 发布CloudEventsCloudEvent cloudEvent new(source: /orders/system,type: Order.Created,data: new { OrderId 12345, Amount 99.99 });cloudEvent.Subject orders/12345;cloudEvent.Id Guid.NewGuid().ToString();cloudEvent.Time DateTimeOffset.UtcNow;await client.SendEventAsync(cloudEvent);// 批量CloudEventsListCloudEvent cloudEvents new(){new CloudEvent(/orders, Order.Created, new { OrderId 1 }),new CloudEvent(/orders, Order.Updated, new { OrderId 2 })};await client.SendEventsAsync(cloudEvents);示例4: 拉取传递命名空间using Azure.Messaging.EventGrid.Namespaces;var receiverClient new EventGridReceiverClient(new Uri(namespaceEndpoint),topicName,subscriptionName,new AzureKeyCredential(topicKey));// 接收事件ReceiveResult result await receiverClient.ReceiveAsync(maxEvents: 10);Liststring lockTokensToAck new();foreach (ReceiveDetails detail in result.Details){CloudEvent cloudEvent detail.Event;string lockToken detail.BrokerProperties.LockToken;try{// 处理事件Console.WriteLine($Event: {cloudEvent.Type});lockTokensToAck.Add(lockToken);}catch (Exception){// 释放以重试await receiverClient.ReleaseAsync(new[] { lockToken });}}// 确认已处理的事件if (lockTokensToAck.Any()){await receiverClient.AcknowledgeAsync(lockTokensToAck);}示例5: Azure Functions触发器using Azure.Messaging.EventGrid;using Microsoft.Azure.WebJobs;using Microsoft.Azure.WebJobs.Extensions.EventGrid;public static class EventGridFunction{[FunctionName(ProcessEventGridEvent)]public static void Run([EventGridTrigger] EventGridEvent eventGridEvent,ILogger log){log.LogInformation($Event Type: {eventGridEvent.EventType});log.LogInformation($Subject: {eventGridEvent.Subject});log.LogInformation($Data: {eventGridEvent.Data});}}事件模式比较特性EventGridEventCloudEvent标准Azure特定CNCF标准必需字段subject, eventType, dataVersion, datasource, type可扩展性有限扩展属性互操作性仅Azure跨平台最佳实践使用CloudEvents: 新实现优先使用CloudEvents行业标准批量事件: 一次调用发送多个事件以提高效率使用Entra ID: 优先使用托管标识而非访问密钥幂等处理程序: 事件可能会多次传递设置事件TTL: 为命名空间事件配置生存时间处理部分失败: 单独确认/释放事件使用死信: 为失败事件配置死信验证模式: 处理前验证事件数据环境变量EVENT_GRID_TOPIC_ENDPOINThttps://topic-name.region.eventgrid.azure.net/api/eventsEVENT_GRID_TOPIC_KEYaccess-keyEVENT_GRID_NAMESPACE_ENDPOINThttps://namespace.region.eventgrid.azure.net相关资源NuGet包: NuGet Gallery | Azure.Messaging.EventGrid 5.0.0API参考: Azure.Messaging.EventGrid Namespace - Azure for .NET Developers | Microsoft Learn快速入门: https://learn.microsoft.com/azure/event-grid/custom-event-quickstart拉取传递: Azure Event Grid Pull Delivery Overview - Azure Event Grid | Microsoft LearnGitHub源码: azure-sdk-for-net/sdk/eventgrid/Azure.Messaging.EventGrid at main · Azure/azure-sdk-for-net · GitHub限制说明仅当任务明确符合上述范围时使用此技能不要将输出视为环境特定验证、测试或专家审查的替代品如果缺少所需的输入、权限、安全边界或成功标准请停止并寻求澄清