Detecting configuration changes in code
When we use configuration settings in our roles, such as the AzureBakery.Production.Storage
setting we created in the previous chapter, we can change these at runtime in the role's CONFIGURATION tab in the portal:
To detect config changes in code, we can attach event handlers to the RoleEnvironment.Changing
event, which is fired before a config change is applied to the role and the RoleEnvironment.Changed
event, which is fired after the change has been applied:
public override bool OnStart() { Trace.TraceInformation("OrderProcessorRole.WorkerRole.OnStart - Begin"); RoleEnvironment.Changing += RoleEnvironment_Changing; RoleEnvironment.Changed += RoleEnvironment_Changed; }
Using the Changing
event, we can set the e.Cancel
flag to true
, which will cause the role to recycle, and the new changes are applied on the next start:
private void RoleEnvironment_Changing(object sender, RoleEnvironmentChangingEventArgs e) { // Implements the changes...