[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill-ef2fa25a-bf6f-4f29-80ea-a3796d54bff9":3,"$fK-GmPoBKby5wtWhg2LTQ_qKK13dWReIn7IAMVhksV9I":43},{"id":4,"title":5,"description":6,"categoryId":7,"moduleId":8,"tags":9,"prompt":10,"icon":11,"source":12,"sourceUrl":13,"authorId":14,"authorName":15,"isPublic":16,"stars":17,"runs":18,"createdAt":19,"updatedAt":19,"module":20,"category":27,"packages":34},"ef2fa25a-bf6f-4f29-80ea-a3796d54bff9","azure-eventhub-dotnet","Azure事件中心SDK for .NET","cat_coding_devops","mod_coding","sickn33,coding","---\nname: azure-eventhub-dotnet\ndescription: Azure Event Hubs SDK for .NET.\nrisk: unknown\nsource: community\ndate_added: '2026-02-27'\n---\n\n# Azure.Messaging.EventHubs (.NET)\n\nHigh-throughput event streaming SDK for sending and receiving events via Azure Event Hubs.\n\n## Installation\n\n```bash\n# Core package (sending and simple receiving)\ndotnet add package Azure.Messaging.EventHubs\n\n# Processor package (production receiving with checkpointing)\ndotnet add package Azure.Messaging.EventHubs.Processor\n\n# Authentication\ndotnet add package Azure.Identity\n\n# For checkpointing (required by EventProcessorClient)\ndotnet add package Azure.Storage.Blobs\n```\n\n**Current Versions**: Azure.Messaging.EventHubs v5.12.2, Azure.Messaging.EventHubs.Processor v5.12.2\n\n## Environment Variables\n\n```bash\nEVENTHUB_FULLY_QUALIFIED_NAMESPACE=\u003Cnamespace>.servicebus.windows.net\nEVENTHUB_NAME=\u003Cevent-hub-name>\n\n# For checkpointing (EventProcessorClient)\nBLOB_STORAGE_CONNECTION_STRING=\u003Cstorage-connection-string>\nBLOB_CONTAINER_NAME=\u003Ccheckpoint-container>\n\n# Alternative: Connection string auth (not recommended for production)\nEVENTHUB_CONNECTION_STRING=Endpoint=sb:\u002F\u002F\u003Cnamespace>.servicebus.windows.net\u002F;SharedAccessKeyName=...\n```\n\n## Authentication\n\n```csharp\nusing Azure.Identity;\nusing Azure.Messaging.EventHubs;\nusing Azure.Messaging.EventHubs.Producer;\n\n\u002F\u002F Always use DefaultAzureCredential for production\nvar credential = new DefaultAzureCredential();\n\nvar fullyQualifiedNamespace = Environment.GetEnvironmentVariable(\"EVENTHUB_FULLY_QUALIFIED_NAMESPACE\");\nvar eventHubName = Environment.GetEnvironmentVariable(\"EVENTHUB_NAME\");\n\nvar producer = new EventHubProducerClient(\n    fullyQualifiedNamespace,\n    eventHubName,\n    credential);\n```\n\n**Required RBAC Roles**:\n- **Sending**: `Azure Event Hubs Data Sender`\n- **Receiving**: `Azure Event Hubs Data Receiver`\n- **Both**: `Azure Event Hubs Data Owner`\n\n## Client Types\n\n| Client | Purpose | When to Use |\n|--------|---------|-------------|\n| `EventHubProducerClient` | Send events immediately in batches | Real-time sending, full control over batching |\n| `EventHubBufferedProducerClient` | Automatic batching with background sending | High-volume, fire-and-forget scenarios |\n| `EventHubConsumerClient` | Simple event reading | Prototyping only, NOT for production |\n| `EventProcessorClient` | Production event processing | **Always use this for receiving in production** |\n\n## Core Workflow\n\n### 1. Send Events (Batch)\n\n```csharp\nusing Azure.Identity;\nusing Azure.Messaging.EventHubs;\nusing Azure.Messaging.EventHubs.Producer;\n\nawait using var producer = new EventHubProducerClient(\n    fullyQualifiedNamespace,\n    eventHubName,\n    new DefaultAzureCredential());\n\n\u002F\u002F Create a batch (respects size limits automatically)\nusing EventDataBatch batch = await producer.CreateBatchAsync();\n\n\u002F\u002F Add events to batch\nvar events = new[]\n{\n    new EventData(BinaryData.FromString(\"{\\\"id\\\": 1, \\\"message\\\": \\\"Hello\\\"}\")),\n    new EventData(BinaryData.FromString(\"{\\\"id\\\": 2, \\\"message\\\": \\\"World\\\"}\"))\n};\n\nforeach (var eventData in events)\n{\n    if (!batch.TryAdd(eventData))\n    {\n        \u002F\u002F Batch is full - send it and create a new one\n        await producer.SendAsync(batch);\n        batch = await producer.CreateBatchAsync();\n        \n        if (!batch.TryAdd(eventData))\n        {\n            throw new Exception(\"Event too large for empty batch\");\n        }\n    }\n}\n\n\u002F\u002F Send remaining events\nif (batch.Count > 0)\n{\n    await producer.SendAsync(batch);\n}\n```\n\n### 2. Send Events (Buffered - High Volume)\n\n```csharp\nusing Azure.Messaging.EventHubs.Producer;\n\nvar options = new EventHubBufferedProducerClientOptions\n{\n    MaximumWaitTime = TimeSpan.FromSeconds(1)\n};\n\nawait using var producer = new EventHubBufferedProducerClient(\n    fullyQualifiedNamespace,\n    eventHubName,\n    new DefaultAzureCredential(),\n    options);\n\n\u002F\u002F Handle send success\u002Ffailure\nproducer.SendEventBatchSucceededAsync += args =>\n{\n    Console.WriteLine($\"Batch sent: {args.EventBatch.Count} events\");\n    return Task.CompletedTask;\n};\n\nproducer.SendEventBatchFailedAsync += args =>\n{\n    Console.WriteLine($\"Batch failed: {args.Exception.Message}\");\n    return Task.CompletedTask;\n};\n\n\u002F\u002F Enqueue events (sent automatically in background)\nfor (int i = 0; i \u003C 1000; i++)\n{\n    await producer.EnqueueEventAsync(new EventData($\"Event {i}\"));\n}\n\n\u002F\u002F Flush remaining events before disposing\nawait producer.FlushAsync();\n```\n\n### 3. Receive Events (Production - EventProcessorClient)\n\n```csharp\nusing Azure.Identity;\nusing Azure.Messaging.EventHubs;\nusing Azure.Messaging.EventHubs.Consumer;\nusing Azure.Messaging.EventHubs.Processor;\nusing Azure.Storage.Blobs;\n\n\u002F\u002F Blob container for checkpointing\nvar blobClient = new BlobContainerClient(\n    Environment.GetEnvironmentVariable(\"BLOB_STORAGE_CONNECTION_STRING\"),\n    Environment.GetEnvironmentVariable(\"BLOB_CONTAINER_NAME\"));\n\nawait blobClient.CreateIfNotExistsAsync();\n\n\u002F\u002F Create processor\nvar processor = new EventProcessorClient(\n    blobClient,\n    EventHubConsumerClient.DefaultConsumerGroup,\n    fullyQualifiedNamespace,\n    eventHubName,\n    new DefaultAzureCredential());\n\n\u002F\u002F Handle events\nprocessor.ProcessEventAsync += async args =>\n{\n    Console.WriteLine($\"Partition: {args.Partition.PartitionId}\");\n    Console.WriteLine($\"Data: {args.Data.EventBody}\");\n    \n    \u002F\u002F Checkpoint after processing (or batch checkpoints)\n    await args.UpdateCheckpointAsync();\n};\n\n\u002F\u002F Handle errors\nprocessor.ProcessErrorAsync += args =>\n{\n    Console.WriteLine($\"Error: {args.Exception.Message}\");\n    Console.WriteLine($\"Partition: {args.PartitionId}\");\n    return Task.CompletedTask;\n};\n\n\u002F\u002F Start processing\nawait processor.StartProcessingAsync();\n\n\u002F\u002F Run until cancelled\nawait Task.Delay(Timeout.Infinite, cancellationToken);\n\n\u002F\u002F Stop gracefully\nawait processor.StopProcessingAsync();\n```\n\n### 4. Partition Operations\n\n```csharp\n\u002F\u002F Get partition IDs\nstring[] partitionIds = await producer.GetPartitionIdsAsync();\n\n\u002F\u002F Send to specific partition (use sparingly)\nvar options = new SendEventOptions\n{\n    PartitionId = \"0\"\n};\nawait producer.SendAsync(events, options);\n\n\u002F\u002F Use partition key (recommended for ordering)\nvar batchOptions = new CreateBatchOptions\n{\n    PartitionKey = \"customer-123\"  \u002F\u002F Events with same key go to same partition\n};\nusing var batch = await producer.CreateBatchAsync(batchOptions);\n```\n\n## EventPosition Options\n\nControl where to start reading:\n\n```csharp\n\u002F\u002F Start from beginning\nEventPosition.Earliest\n\n\u002F\u002F Start from end (new events only)\nEventPosition.Latest\n\n\u002F\u002F Start from specific offset\nEventPosition.FromOffset(12345)\n\n\u002F\u002F Start from specific sequence number\nEventPosition.FromSequenceNumber(100)\n\n\u002F\u002F Start from specific time\nEventPosition.FromEnqueuedTime(DateTimeOffset.UtcNow.AddHours(-1))\n```\n\n## ASP.NET Core Integration\n\n```csharp\n\u002F\u002F Program.cs\nusing Azure.Identity;\nusing Azure.Messaging.EventHubs.Producer;\nusing Microsoft.Extensions.Azure;\n\nbuilder.Services.AddAzureClients(clientBuilder =>\n{\n    clientBuilder.AddEventHubProducerClient(\n        builder.Configuration[\"EventHub:FullyQualifiedNamespace\"],\n        builder.Configuration[\"EventHub:Name\"]);\n    \n    clientBuilder.UseCredential(new DefaultAzureCredential());\n});\n\n\u002F\u002F Inject in controller\u002Fservice\npublic class EventService\n{\n    private readonly EventHubProducerClient _producer;\n    \n    public EventService(EventHubProducerClient producer)\n    {\n        _producer = producer;\n    }\n    \n    public async Task SendAsync(string message)\n    {\n        using var batch = await _producer.CreateBatchAsync();\n        batch.TryAdd(new EventData(message));\n        await _producer.SendAsync(batch);\n    }\n}\n```\n\n## Best Practices\n\n1. **Use `EventProcessorClient` for receiving** — Never use `EventHubConsumerClient` in production\n2. **Checkpoint strategically** — After N events or time interval, not every event\n3. **Use partition keys** — For ordering guarantees within a partition\n4. **Reuse clients** — Create once, use as singleton (thread-safe)\n5. **Use `await using`** — Ensures proper disposal\n6. **Handle `ProcessErrorAsync`** — Always register error handler\n7. **Batch events** — Use `CreateBatchAsync()` to respect size limits\n8. **Use buffered producer** — For high-volume scenarios with automatic batching\n\n## Error Handling\n\n```csharp\nusing Azure.Messaging.EventHubs;\n\ntry\n{\n    await producer.SendAsync(batch);\n}\ncatch (EventHubsException ex) when (ex.Reason == EventHubsException.FailureReason.ServiceBusy)\n{\n    \u002F\u002F Retry with backoff\n    await Task.Delay(TimeSpan.FromSeconds(5));\n}\ncatch (EventHubsException ex) when (ex.IsTransient)\n{\n    \u002F\u002F Transient error - safe to retry\n    Console.WriteLine($\"Transient error: {ex.Message}\");\n}\ncatch (EventHubsException ex)\n{\n    \u002F\u002F Non-transient error\n    Console.WriteLine($\"Error: {ex.Reason} - {ex.Message}\");\n}\n```\n\n## Checkpointing Strategies\n\n| Strategy | When to Use |\n|----------|-------------|\n| Every event | Low volume, critical data |\n| Every N events | Balanced throughput\u002Freliability |\n| Time-based | Consistent checkpoint intervals |\n| Batch completion | After processing a logical batch |\n\n```csharp\n\u002F\u002F Checkpoint every 100 events\nprivate int _eventCount = 0;\n\nprocessor.ProcessEventAsync += async args =>\n{\n    \u002F\u002F Process event...\n    \n    _eventCount++;\n    if (_eventCount >= 100)\n    {\n        await args.UpdateCheckpointAsync();\n        _eventCount = 0;\n    }\n};\n```\n\n## Related SDKs\n\n| SDK | Purpose | Install |\n|-----|---------|---------|\n| `Azure.Messaging.EventHubs` | Core sending\u002Freceiving | `dotnet add package Azure.Messaging.EventHubs` |\n| `Azure.Messaging.EventHubs.Processor` | Production processing | `dotnet add package Azure.Messaging.EventHubs.Processor` |\n| `Azure.ResourceManager.EventHubs` | Management plane (create hubs) | `dotnet add package Azure.ResourceManager.EventHubs` |\n| `Microsoft.Azure.WebJobs.Extensions.EventHubs` | Azure Functions binding | `dotnet add package Microsoft.Azure.WebJobs.Extensions.EventHubs` |\n\n## When to Use\nThis skill is applicable to execute the workflow or actions described in the overview.\n\n## Limitations\n- Use this skill only when the task clearly matches the scope described above.\n- Do not treat the output as a substitute for environment-specific validation, testing, or expert review.\n- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.\n","","imported","https:\u002F\u002Fgithub.com\u002Fsickn33\u002Fantigravity-awesome-skills","user_system_seed","SkillOPIC",true,85,852,"2026-05-16 13:06:18",{"id":8,"name":21,"slug":22,"icon":23,"description":24,"sort":25,"createdAt":26},"编程开发","coding","mdi-code-braces","代码生成、调试、审查，提升开发效率",2,"2026-05-16 12:53:40",{"id":7,"name":28,"slug":29,"icon":30,"description":31,"moduleId":8,"sort":32,"skillCount":33,"createdAt":26},"DevOps","devops","mdi-cog-outline","CI\u002FCD、容器化、部署运维",3,162,[35],{"id":36,"skillId":4,"version":37,"fileName":38,"fileSize":39,"filePath":40,"fileHash":41,"manifest":42,"createdAt":19},"5cbc2102-9f19-45ca-940b-f2ded40b8416","1.0.0","azure-eventhub-dotnet.zip",3508,"uploads\u002Fskills\u002Fef2fa25a-bf6f-4f29-80ea-a3796d54bff9\u002Fazure-eventhub-dotnet.zip","6ff980f0d9f294281eea46d5596151d6643e3279d9f48812e690347ec91ac6bf","[{\"path\":\"SKILL.md\",\"isDirectory\":false,\"size\":10409}]",{"code":44,"message":45,"data":46},200,"success",{"items":47,"stats":48,"page":51},[],{"averageRating":49,"totalRatings":49,"ratingCounts":50},0,[49,49,49,49,49],{"limit":52,"offset":49,"hasMore":53,"nextOffset":52,"ratedOnly":16},15,false]