你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

WCF Azure Queue Storage client library for .NET

WCF Azure Queue Storage client is the client side library that will enable WCF clients to be able to send requests to a CoreWCF service which uses Azure Queue Storage as a transport.

Getting started

Install the package

Install the Microsoft.WCF.Azure.StorageQueues.Client library for .NET with NuGet:

dotnet add package Microsoft.WCF.Azure.StorageQueues.Client --prerelease

Prerequisites

You need an Azure subscription and a Storage Account to use this package.

To create a new Storage Account, you can use the Azure portal, Azure PowerShell, or the Azure CLI. Here's an example using the Azure CLI:

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

Authenticate the service to Azure Queue Storage

To send requests to the Azure Queue Storage service, you'll need to configure WCF with the appropriate endpoint, and configure credentials. The Azure Identity library makes it easy to add Microsoft Entra ID support for authenticating with Azure services.

There are multiple authentication mechanisms for Azure Queue Storage. Which mechanism to use is configured via the property AzureQueueStorageBinding.Security.Transport.ClientCredentialType. The default authentication mechanism is Default which uses DefaultAzureCredential.

// Create a binding instance to use Azure Queue Storage.
// The default client credential type is Default, which uses DefaultAzureCredential
var aqsBinding = new AzureQueueStorageBinding();

// Create a ChannelFactory to using the binding and endpoint address, open it, and create a channel
string queueEndpointString = "https://MYSTORAGEACCOUNT.queue.core.windows.net/QUEUENAME";
var factory = new ChannelFactory<IService>(aqsBinding, new EndpointAddress(queueEndpointString));
factory.Open();
IService channel = factory.CreateChannel();

// IService dervies from IChannel so you can call channel.Open without casting
channel.Open();
await channel.SendDataAsync(42);

Learn more about enabling Microsoft Entra ID for authentication with Azure Storage in our documentation.

Other authentication credential mechanisms

If you are using a different credential mechanism such as StorageSharedKeyCredential, you configure the appropriate ClientCredentialType on the binding and set the credential on an AzureClientCredentials instance via an extension method.

// Create a binding instance to use Azure Queue Storage.
var aqsBinding = new AzureQueueStorageBinding();

// Configure the client credential type to use StorageSharedKeyCredential
aqsBinding.Security.Transport.ClientCredentialType = AzureClientCredentialType.StorageSharedKey;

// Create a ChannelFactory to using the binding and endpoint address
string queueEndpointString = "https://MYSTORAGEACCOUNT.queue.core.windows.net/QUEUENAME";
var factory = new ChannelFactory<IService>(aqsBinding, new EndpointAddress(queueEndpointString));

// Use extension method to configure WCF to use AzureClientCredentials and set the
// StorageSharedKeyCredential instance.
factory.UseAzureCredentials(credentials =>
{
    credentials.StorageSharedKey = GetStorageSharedKey();
});

// Local function to get the StorageSharedKey
StorageSharedKeyCredential GetStorageSharedKey()
{
    // Fetch shared key using a secure mechanism such as Azure Key Vault
    // and construct an instance of StorageSharedKeyCredential to return;
    return default;
}

// Open the factory and create a channel
factory.Open();
IService channel = factory.CreateChannel();

// IService dervies from IChannel so you can call channel.Open without casting
channel.Open();
await channel.SendDataAsync(42);

Other values for ClientCredentialType are Sas, Token, and ConnectionString. The credentials class AzureClientCredentials has corresponding properties to set each of these credential types.

Troubleshooting

If there is a problem with sending a request to Azure Queue Storage, the operation call will throw a CommunicationException exception. See CommunicationException.InnerException for the original exception thrown by the Azure Storage Queues client.

Key concepts

The goal of this project is to enable migrating existing WCF clients to .NET that are currently using MSMQ and wish to deploy their service to Azure, replacing MSMQ with Azure Queue Storage.

More general samples of using WCF can be found in the .NET samples repository. To create a service to receive messages sent to an Azure Storage Queue, see the Microsoft.CoreWCF.Azure.StorageQueues documentation.

Contributing

See the Storage CONTRIBUTING.md for details on building,testing, and contributing to this library.

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit cla.microsoft.com.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.