Search code examples
azureazure-cloud-servicesazure-sdk-.netazure-role-environment

Azure Cloud Service: RoleEnvironment.StatusCheck event not firing


I am maintaining a legacy Cloud Services application hosted on Azure targeting .net 4.6.1. Inside the Application_Start method of the Global.asax on the Web Role we are registering an event handler for RoleEnvironment.StatusCheck however our logs are demonstrating that this event call back is never being called or triggered.

According to this blog: https://convective.wordpress.com/2010/03/18/service-runtime-in-windows-azure/ we were expecting this event to be triggered every 15 seconds and we believe this was happening however has since stopped. We expect that the stopped working around the time we installed some new DLLs into the solution (some of these dlls include: Microsoft.Rest.ClientRuntime.dll, Microsoft.Azure.Storage.Common.dll, Microsoft.Azure.Storage.Blob.dll, Microsoft.Azure.KeyVault.dll)

We've tried RDP-ing onto the VM to check the event logs but nothing obvious is there. Any suggestions on where we may be able to search for clues?


Solution

  • It seems your event handler is not registered. Try below code with a different approach:

    public class WorkerRole : RoleEntryPoint
    {
        public override bool OnStart()
        {
            RoleEnvironment.StatusCheck += RoleEnvironmentStatusCheck;
            return base.OnStart();
        }
    
        // Use the busy object to indicate that the status of the role instance must be Busy
        private volatile bool busy = true;
    
        private void RoleEnvironmentStatusCheck(object sender, RoleInstanceStatusCheckEventArgs e)
        {
            if (this.busy)
            {
                // Sets the status of the role instance to Busy for a short interval.
                // If you want the role instance to remain busy, add code to
                // continue to call the SetBusy method
                e.SetBusy();
            }
        }
    
        public override void Run()
        {
            Trace.TraceInformation("Worker entry point called", "Information");
    
            while (true)
            {
                Thread.Sleep(10000);
            }
        }
    
        public override void OnStop()
        {
            base.OnStop();
        }
    }