添加Web角色实例时,Azure Worker Role会重新启动

时间:2015-03-02 22:12:40

标签: azure azure-web-roles azure-worker-roles azure-cloud-services azure-api-management

我有一个带有Web角色和辅助角色的Windows Azure云服务。我建立了一个网站,允许我对云服务执行各种管理功能(停止/启动,重启实例,添加实例,删除实例)。所有功能都通过web api执行。我的问题是,当我添加Web角色的实例时,worker角色会重新启动。请注意,如果我通过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 );
    }

1 个答案:

答案 0 :(得分:0)

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

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