Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching an exception in an Azure Worker Role's onStart method?

I'm running into an issue where my deployment loops through Recycling. From Visual Studio:

"Role instances recycled for a certain amount of times during an update or upgrade operation. This indicates that the new version of your service or the configuration settings you provided when configuring the service prevent role instances from running. The most likely reason for this is that your code throws an unhandled exception. Please consider fixing your service or changing your configuration settings so that role instances do not throw unhandled exceptions. Then start another update or upgrade operation. Until you start another update or upgrade operation, Windows Azure will continue trying to update your service to the new version or configuration you provided"

My question: What is the best way to catch the exception? I'm a bit new to C#. A condensed version of my onStart in case it helps:

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();
    }
}

}

Also in case it helps, here is the stack trace from 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()
like image 440
RobVious Avatar asked Jun 21 '12 17:06

RobVious


1 Answers

My blog post Printf("HERE") in the Cloud may help. Essentially, wrap the whole thing in a try/catch and log the error.

like image 89
user94559 Avatar answered Sep 19 '22 00:09

user94559