title: Azure Cloud Service role instances - auto-scaling - Changing event not firing
body: <p>I got a Cloud Service deployment with 4 worker roles, one of which got auto-scaling enabled. As soon as auto-scaling occurs, all instances of all roles are recycling. </p>
  
  <p>Ideally, I'd like to stop the roles from recycling or at least terminate the work of all other roles in a controlled way.</p>
  
  <p>I found out, that you can <a href="https://stackoverflow.com/questions/22252802/azure-autoscale-restarts-running-instances">react to the RoleEnvironment.Changing event</a> and cancel it to request a graceful shutdown (i.e. make OnStop being called). However, by adding tracing output to the Changing event handler, I noticed that the Changing event was obviously not even fired, so the cancellation was not being registered either. </p>
  
  <pre><code>private void RoleEnvironmentChanging(object sender, RoleEnvironmentChangingEventArgs e)
  {
      // This tracing output does not show up in the logs table.
      Trace.TraceInformation("RoleEnvironmentChanging event fired.");
      if ((e.Changes.Any(change =&gt; change is RoleEnvironmentConfigurationSettingChange)))
      {
          // This one neither.
          Trace.TraceInformation("One of the changes is a RoleEnvironmentConfigurationSettingChange. Cancelling..");
  
          e.Cancel = true;
      }
      if ((e.Changes.Any(change =&gt; change is RoleEnvironmentTopologyChange)))
      {
          // This one neither.
          Trace.TraceInformation("One of the changes is a RoleEnvironmentTopologyChange. Cancelling.");
  
          e.Cancel = true;
      }
  }
  
  public override bool OnStart()
  {
      // Hook up to the changing event to prevent roles from unnecessarily restarting.
      RoleEnvironment.Changing += RoleEnvironmentChanging;
  
      // Set the maximum number of concurrent connections
      ServicePointManager.DefaultConnectionLimit = 12;
  
      bool result = base.OnStart();
  
      return result;
  }
  </code></pre>
  
  <p>Also <a href="http://azure.microsoft.com/blog/2011/01/04/responding-to-role-topology-changes/" rel="nofollow noreferrer">adding an internal endpoint</a> to each role did not bring the change. Here the configuration from the .csdef:</p>
  
  <pre><code>&lt;WorkerRole name="MyRole" vmsize="Medium"&gt;
  [...ConfigurationSettings...]
  &lt;Endpoints&gt;
    &lt;InternalEndpoint name="Endpoint1" protocol="http" /&gt;
  &lt;/Endpoints&gt;
  &lt;/WorkerRole&gt;
  </code></pre>
  
  <p>Also changing the protocol to "any" wasn't successful.</p>
  
  <p>How can I stop my role instances from recycling after a scaling operation?</p>
  
  <p><strong>EDIT:</strong><br>
  &raquo; Included code snippets<br>
  &raquo; Fixed typos</p>