Search code examples
azurehttpswebwebroleonstart

Handling the RoleEntryPoint.OnStart() event of an Azure Webrole in the second website


First Why I'm doing this:
I have a WCF service deployed as an Azure Webrole using HTTPS as endpoint. I need to have a custom "load balancer probe" set for this service. Azure load balancers does not support HTTPS, so I've deployed a second website with http endpoint to use it as my "load balancer prob"

So What's my problem?!
I have a webrole with two websites like:

<WebRole name="WebRole1" vmsize="Medium">
 <Sites>
   <Site name="Web" physicalDirectory="roles\WebRole1">
     <Bindings>
       <Binding name="Web" endpointName="WebRoleEndpoint" />
     </Bindings>
   </Site>
   <Site name="Web2" physicalDirectory="roles\HealthProb">
     <Bindings>
       <Binding name="Web" endpointName="HealthProbEndpoint" />
     </Bindings>
   </Site>
 </Sites>
 ...

it is recommended to handle the RoleEntryPoint.OnStop() to make sure you return the correct health status code to the loadbalancer (http://blog.mdavies.net/2013/04/27/custom-load-balancing-endpoints-azure-web-worker-vm-role/)
my problem is how I can hendle the RoleEntryPoint.OnStop() in my second website. though in the webrole I can easily do this by extending and overriding the RoleEntryPoint.OnStop(), in the second website I cant handle that event.

Is there any way that I can handle the OnStop or some similar event in the second website?!


Solution

  • Any process running on the VM can subscribe to the RoleEnvironment events. Simply add a reference to Microsoft.WindowsAzure.ServiceRuntime.DLL in your website project and then add a handler for the RoleEnvironment.Stopping event.

    But instead of using a custom load balancer probe I would recommend just using the RoleEnvironment.StatusCheck event in your website. The purpose of the custom load balancer probe is to allow you to monitor the health of your w3wp process since the Azure runtime doesn't normally monitor this process. But if you are deploying a separate site to implement the custom LB probe then you really aren't monitoring your main w3wp process. In this case you are better off just using the StatusCheck event which can implement your LB logic, and then you get the Stopping/OnStop behavior for free.