.NET용 Azure Storage 큐 클라이언트 라이브러리 - 버전 12.13.1

서버 버전: 2021-02-12, 2020-12-06, 2020-10-02, 2020-08-04, 2020-06-12, 2020-04-08, 2020-02-10, 2019-12-12, 2019-07-07 및 2019-02-02

Azure Queue Storage는 HTTP 또는 HTTPS를 사용하여 인증된 호출을 통해 전 세계 어디에서나 액세스할 수 있는 다수의 메시지를 저장하기 위한 서비스입니다. 단일 큐 메시지의 크기는 최대 64KB일 수 있으며, 하나의 큐에 스토리지 계정의 총 용량 제한까지 수백만 개의 메시지가 포함될 수 있습니다.

소스 코드 | 패키지(NuGet) | API 참조 설명서 | REST API 설명서 | 제품 설명서

시작

패키지 설치

NuGet을 사용하여 .NET용 Azure Storage 큐 클라이언트 라이브러리를 설치합니다.

dotnet add package Azure.Storage.Queues

필수 구성 요소

이 패키지를 사용하려면 Azure 구독스토리지 계정이 필요합니다.

새 Storage 계정을 만들려면 Azure Portal, Azure PowerShell 또는 Azure CLI를 사용할 수 있습니다. 다음은 Azure CLI 사용 예입니다.

az storage account create --name MyStorageAccount --resource-group MyResourceGroup --location westus --sku Standard_LRS

클라이언트 인증

Azure Queue Storage 서비스와 상호 작용하려면 QueueClient 클래스의 instance 만들어야 합니다. Azure ID 라이브러리를 사용하면 해당 Azure 서비스를 사용하여 Azure SDK 클라이언트를 인증하기 위한 Azure Active Directory 지원을 쉽게 추가할 수 있습니다.

// Create a QueueClient that will authenticate through Active Directory
Uri queueUri = new Uri("https://MYSTORAGEACCOUNT.queue.core.windows.net/QUEUENAME");
QueueClient queue = new QueueClient(queueUri, new DefaultAzureCredential());

설명서 및 샘플에서 Azure Storage를 사용하여 Azure Active Directory 인증을 사용하도록 설정하는 방법에 대해 자세히 알아봅니.

주요 개념

Queue Storage의 일반적인 사용은 다음과 같습니다.

  • 비동기적으로 처리할 작업 백로그 만들기
  • 분산 애플리케이션의 여러 부분 간에 메시지 전달

스레드로부터의 안전성

모든 클라이언트 instance 메서드는 스레드로부터 안전하고 서로 독립적임을 보장합니다(지침). 이렇게 하면 스레드 간에 클라이언트 인스턴스를 다시 사용하는 것이 항상 안전합니다.

추가 개념

클라이언트 옵션 | 응답 | 에 액세스 장기 실행 작업 | 오류 | 처리 진단 | 조롱 | 클라이언트 수명

예제

메시지 보내기

// We'll need a connection string to your Azure Storage account.
// You can obtain your connection string from the Azure Portal
// (click Access Keys under Settings in the Portal Storage account
// blade) or using the Azure CLI with:
//
//     az storage account show-connection-string --name <account_name> --resource-group <resource_group>
//
// You would normally provide the connection string to your
// application using an environment variable.
string connectionString = "<connection_string>";

// Name of the queue we'll send messages to
string queueName = "sample-queue";

// Get a reference to a queue and then create it
QueueClient queue = new QueueClient(connectionString, queueName);
queue.Create();

// Send a message to our queue
queue.SendMessage("Hello, Azure!");

메시지 받기

// We'll need a connection string to your Azure Storage account.
string connectionString = "<connection_string>";

// Name of an existing queue we'll operate on
string queueName = "sample-queue";

// Get a reference to a queue and then fill it with messages
QueueClient queue = new QueueClient(connectionString, queueName);
queue.SendMessage("first");
queue.SendMessage("second");
queue.SendMessage("third");

// Get the next messages from the queue
foreach (QueueMessage message in queue.ReceiveMessages(maxMessages: 10).Value)
{
    // "Process" the message
    Console.WriteLine($"Message: {message.Body}");

    // Let the service know we're finished with the message and
    // it can be safely deleted.
    queue.DeleteMessage(message.MessageId, message.PopReceipt);
}

비동기 API

동기 API와 비동기 API를 모두 완벽하게 지원합니다.

// We'll need a connection string to your Azure Storage account.
string connectionString = "<connection_string>";

// Name of the queue we'll send messages to
string queueName = "sample-queue";

// Get a reference to a queue and then create it
QueueClient queue = new QueueClient(connectionString, queueName);
await queue.CreateAsync();

// Send a message to our queue
await queue.SendMessageAsync("Hello, Azure!");

메시지 인코딩

이 버전의 라이브러리는 기본적으로 메시지를 인코딩하지 않습니다. V11 및 이전 버전과 Azure Functions 기본적으로 base64로 인코딩된 메시지를 사용합니다. 따라서 interop 시나리오에 이 기능을 사용하는 것이 좋습니다.

QueueClientOptions queueClientOptions = new QueueClientOptions()
{
    MessageEncoding = QueueMessageEncoding.Base64
};

QueueClient queueClient = new QueueClient(connectionString, queueName, queueClientOptions);

문제 해결

모든 Azure Storage 큐 서비스 작업은 유용한 ErrorCodes와 함께 실패 시 RequestFailedException을 throw합니다. 이러한 오류 중 대부분은 복구할 수 있습니다.

// We'll need a connection string to your Azure Storage account.
string connectionString = "<connection_string>";

// Name of an existing queue we'll operate on
string queueName = "sample-queue";

try
{
    // Try to create a queue that already exists
    QueueClient queue = new QueueClient(connectionString, queueName);
    queue.Create();
}
catch (RequestFailedException ex)
    when (ex.ErrorCode == QueueErrorCode.QueueAlreadyExists)
{
    // Ignore any errors if the queue already exists
}

다음 단계

큐 샘플 시작:

  1. 헬로 월드: 큐에 넣기, 큐에 넣기, 피킹 및 큐 메시지 업데이트(또는 비동기)
  2. 인증: 연결 문자열, 공유 키, 공유 액세스 서명 및 Azure Active Directory를 사용하여 인증합니다.

참여

이 라이브러리의 빌드, 테스트 및 기여에 대한 자세한 내용은 Storage CONTRIBUTING.md 참조하세요.

이 프로젝트에 대한 기여와 제안을 환영합니다. 대부분의 경우 기여하려면 권한을 부여하며 실제로 기여를 사용할 권한을 당사에 부여한다고 선언하는 CLA(기여자 라이선스 계약)에 동의해야 합니다. 자세한 내용은 cla.microsoft.com.

이 프로젝트에는 Microsoft Open Source Code of Conduct(Microsoft 오픈 소스 준수 사항)가 적용됩니다. 자세한 내용은 Code of Conduct FAQ(규정 FAQ)를 참조하세요. 또는 추가 질문이나 의견은 opencode@microsoft.com으로 문의하세요.

Impressions