您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

用于 .NET 的 Azure 事件中心库Azure Event Hubs libraries for .NET

概述Overview

Azure 事件中心是高度可缩放的数据流式处理平台和事件引入服务。Azure Event Hubs is a highly scalable data streaming platform and event ingestion service.

若要详细了解 Azure 事件中心,请阅读什么是事件中心?一文。To learn more about Azure Event Hubs, read the article What is Event Hubs?. 若要开始使用,请查看事件中心编程指南To get started, check out the Event Hubs Programming Guide.

客户端库Client library

使用事件中心客户端可与事件中心来回发送和接收消息。Use the Event Hubs client to send and receive messages to and from Event Hubs.

直接从 Visual Studio 包管理器控制台或使用 .NET Core CLI 安装 NuGet 包Install the NuGet package directly from the Visual Studio Package Manager console or with the .NET Core CLI.

Visual Studio 包管理器Visual Studio Package Manager

Install-Package Microsoft.Azure.EventHubs
dotnet add package Microsoft.Azure.EventHubs

代码示例Code Example

以下代码创建事件中心客户端并将消息发送到中心。The following code creates an Event Hubs client and sends a message to the hub.

EventHubsConnectionStringBuilder connectionStringBuilder = new EventHubsConnectionStringBuilder(eventHubConnectionString)
{
    EntityPath = eventHubEntityPath
};

EventHubClient eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
string message = $"Message {i}";
Console.WriteLine($"Sending message: {message}");
await eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(message)));

管理库Management library

使用事件中心管理库可创建、更新和删除中心与使用者组。Use the Event Hubs management library to create, update, and remove hubs and consumer groups.

直接从 Visual Studio 包管理器控制台或使用 .NET Core CLI 安装 NuGet 包Install the NuGet package directly from the Visual Studio Package Manager console or with the .NET Core CLI.

Visual Studio 包管理器Visual Studio Package Manager

Install-Package Microsoft.Azure.Management.EventHub
dotnet add package Microsoft.Azure.Management.EventHub

代码示例Code Example

以下代码创建新的事件中心。The following code creates a new event hub.

TokenCredentials creds = new TokenCredentials(token);
EventHubManagementClient ehClient = new EventHubManagementClient(creds)
{
    SubscriptionId = subscriptionId
};

EventHubCreateOrUpdateParameters ehParams = new EventHubCreateOrUpdateParameters()
{
    Location = location
};

Console.WriteLine("Creating Event Hub...");
await ehClient.EventHubs.CreateOrUpdateAsync(resourceGroupName, namespaceName, EventHubName, ehParams);
Console.WriteLine("Created Event Hub successfully.");

教程Tutorials

示例Samples

详细了解可在应用中使用的示例 .NET 代码Explore more sample .NET code you can use in your apps.