azure-eventhub-dotnet
Azure Event Hubs SDK for .NET. Use for high-throughput event streaming: sending events (EventHubProducerClient, EventHubBufferedProducerClient), receiving events (EventProcessorClient with checkpointing), partition management, and real-time data ingestion. Triggers: "Event Hubs", "event streaming", "EventHubProducerClient", "EventProcessorClient", "send events", "receive events", "checkpointing", "partition".
- License 许可
- MIT
- 所属仓库
- microsoft/skills
- 最近更新
- 2026-11-14
- Stars
- ★ 3.2k
- Forks
- 368
01 前言 Front Matter
- name
- azure-eventhub-dotnet
- license
- MIT
- metadata
- { "author": "Microsoft", "package": "Azure.Messaging.EventHubs", "version": "1.0.0" }
- description
- Azure Event Hubs SDK for .NET. Use for high-throughput event streaming: sending events (EventHubProducerClient, EventHubBufferedProducerClient), receiving events (EventProcessorClient with checkpointing), partition management, and real-time data ingestion. Triggers: "Event Hubs", "event streaming", "EventHubProducerClient", "EventProcessorClient", "send events", "receive events", "checkpointing", "partition".
02 资源文件 110
- 参考文档copilot-studio/agent.sync.yaml174 B
- 参考文档copilot-studio/behaviors/aibast_account-intelligence-account-overview.mcs.yml2.0 KB
- 参考文档copilot-studio/behaviors/aibast_account-intelligence-competitive-intel.mcs.yml2.0 KB
- 参考文档copilot-studio/behaviors/aibast_account-intelligence-executive-briefing.mcs.yml2.1 KB
- 参考文档copilot-studio/behaviors/aibast_account-intelligence-risk-assessment.mcs.yml2.0 KB
- 参考文档copilot-studio/behaviors/aibast_account-intelligence-stakeholder-map.mcs.yml2.0 KB
- 参考文档copilot-studio/behaviors/aibast_account-intelligence-value-messaging.mcs.yml2.0 KB
- 参考文档copilot-studio/capabilities/knowledge/files/aibast_account-intelligence-operating-rules.md7.0 KB
- 参考文档copilot-studio/capabilities/knowledge/files/aibast_account-intelligence-operating-rules.md.mcs.yml196 B
- 参考文档copilot-studio/capabilities/knowledge/files/aibast_account-intelligence-synthetic-records.md6.7 KB
- 参考文档copilot-studio/capabilities/knowledge/files/aibast_account-intelligence-synthetic-records.md.mcs.yml200 B
- 参考文档copilot-studio/settings.mcs.yml4.6 KB
- 参考文档deployment.json4.3 KB
- 参考文档EASY-MODE-COPILOT-CHAT.md231 B
- 参考文档EASY-MODE-PERSONLESS.md231 B
- 参考文档EASY-MODE.md2.7 KB
- 参考文档evals/copilot-studio-preview-evidence.json2.6 KB
- 参考文档evals/dataverse-draft-evidence.json1.7 KB
- 参考文档evals/demo-cases.json5.3 KB
- 参考文档evals/manual-build-evidence.json4.4 KB
- 参考文档evals/onepager-map.json1.8 KB
- 参考文档evals/transcripts.json16.1 KB
- 参考文档evals/visual-checkpoints.json31.4 KB
- 参考文档evidence-report.html32.5 KB
- 参考文档export-manifest.json54.7 KB
- 资产文件exports/account-intelligence-copilot-studio-solution.zip47.7 KB
- 参考文档exports/account-intelligence-deployment-settings.json215 B
- 参考文档exports/account-intelligence-solution-export.json1.1 KB
- 参考文档exports/README.md1.5 KB
- 参考文档field-guide.html38.2 KB
- 参考文档FIELD-GUIDE.md14.0 KB
- 参考文档FLOW.md1.5 KB
- 参考文档manual-tutorial.html92.7 KB
- 参考文档manual/GLOBAL-INSTRUCTIONS.md3.6 KB
- 参考文档manual/knowledge/aibast_account-intelligence-operating-rules.md7.0 KB
- 参考文档manual/knowledge/aibast_account-intelligence-synthetic-records.md6.7 KB
- 参考文档quest.html147.6 KB
- 参考文档README.md5.2 KB
- 资产文件screenshots/assisted/01-review-build.jpg119.5 KB
- 资产文件screenshots/assisted/02-ai-01.jpg182.1 KB
- 资产文件screenshots/assisted/03-ai-02.jpg223.5 KB
- 资产文件screenshots/assisted/04-ai-03.jpg245.0 KB
- 资产文件screenshots/assisted/05-ai-04.jpg233.0 KB
- 资产文件screenshots/assisted/06-ai-05.jpg219.4 KB
- 资产文件screenshots/assisted/07-ai-06.jpg269.4 KB
- 资产文件screenshots/assisted/08-confirm-draft.jpg28.3 KB
- 资产文件screenshots/assisted/annotated/01-review-build.png117.0 KB
- 资产文件screenshots/assisted/annotated/02-ai-01.png174.3 KB
- 资产文件screenshots/assisted/annotated/03-ai-02.png224.1 KB
- 资产文件screenshots/assisted/annotated/04-ai-03.png234.4 KB
- 资产文件screenshots/assisted/annotated/05-ai-04.png231.6 KB
- 资产文件screenshots/assisted/annotated/06-ai-05.png221.6 KB
- 资产文件screenshots/assisted/annotated/07-ai-06.png259.9 KB
- 资产文件screenshots/assisted/annotated/08-confirm-draft.png28.9 KB
- 参考文档screenshots/assisted/browserfilm.json1.6 KB
- 资产文件screenshots/assisted/copilot-assisted-contact-sheet.jpg201.5 KB
- 资产文件screenshots/assisted/copilot-assisted-walkthrough.gif1.2 MB
- 参考文档screenshots/assisted/README.md467 B
- 资产文件screenshots/manual/01-create-blank-agent.jpg57.7 KB
- 资产文件screenshots/manual/02-name-agent.jpg59.6 KB
- 资产文件screenshots/manual/03-enter-instructions.jpg119.5 KB
- 资产文件screenshots/manual/04-save-instructions.jpg120.9 KB
- 资产文件screenshots/manual/05-remove-web-search.jpg119.6 KB
- 资产文件screenshots/manual/06-add-aibast-account-intelligence-operating-rules.jpg121.3 KB
- 资产文件screenshots/manual/07-add-aibast-account-intelligence-synthetic-records.jpg124.3 KB
- 资产文件screenshots/manual/08-add-account-overview.jpg128.7 KB
- 资产文件screenshots/manual/09-add-competitive-intel.jpg131.4 KB
- 资产文件screenshots/manual/10-add-executive-briefing.jpg134.0 KB
- 资产文件screenshots/manual/11-add-risk-assessment.jpg136.7 KB
- 资产文件screenshots/manual/12-add-stakeholder-map.jpg139.3 KB
- 资产文件screenshots/manual/13-add-value-messaging.jpg139.6 KB
- 资产文件screenshots/manual/14-review-inventory.jpg139.6 KB
- 资产文件screenshots/manual/15-review-skills.jpg127.6 KB
- 资产文件screenshots/manual/16-review-skills-more.jpg130.9 KB
- 资产文件screenshots/manual/17-open-preview.jpg32.4 KB
- 资产文件screenshots/manual/18-ai-01.jpg195.7 KB
- 资产文件screenshots/manual/19-ai-02.jpg214.5 KB
- 资产文件screenshots/manual/20-ai-03.jpg234.7 KB
- 资产文件screenshots/manual/21-ai-04.jpg200.1 KB
- 资产文件screenshots/manual/22-ai-05.jpg208.8 KB
- 资产文件screenshots/manual/23-ai-06.jpg210.0 KB
- 资产文件screenshots/manual/24-confirm-draft.jpg28.7 KB
- 资产文件screenshots/manual/annotated/01-create-blank-agent.png62.7 KB
- 资产文件screenshots/manual/annotated/02-name-agent.png63.8 KB
- 资产文件screenshots/manual/annotated/03-enter-instructions.png120.0 KB
- 资产文件screenshots/manual/annotated/04-save-instructions.png118.9 KB
- 资产文件screenshots/manual/annotated/05-remove-web-search.png120.9 KB
- 资产文件screenshots/manual/annotated/06-add-aibast-account-intelligence-operating-rules.png122.7 KB
- 资产文件screenshots/manual/annotated/07-add-aibast-account-intelligence-synthetic-records.png125.2 KB
- 资产文件screenshots/manual/annotated/08-add-account-overview.png127.6 KB
- 资产文件screenshots/manual/annotated/10-add-executive-briefing.png132.2 KB
- 资产文件screenshots/manual/annotated/11-add-risk-assessment.png134.2 KB
- 资产文件screenshots/manual/annotated/12-add-stakeholder-map.png136.4 KB
- 资产文件screenshots/manual/annotated/13-add-value-messaging.png136.8 KB
- 资产文件screenshots/manual/annotated/14-review-inventory.png142.6 KB
- 资产文件screenshots/manual/annotated/15-review-skills.png142.0 KB
- 资产文件screenshots/manual/annotated/16-review-skills-more.png145.5 KB
- 资产文件screenshots/manual/annotated/17-open-preview.png31.9 KB
- 资产文件screenshots/manual/annotated/18-ai-01.png198.8 KB
- 资产文件screenshots/manual/annotated/19-ai-02.png216.6 KB
- 资产文件screenshots/manual/annotated/20-ai-03.png238.5 KB
- 资产文件screenshots/manual/annotated/21-ai-04.png209.3 KB
- 资产文件screenshots/manual/annotated/22-ai-05.png216.9 KB
- 资产文件screenshots/manual/annotated/23-ai-06.png211.0 KB
- 资产文件screenshots/manual/annotated/24-confirm-draft.png29.4 KB
- 参考文档screenshots/manual/browserfilm.json4.1 KB
- 资产文件screenshots/manual/manual-build-contact-sheet.jpg798.0 KB
- 资产文件screenshots/manual/manual-build-walkthrough.gif5.2 MB
- 参考文档screenshots/manual/README.md461 B
- 参考文档screenshots/README.md521 B
03 SKILL.md 正文
Azure.Messaging.EventHubs (.NET)
High-throughput event streaming SDK for sending and receiving events via Azure Event Hubs.
Installation
# Core package (sending and simple receiving)
dotnet add package Azure.Messaging.EventHubs
# Processor package (production receiving with checkpointing)
dotnet add package Azure.Messaging.EventHubs.Processor
# Authentication
dotnet add package Azure.Identity
# For checkpointing (required by EventProcessorClient)
dotnet add package Azure.Storage.Blobs
Current Versions: Azure.Messaging.EventHubs v5.12.2, Azure.Messaging.EventHubs.Processor v5.12.2
Environment Variables
EVENTHUB_FULLY_QUALIFIED_NAMESPACE=<namespace>.servicebus.windows.net # Required: Event Hubs fully qualified namespace
EVENTHUB_NAME=<event-hub-name> # Required: Event Hub name
BLOB_STORAGE_CONNECTION_STRING=<storage-connection-string> # Alternative to Entra ID auth
BLOB_CONTAINER_NAME=<checkpoint-container> # Required: checkpoint container name
EVENTHUB_CONNECTION_STRING=Endpoint=sb://<namespace>.servicebus.windows.net/;SharedAccessKeyName=... # Alternative to Entra ID auth
AZURE_TOKEN_CREDENTIALS=prod # Required only if DefaultAzureCredential is used in production
Authentication
using Azure.Identity;
using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Producer;
// Local dev: DefaultAzureCredential. Production: set AZURE_TOKEN_CREDENTIALS=prod or AZURE_TOKEN_CREDENTIALS=<specific_credential>
var credential = new DefaultAzureCredential(
DefaultAzureCredential.DefaultEnvironmentVariableName
);
// Or use a specific credential directly in production:
// See https://learn.microsoft.com/dotnet/api/overview/azure/identity-readme?view=azure-dotnet#credential-classes
// var credential = new ManagedIdentityCredential();
var fullyQualifiedNamespace = Environment.GetEnvironmentVariable("EVENTHUB_FULLY_QUALIFIED_NAMESPACE");
var eventHubName = Environment.GetEnvironmentVariable("EVENTHUB_NAME");
var producer = new EventHubProducerClient(
fullyQualifiedNamespace,
eventHubName,
credential);
Required RBAC Roles:
- Sending:
Azure Event Hubs Data Sender - Receiving:
Azure Event Hubs Data Receiver - Both:
Azure Event Hubs Data Owner
Client Types
| Client | Purpose | When to Use |
|---|---|---|
EventHubProducerClient |
Send events immediately in batches | Real-time sending, full control over batching |
EventHubBufferedProducerClient |
Automatic batching with background sending | High-volume, fire-and-forget scenarios |
EventHubConsumerClient |
Simple event reading | Prototyping only, NOT for production |
EventProcessorClient |
Production event processing | Always use this for receiving in production |
Core Workflow
1. Send Events (Batch)
using Azure.Identity;
using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Producer;
await using var producer = new EventHubProducerClient(
fullyQualifiedNamespace,
eventHubName,
new DefaultAzureCredential());
// Create a batch (respects size limits automatically)
using EventDataBatch batch = await producer.CreateBatchAsync();
// Add events to batch
var events = new[]
{
new EventData(BinaryData.FromString("{\"id\": 1, \"message\": \"Hello\"}")),
new EventData(BinaryData.FromString("{\"id\": 2, \"message\": \"World\"}"))
};
foreach (var eventData in events)
{
if (!batch.TryAdd(eventData))
{
// Batch is full - send it and create a new one
await producer.SendAsync(batch);
batch = await producer.CreateBatchAsync();
if (!batch.TryAdd(eventData))
{
throw new Exception("Event too large for empty batch");
}
}
}
// Send remaining events
if (batch.Count > 0)
{
await producer.SendAsync(batch);
}
2. Send Events (Buffered - High Volume)
using Azure.Messaging.EventHubs.Producer;
var options = new EventHubBufferedProducerClientOptions
{
MaximumWaitTime = TimeSpan.FromSeconds(1)
};
await using var producer = new EventHubBufferedProducerClient(
fullyQualifiedNamespace,
eventHubName,
new DefaultAzureCredential(),
options);
// Handle send success/failure
producer.SendEventBatchSucceededAsync += args =>
{
Console.WriteLine($"Batch sent: {args.EventBatch.Count} events");
return Task.CompletedTask;
};
producer.SendEventBatchFailedAsync += args =>
{
Console.WriteLine($"Batch failed: {args.Exception.Message}");
return Task.CompletedTask;
};
// Enqueue events (sent automatically in background)
for (int i = 0; i < 1000; i++)
{
await producer.EnqueueEventAsync(new EventData($"Event {i}"));
}
// Flush remaining events before disposing
await producer.FlushAsync();
3. Receive Events (Production - EventProcessorClient)
using Azure.Identity;
using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Consumer;
using Azure.Messaging.EventHubs.Processor;
using Azure.Storage.Blobs;
// Blob container for checkpointing
var blobClient = new BlobContainerClient(
Environment.GetEnvironmentVariable("BLOB_STORAGE_CONNECTION_STRING"),
Environment.GetEnvironmentVariable("BLOB_CONTAINER_NAME"));
await blobClient.CreateIfNotExistsAsync();
// Create processor
var processor = new EventProcessorClient(
blobClient,
EventHubConsumerClient.DefaultConsumerGroup,
fullyQualifiedNamespace,
eventHubName,
new DefaultAzureCredential());
// Handle events
processor.ProcessEventAsync += async args =>
{
Console.WriteLine($"Partition: {args.Partition.PartitionId}");
Console.WriteLine($"Data: {args.Data.EventBody}");
// Checkpoint after processing (or batch checkpoints)
await args.UpdateCheckpointAsync();
};
// Handle errors
processor.ProcessErrorAsync += args =>
{
Console.WriteLine($"Error: {args.Exception.Message}");
Console.WriteLine($"Partition: {args.PartitionId}");
return Task.CompletedTask;
};
// Start processing
await processor.StartProcessingAsync();
// Run until cancelled
await Task.Delay(Timeout.Infinite, cancellationToken);
// Stop gracefully
await processor.StopProcessingAsync();
4. Partition Operations
// Get partition IDs
string[] partitionIds = await producer.GetPartitionIdsAsync();
// Send to specific partition (use sparingly)
var options = new SendEventOptions
{
PartitionId = "0"
};
await producer.SendAsync(events, options);
// Use partition key (recommended for ordering)
var batchOptions = new CreateBatchOptions
{
PartitionKey = "customer-123" // Events with same key go to same partition
};
using var batch = await producer.CreateBatchAsync(batchOptions);
EventPosition Options
Control where to start reading:
// Start from beginning
EventPosition.Earliest
// Start from end (new events only)
EventPosition.Latest
// Start from specific offset
EventPosition.FromOffset(12345)
// Start from specific sequence number
EventPosition.FromSequenceNumber(100)
// Start from specific time
EventPosition.FromEnqueuedTime(DateTimeOffset.UtcNow.AddHours(-1))
ASP.NET Core Integration
// Program.cs
using Azure.Identity;
using Azure.Messaging.EventHubs.Producer;
using Microsoft.Extensions.Azure;
builder.Services.AddAzureClients(clientBuilder =>
{
clientBuilder.AddEventHubProducerClient(
builder.Configuration["EventHub:FullyQualifiedNamespace"],
builder.Configuration["EventHub:Name"]);
clientBuilder.UseCredential(new DefaultAzureCredential());
});
// Inject in controller/service
public class EventService
{
private readonly EventHubProducerClient _producer;
public EventService(EventHubProducerClient producer)
{
_producer = producer;
}
public async Task SendAsync(string message)
{
using var batch = await _producer.CreateBatchAsync();
batch.TryAdd(new EventData(message));
await _producer.SendAsync(batch);
}
}
Best Practices
- Use
EventProcessorClientfor receiving — Never useEventHubConsumerClientin production - Checkpoint strategically — After N events or time interval, not every event
- Use partition keys — For ordering guarantees within a partition
- Reuse clients — Create once, use as singleton (thread-safe)
- Use
await using— Ensures proper disposal - Handle
ProcessErrorAsync— Always register error handler - Batch events — Use
CreateBatchAsync()to respect size limits - Use buffered producer — For high-volume scenarios with automatic batching
Error Handling
using Azure.Messaging.EventHubs;
try
{
await producer.SendAsync(batch);
}
catch (EventHubsException ex) when (ex.Reason == EventHubsException.FailureReason.ServiceBusy)
{
// Retry with backoff
await Task.Delay(TimeSpan.FromSeconds(5));
}
catch (EventHubsException ex) when (ex.IsTransient)
{
// Transient error - safe to retry
Console.WriteLine($"Transient error: {ex.Message}");
}
catch (EventHubsException ex)
{
// Non-transient error
Console.WriteLine($"Error: {ex.Reason} - {ex.Message}");
}
Checkpointing Strategies
| Strategy | When to Use |
|---|---|
| Every event | Low volume, critical data |
| Every N events | Balanced throughput/reliability |
| Time-based | Consistent checkpoint intervals |
| Batch completion | After processing a logical batch |
// Checkpoint every 100 events
private int _eventCount = 0;
processor.ProcessEventAsync += async args =>
{
// Process event...
_eventCount++;
if (_eventCount >= 100)
{
await args.UpdateCheckpointAsync();
_eventCount = 0;
}
};
Related SDKs
| SDK | Purpose | Install |
|---|---|---|
Azure.Messaging.EventHubs |
Core sending/receiving | dotnet add package Azure.Messaging.EventHubs |
Azure.Messaging.EventHubs.Processor |
Production processing | dotnet add package Azure.Messaging.EventHubs.Processor |
Azure.ResourceManager.EventHubs |
Management plane (create hubs) | dotnet add package Azure.ResourceManager.EventHubs |
Microsoft.Azure.WebJobs.Extensions.EventHubs |
Azure Functions binding | dotnet add package Microsoft.Azure.WebJobs.Extensions.EventHubs |
04 同仓库更多 Skills
azure-advisor-cost-optimization
Analyze Azure Advisor Cost/IdleResources findings and prevalidate deletion safety, including Service Groups. WHEN: "find unused Azure resources", "what resources am I paying for but no longer using", "idle-resource savings", "historical idle-resource savings", "are these idle resources safe to delete". Generic Cost, other subcategories, and cross-category requests use azure-advisor.
skill-creator
Guide for creating effective skills for AI coding agents working with Azure SDKs and Microsoft Foundry services. Use when creating new skills or updating existing skills.
podcast-generation
Generate AI-powered podcast-style audio narratives using Azure OpenAI's GPT Realtime Mini model via WebSocket. Use when building text-to-speech features, audio narrative generation, podcast creation from content, or integrating with Azure OpenAI Realtime API for real audio output. Covers full-stack implementation from React frontend to Python FastAPI backend with WebSocket streaming.
microsoft-docs
Understand Microsoft technologies by querying official documentation. Use whenever the user asks how something works, wants tutorials, needs configuration options, limits, quotas, or best practices for any Microsoft technology (Azure, .NET, M365, Windows, Power Platform, etc.)—even if they don't mention "docs." If the question is about understanding a concept rather than writing code, this is the right skill.
mcp-builder
Guide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP), Node/TypeScript (MCP SDK), or C#/.NET (Microsoft MCP SDK).
kql
KQL language expertise for writing correct, efficient Kusto Query Language queries. Covers syntax gotchas, join patterns, dynamic types, datetime pitfalls, regex patterns, serialization, memory management, result-size discipline, and advanced functions (geo, vector, graph). USE THIS SKILL whenever writing, debugging, or reviewing KQL queries — even simple ones — because the gotchas section prevents the most common errors that waste tool calls and cause expensive retry cascades. Trigger on: KQL, Kusto, ADX, Azure Data Explorer, Fabric Real-Time Intelligence, EventHouse, Log Analytics, log analysis, data exploration, time series, anomaly detection, summarize, where clause, join, extend, project, let statement, parse operator, extract function, any mention of pipe-forward query syntax.