0

我有一个带有 Web 角色和 Worker 角色的 Windows Azure 云服务。我建立了一个网站,允许我对云服务执行各种管理功能(停止/启动、重启实例、添加实例、删除实例)。所有功能均通过 web api 执行。我的问题是,当我添加 Web 角色的实例时,辅助角色会重新启动。请注意,如果我通过 Azure 门户添加实例,则不会发生这种情况。该代码在所有其他方面都能正常运行。知道如何做到这一点,以便只有受影响的角色回收而不是所有角色回收?

我的代码:

    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 );
    }
4

1 回答 1

0

这不是很直观,但是您必须取消缩放事件,否则这将告诉 Azure 重新启动其他实例。将以下行添加到 RoleEntryPoint 文件中的 OnStart 方法:

RoleEnvironment.Changing += (sender, args) => { args.Cancel = false; };
于 2015-03-03T13:10:11.270 回答