5

我遇到了一个问题,我的部署循环通过回收。从 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()
4

1 回答 1

8

我在云端的博文 Printf("HERE")可能会有所帮助。本质上,将整个事情包装在 try/catch 中并记录错误。

于 2012-06-21T18:08:45.320 回答