在Azure Worker Role的onStart方法中捕获异常?

时间:2012-06-21 17:12:29

标签: azure azure-storage azure-storage-blobs azure-worker-roles

我遇到了一个问题,我的部署循环通过Recycling。来自Visual Studio:

“在更新或升级操作期间,角色实例会循环一定次数。这表示新服务版本或配置服务时提供的配置设置会阻止角色实例运行。最可能的原因是这是您的代码抛出未处理的异常。请考虑修复您的服务或更改配置设置,以便角色实例不会抛出未处理的异常。然后启动另一个更新或升级操作。在您开始另一个更新或升级操作之前,Windows Azure将继续尝试将您的服务更新为您提供的新版本或配置“

我的问题:捕获异常的最佳方法是什么?我对C#有点新鲜。我的onStart的精简版,以防它有用:

public override void Run()
    {
        // This is a sample worker implementation. Replace with your logic.
        Trace.WriteLine("WorkerRole1 entry point called", "Information");

        while (true)
        {
            Thread.Sleep(10000);
            Trace.WriteLine("Working", "Information");
        }
    }

public override bool OnStart()
    {

        // Set the maximum number of concurrent connections 
        ServicePointManager.DefaultConnectionLimit = 12;

        // Retrieve storage account from Connection String
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

        // Create blob client
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        // retrieve reference to container (blob resides within container)
        CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

        // create container if it doesn't already exist
        container.CreateIfNotExist();

        // make container public *temp*
        container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });

        // Retrieve references to required blobs
        CloudBlob batch = container.GetBlobReference("batch.bat");

        // Download batch file
        using (var fileStream = System.IO.File.OpenWrite(@"C:\batch.bat"))
        {
            zip.DownloadToStream(fileStream);
        }


        // run batch file

        System.Diagnostics.Process proc = new System.Diagnostics.Process();
        proc.StartInfo.FileName = "C:\\batch.bat";
        proc.StartInfo.RedirectStandardError = false;
        proc.StartInfo.RedirectStandardOutput = false;
        proc.StartInfo.UseShellExecute = false;
        proc.Start();
        proc.WaitForExit();

        return base.OnStart();
    }
}

}

如果它有帮助,这里是来自RDP的堆栈跟踪:

Stack:


at System.IO.__Error.WinIOError(Int32, System.String)
at System.IO.FileStream.Init(System.String, System.IO.FileMode, System.IO.FileAccess, Int32, Boolean, System.IO.FileShare, Int32, System.IO.FileOptions, SECURITY_ATTRIBUTES, System.String, Boolean, Boolean)
at System.IO.FileStream..ctor(System.String, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, Int32, System.IO.FileOptions, System.String, Boolean)
at System.IO.FileStream..ctor(System.String, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare)
at System.IO.File.OpenWrite(System.String)
at WorkerRole1.WorkerRole.OnStart()
at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.InitializeRoleInternal(Microsoft.WindowsAzure.ServiceRuntime.Implementation.Loader.RoleType)
at Microsoft.WindowsAzure.ServiceRuntime.Implementation.Loader.RoleRuntimeBridge.<InitializeRole>b__0()
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()

1 个答案:

答案 0 :(得分:8)

我的博文Printf("HERE") in the Cloud可能有所帮助。基本上,将整个事物包装在try / catch中并记录错误。