【问题标题】:Catching an exception in an Azure Worker Role's onStart method?在 Azure 辅助角色的 onStart 方法中捕获异常?
【发布时间】:2012-06-21 17:12:29
【问题描述】:

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

【问题讨论】:

  • 不是您问题的确切答案,但您的代码正在尝试访问 c:您需要将此类访问替换为本地存储:link。一个例子:link
  • 感谢里奇!这解决了问题:)

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 2017-05-03
    • 2012-08-28
    • 2012-06-11
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    相关资源
    最近更新 更多