Microsoft.WindowsAzure.Storage.StorageException:远程服务器返回错误:(404)Not Found

时间:2015-12-03 19:58:55

标签: c# azure

我在工作人员角色中收到此错误:

 Microsoft.WindowsAzure.Storage.StorageException: The remote server returned an error: (404) Not Found. ---> System.Net.WebException: The remote server returned an error: (404) Not Found.     at System.Net.HttpWebRequest.GetResponse()     at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](StorageCommandBase`1 cmd, IRetryPolicy policy, OperationContext operationContext)     --- End of inner exception stack trace ---     at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](StorageCommandBase`1 cmd, IRetryPolicy policy, OperationContext operationContext)     at Microsoft.WindowsAzure.Storage.Queue.CloudQueue.DeleteMessage(String messageId, String popReceipt, QueueRequestOptions options, OperationContext operationContext)     at Microsoft.WindowsAzure.Storage.Queue.CloudQueue.DeleteMessage(CloudQueueMessage message, QueueRequestOptions options, OperationContext operationContext)     at CloudCartConnector.TaskRole2.WorkerRole.ExecuteTask() in C:\a\src\CCC\Source\CloudCartConnector.TaskRole2\WorkerRole.cs:line 101  Request Information  RequestID:7a7c08ec-0003-0059-6d7b-2d118f000000  RequestDate:Thu, 03 Dec 2015 03:33:11 GMT  StatusMessage:The specified queue does not exist.  ErrorCode:QueueNotFound  

如果on start方法中存在异常,是否会导致worker角色无法运行?我应该在on start方法中输入try catch语句并返回base.OnStart()吗?如果我的存储帐户因MS升级或服务器关闭而变得不可用,那么try是最好的吗?

    public override bool OnStart()
    {
        ServicePointManager.DefaultConnectionLimit = 12;
        // Retrieve storage account from connection string.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            CloudConfigurationManager.GetSetting("StorageConnectionString"));

        // Create the queue client.
        CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();

        // Retrieve a reference to a queue.
        queue = queueClient.GetQueueReference("taskqueue");

        return base.OnStart();
    }

在此代码下面,我执行一项任务。如果队列为空,我应该说,只返回?

public string GetTasks()
    {


                CloudQueueMessage cloudQueueMessasge = new CloudQueueMessage(message);
                queue.AddMessage(cloudQueueMessasge, new TimeSpan(0, 30, 0));                
        }
        catch (Exception ex)
        {
            return ex.ToString();

        }
    }

1 个答案:

答案 0 :(得分:3)

你的OnStart方法应该有一个try...catch块。

try{
      CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
      CloudConfigurationManager.GetSetting("StorageConnectionString"));

      // Create the queue client.
      CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();

            // Retrieve a reference to a queue.
       queue = queueClient.GetQueueReference("taskqueue");
}
catch(Microsoft.WindowsAzure.Storage.StorageException e)
{
   // Exception Handling & Logging
   // Return false for OnStart
}

您还应检查GetTasks()方法中队列是否为空,以防止丢失潜在的NullReferenceException。