仅当使用 rest API 队列不可用时才在 azure 队列中创建队列
Create queue in azure queue only if queue not available using rest API
我只想在队列在 Azure 存储中不可用时创建队列。我正在使用此 PutMessage 方法在队列中创建消息。这种方法工作正常,但问题出在我身上,我需要附加 CreateQueue 方法。
我的 createqueue 方法也工作正常,但我无法确定需要添加的位置,因为当 PutMessage 调用 request.GetResponse() 时,如果队列是错误的。谢谢
public static void PutMessage(String queueName, String message)
{
String requestMethod = "POST";
String urlPath = $"{queueName}/messages";
String storageServiceVersion = "2017-11-09";
String dateInRfc1123Format = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);
String messageText = $"<QueueMessage><MessageText>{message}</MessageText></QueueMessage>";
UTF8Encoding utf8Encoding = new UTF8Encoding();
Byte[] messageContent = utf8Encoding.GetBytes(messageText);
Int32 messageLength = messageContent.Length;
String canonicalizedHeaders = String.Format(
"x-ms-date:{0}\nx-ms-version:{1}",
dateInRfc1123Format,
storageServiceVersion);
String canonicalizedResource = $"/{StorageAccountName}/{urlPath}";
String stringToSign = $"{requestMethod}\n\n\n{messageLength}\n\n\n\n\n\n\n\n\n{canonicalizedHeaders}\n{canonicalizedResource}";
String authorizationHeader = CreateAuthorizationHeader(stringToSign);
Uri uri = new Uri("https://" + StorageAccountName + ".queue.Azure.com/" + urlPath);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = requestMethod;
request.Headers.Add("x-ms-date", dateInRfc1123Format);
request.Headers.Add("x-ms-version", storageServiceVersion);
request.Headers.Add("Authorization", authorizationHeader);
request.ContentLength = messageLength;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(messageContent, 0, messageLength);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
String requestId = response.Headers["x-ms-request-id"];
}
}
您可以将以下代码放在 try/catch 块中并检查 WebException
.
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
String requestId = response.Headers["x-ms-request-id"];
}
如果队列不存在,那么您将收到 404 (Not Found)
响应状态的错误。在这种情况下,您应该尝试创建队列,一旦收到成功的响应,您应该再次调用您的放置消息。
您的代码将类似于:
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
String requestId = response.Headers["x-ms-request-id"];
}
}
catch (WebException exception)
{
HttpWebResponse response = (HttpWebResponse) exception.Response;
if (response.StatusCode == HttpStatusCode.NotFound)
{
//Call your create queue method here...
CreateQueue(queueName);
//Once the operation is successful, call PutMessage again.
PutMessage(queueName, message);
}
else
{
throw;
}
}
请注意,您也可以获得其他异常,但是在此操作的上下文中,我们不会尝试处理它们,而只是重新抛出这些异常。
我只想在队列在 Azure 存储中不可用时创建队列。我正在使用此 PutMessage 方法在队列中创建消息。这种方法工作正常,但问题出在我身上,我需要附加 CreateQueue 方法。
我的 createqueue 方法也工作正常,但我无法确定需要添加的位置,因为当 PutMessage 调用 request.GetResponse() 时,如果队列是错误的。谢谢
public static void PutMessage(String queueName, String message)
{
String requestMethod = "POST";
String urlPath = $"{queueName}/messages";
String storageServiceVersion = "2017-11-09";
String dateInRfc1123Format = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);
String messageText = $"<QueueMessage><MessageText>{message}</MessageText></QueueMessage>";
UTF8Encoding utf8Encoding = new UTF8Encoding();
Byte[] messageContent = utf8Encoding.GetBytes(messageText);
Int32 messageLength = messageContent.Length;
String canonicalizedHeaders = String.Format(
"x-ms-date:{0}\nx-ms-version:{1}",
dateInRfc1123Format,
storageServiceVersion);
String canonicalizedResource = $"/{StorageAccountName}/{urlPath}";
String stringToSign = $"{requestMethod}\n\n\n{messageLength}\n\n\n\n\n\n\n\n\n{canonicalizedHeaders}\n{canonicalizedResource}";
String authorizationHeader = CreateAuthorizationHeader(stringToSign);
Uri uri = new Uri("https://" + StorageAccountName + ".queue.Azure.com/" + urlPath);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = requestMethod;
request.Headers.Add("x-ms-date", dateInRfc1123Format);
request.Headers.Add("x-ms-version", storageServiceVersion);
request.Headers.Add("Authorization", authorizationHeader);
request.ContentLength = messageLength;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(messageContent, 0, messageLength);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
String requestId = response.Headers["x-ms-request-id"];
}
}
您可以将以下代码放在 try/catch 块中并检查 WebException
.
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
String requestId = response.Headers["x-ms-request-id"];
}
如果队列不存在,那么您将收到 404 (Not Found)
响应状态的错误。在这种情况下,您应该尝试创建队列,一旦收到成功的响应,您应该再次调用您的放置消息。
您的代码将类似于:
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
String requestId = response.Headers["x-ms-request-id"];
}
}
catch (WebException exception)
{
HttpWebResponse response = (HttpWebResponse) exception.Response;
if (response.StatusCode == HttpStatusCode.NotFound)
{
//Call your create queue method here...
CreateQueue(queueName);
//Once the operation is successful, call PutMessage again.
PutMessage(queueName, message);
}
else
{
throw;
}
}
请注意,您也可以获得其他异常,但是在此操作的上下文中,我们不会尝试处理它们,而只是重新抛出这些异常。