0
votes

I have a Windows Azure cloud service with a Web Role and a Worker Role. I I have built a website that allows me to perform various management functions against the cloud service (stop/start, reboot instance, add instance, remove instance). All functions are performed via the web api. My issue is that when I add an instance of the web role, the worker role reboots. Note that this doesn't happen if I add an instance via the Azure portal. The code functions correctly in all other aspects. Any idea how to do this so that only the role being affected recycles instead of all roles recycling?

My code:

    public void AddInstance()
    {
        XDocument configDoc = this.GetDeploymentConfiguration();
        var ns = configDoc.Root.GetDefaultNamespace();

        configDoc.Root
            .Elements( ns + "Role" )
            .FirstOrDefault( r => r.Attribute( "name" ).Value.ToLower() == this.RoleName.ToLower() )
            .Element( ns + "Instances" )
            .Attribute( "count" )
            .Value = ( int.Parse( configDoc.Root
                           .Elements( ns + "Role" )
                           .FirstOrDefault( r => r.Attribute( "name" ).Value.ToLower() == this.RoleName.ToLower() )
                           .Element( ns + "Instances" )
                           .Attribute( "count" )
                           .Value ) + 1 ).ToString();

        string encodedString = Convert.ToBase64String( Encoding.UTF8.GetBytes( configDoc.ToString() ) );
        this.SetDeploymentConfig( encodedString );
    }

    public XDocument GetDeploymentConfiguration()
    {
        string uri = string.Format( this.servicePropertiesOperationFormat, this.subscriptionID, this.serviceName, "production", "" );
        ServiceManagementOperation operation = new ServiceManagementOperation( this.thumbprint, this.versionID );

        var xdoc= operation.Invoke( uri );
        var myelm = xdoc.Element( wa + "Deployment" ).Element( wa + "Configuration" );

        var mystring=  Encoding.UTF8.GetString( Convert.FromBase64String( myelm.Value ) );

        return XDocument.Parse( mystring );
    }

    public string SetDeploymentConfig( string configurationFile )
    {
        string uri = string.Format( this.servicePropertiesOperationFormat, this.subscriptionID, this.serviceName, "production", "/?comp=config" );
        ServiceManagementOperation operation = new ServiceManagementOperation( this.thumbprint, this.versionID );
        string payloadString = string.Format(
            @"<?xml version=""1.0"" encoding=""utf-8""?>
            <ChangeConfiguration xmlns=""http://schemas.microsoft.com/windowsazure"">
                    <Configuration>{0}</Configuration>
            </ChangeConfiguration>", configurationFile );

        XDocument payload = XDocument.Parse( payloadString );
        return operation.Invoke( uri, payload );
    }
1
Do your roles share an Upgrade Domain? Consider the details explained here: msdn.microsoft.com/en-us/library/azure/hh472157.aspxGreg D

1 Answers

0
votes

It's not very intuitive, but you have to cancel the scaling event, otherwise this will tell Azure to reboot other instances. Add the following line to the OnStart Method in Your RoleEntryPoint file:

RoleEnvironment.Changing += (sender, args) => { args.Cancel = false; };