Chuyển đến nội dung
Skillpedia
API và máy chủ (backend)48k · #3927

azure-eventhub-dotnet

Skill này làm gì

Đoạn mã này cho phép các chương trình khác gửi và nhận một lượng lớn dữ liệu theo thời gian thực qua dịch vụ Azure Event Hubs. Có thể hình dung nó như một đường ống tốc độ cao để truyền thông tin.

Khi nào nên dùng

  • ✓Khi bạn cần gửi nhiều dữ liệu nhanh chóng đến một dịch vụ.
  • ✓Khi bạn cần nhiều hệ thống nhận thông tin cùng một lúc.
  • ✓Khi bạn muốn theo dõi và xử lý các luồng dữ liệu liên tục.

Liên kết skill

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/azure-eventhub-dotnet/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/azure-eventhub-dotnet/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Gửi 1000 tin nhắn “Hello World” đến Event Hub tên là “my-hub”.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Đoạn mã kết nối với Azure Event Hubs và dùng các “client” khác nhau để gửi hoặc nhận dữ liệu. Để nhận dữ liệu một cách đáng tin cậy trong môi trường thực tế, nó dùng một “processor” có lưu lại tiến trình, nhờ đó không bỏ sót dữ liệu nào.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

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
EVENTHUB_NAME=<event-hub-name>

# For checkpointing (EventProcessorClient)
BLOB_STORAGE_CONNECTION_STRING=<storage-connection-string>
BLOB_CONTAINER_NAME=<checkpoint-container>

# Alternative: Connection string auth (not recommended for production)
EVENTHUB_CONNECTION_STRING=Endpoint=sb://<namespace>.servicebus.windows.net/;SharedAccessKeyName=...

Authentication

using Azure.Identity;
using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Producer;

// Always use DefaultAzureCredential for production
var credential = new DefaultAzureCredential();

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;

…
Câu hỏi thường gặp

“Event Hubs” là gì?

Đây là dịch vụ đám mây của Microsoft (Azure), hoạt động như một nơi thu gom và phân phối sự kiện hoặc thông điệp quy mô lớn, có khả năng xử lý khối lượng dữ liệu rất lớn.

Khác biệt giữa `EventHubProducerClient` và `EventHubBufferedProducerClient` là gì?

`EventHubProducerClient` gửi dữ liệu ngay khi bạn yêu cầu, còn `EventHubBufferedProducerClient` gom dữ liệu thành lô và gửi ở chế độ nền, hiệu quả hơn khi khối lượng lớn.

Vì sao nên dùng `EventProcessorClient` để nhận dữ liệu trong môi trường thực tế?

Nó ổn định hơn và quản lý việc nhận dữ liệu an toàn hơn, vì lưu lại tiến trình (checkpoint) để đảm bảo không mất dữ liệu nếu chương trình cần khởi động lại.

Chi tiết

Mô tả gốc của tác giả

Azure Event Hubs SDK for .NET.

repo: sickn33/agentic-awesome-skills

Độ phổ biến

Sao GitHub
48k
Vị trí xếp hạng
#3927
Giấy phép
MIT
Cập nhật
15/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự