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

1

5

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()
Despoil answered 21/6, 2012 at 17:12 Comment(1)
Not an exact answer to your question, but your code is attempting to access c: You need to replace such access with Local Storage: link. An example: linkHinds
F
8

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

Furey answered 21/6, 2012 at 18:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.