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

用于 .NET 的 Azure 服务总线库Azure Service Bus libraries for .NET

概述Overview

Azure 服务总线是位于各个应用程序之间的消息传送基础结构,允许应用程序交换消息,从而扩大规模并提高恢复能力。Azure Service Bus is a messaging infrastructure that sits between applications allowing them to exchange messages for improved scale and resiliency.

客户端库Client library

直接从 Visual Studio 包管理器控制台安装 NuGet 包Install the NuGet package directly from the Visual Studio Package Manager console.

Visual Studio 包管理器Visual Studio Package Manager

Install-Package Microsoft.Azure.ServiceBus

代码示例Code Example

此示例向服务总线队列发送消息。This example sends a message to a Service Bus queue.

// using Microsoft.Azure.ServiceBus;
// Microsoft.Azure.ServiceBus 2.0.0 (stable)

byte[] messageBody = System.Text.Encoding.Unicode.GetBytes("Hello, world!");
ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(connectionString);
QueueClient client = new QueueClient(builder, ReceiveMode.PeekLock);
client.SendAsync(new Message(messageBody));

管理库Management library

直接从 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.ServiceBus.Fluent

.NET Core CLI.NET Core CLI

dotnet add package Microsoft.Azure.Management.ServiceBus.Fluent

代码示例Code Example

此示例创建最大大小为 1024 MB 的服务总线队列。This example creates a Service Bus queue with a maximum size of 1024 MB.

// using Microsoft.Azure.Management.ServiceBus.Fluent;
// using Microsoft.Azure.Management.ServiceBus.Fluent.Models;

using (ServiceBusManagementClient client = new ServiceBusManagementClient(credentials))
{
    client.SubscriptionId = subscriptionId;
    QueueInner parameters = new QueueInner
    {
        MaxSizeInMegabytes = 1024
    };
    await client.Queues.CreateOrUpdateAsync(resourceGroupName, namespaceName, queueName, parameters);
}

示例Samples

查看 Azure 服务总线示例的完整列表View the complete list of Azure Service Bus samples.