I want create queue only if queue is not available in azure storage. I am using this PutMessage method to create message in queue. This method is working fine but problem is with me where I need to attach CreateQueue method.
My createqueue method is also working fine but I can't identify where need to add because when PutMessage calls request.GetResponse() then it generate error if queue is wrong. Thanks
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"];
}
}
CodePudding user response:
You can put the following code in try/catch block and check for WebException
.
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
String requestId = response.Headers["x-ms-request-id"];
}
If the queue does not exist, then you will get an error with 404 (Not Found)
response status. In this case you should try to create the queue and once you receive a successful response back, you should call your put message again.
Your code would look something like:
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;
}
}
Please note that you can get other exceptions as well however in the context of this operation we will not try to handle them and simply rethrow those exceptions.