gpt4 book ai didi

azure - 在 Azure 辅助角色的 onStart 方法中捕获异常?

转载 作者:行者123 更新时间:2023-12-04 17:54:16 24 4
gpt4 key购买 nike

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

最佳答案

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

关于azure - 在 Azure 辅助角色的 onStart 方法中捕获异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11143280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com