How to Create Queue in Windows Azure?

2020-06-30 05:10发布

问题:

I am using below code to create queue, using SharedSecretTokenProvider. However I am not able to supply correct values of managerName & managerKey value form windows azure portal.

This results in Http 401 Unauthorized exception. How do I resolve this error?

const string queueName = "thequeue";
var tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(
    ConfigurationManager.AppSettings["managerName"],
    ConfigurationManager.AppSettings["managerKey"]);

Uri uri = ServiceBusEnvironment.CreateServiceUri("http", "MyNamespace" , string.Empty);
NamespaceManager namespaceManager = new NamespaceManager(uri, tokenProvider);

QueueDescription qd = namespaceManager.CreateQueue(new QueueDescription(queueName)
{
    DefaultMessageTimeToLive = TimeSpan.FromMinutes(15),
    DuplicateDetectionHistoryTimeWindow = TimeSpan.FromMinutes(10),
    LockDuration = TimeSpan.FromMinutes(2),
    EnableBatchedOperations = true,
    EnableDeadLetteringOnMessageExpiration = true,
    RequiresDuplicateDetection = true
});

回答1:

I had tried this a couple of times with your code before I realized the problem. You are using the SharedSecretTokenProvider which will go to ACS thinking it has an issuer and a key. Since you are trying to use the SAS you'll want to use a CreateSharedAccessSignatureTokenProvider instead.

Swap that out and provide the key and keyName and you should be good.

Also, Viperguynaz is correct, you should use the "sb" instead of http as well. It was failing before it reached there because the token provider was correctly declining you access since it didn't understand the key and keyname you were passing for what it thought was the issuer and key that ACS uses.



回答2:

Start with ServiceBusEnvironment.CreateServiceUri Method. Note that Service Bus endpoint URIs must always use the “sb://” protocol; for example sb://contoso.servicebus.windows.net/helloservicebus.

Uri address = ServiceBusEnvironment.CreateServiceUri("sb", "contoso", "helloservicebus");

Get your URI inputs set correctly and you should be in business.