调用两次时,Connect-ServiceFabricCluster调用失败

时间:2016-06-08 23:36:32

标签: powershell azure azure-powershell

我正在创建一个脚本,我可以使用该脚本将Azure Service Fabric应用程序部署到本地群集或Azure托管群集。如果我运行脚本两次,我遇到了问题。第二次执行脚本时Connect-ServiceFabricCluster命令失败。我可以重新启动PowerShell控制台或PowerShell ISE,直到第二次调用才会再次出现此问题。这导致了更改脚本的繁琐过程。

function Read-XmlElementAsHashtable
{
    Param (
        [System.Xml.XmlElement]
        $Element
    )

    $hashtable = @{}
    if ($Element.Attributes)
    {
        $Element.Attributes | 
            ForEach-Object {
                $boolVal = $null
                if ([bool]::TryParse($_.Value, [ref]$boolVal)) {
                    $hashtable[$_.Name] = $boolVal
                }
                else {
                    $hashtable[$_.Name] = $_.Value
                }
            }
    }

    return $hashtable
}

function Read-PublishProfile
{
    Param (
        [ValidateScript({Test-Path $_ -PathType Leaf})]
        [String]
        $PublishProfileFile
    )

    $publishProfileXml = [Xml] (Get-Content $PublishProfileFile)
    $publishProfile = @{}

    $publishProfile.ClusterConnectionParameters = Read-XmlElementAsHashtable $publishProfileXml.PublishProfile.Item("ClusterConnectionParameters")
    $publishProfile.UpgradeDeployment = Read-XmlElementAsHashtable $publishProfileXml.PublishProfile.Item("UpgradeDeployment")

    if ($publishProfileXml.PublishProfile.Item("UpgradeDeployment"))
    {
        $publishProfile.UpgradeDeployment.Parameters = Read-XmlElementAsHashtable $publishProfileXml.PublishProfile.Item("UpgradeDeployment").Item("Parameters")
        if ($publishProfile.UpgradeDeployment["Mode"])
        {
            $publishProfile.UpgradeDeployment.Parameters[$publishProfile.UpgradeDeployment["Mode"]] = $true
        }
    }

    $publishProfileFolder = (Split-Path $PublishProfileFile)
    $publishProfile.ApplicationParameterFile = [System.IO.Path]::Combine($PublishProfileFolder, $publishProfileXml.PublishProfile.ApplicationParameterFile.Path)

    return $publishProfile
}

$profileSettings = Read-PublishProfile $PublishProfileFile

try
{
    $clusterConnectionParameters = $profileSettings.ClusterConnectionParameters
    [void](Connect-ServiceFabricCluster @clusterConnectionParameters)
}
catch [System.Fabric.FabricObjectClosedException]
{
    Write-Warning "Service Fabric cluster may not be connected."
    throw
}

如果您查看了Visual Studio在创建Azure Service Fabric应用程序时创建的Deploy-FabricApplication.ps1脚本,则此代码应该看起来很熟悉,因为这是我在脚本中使用的源代码。

我第二次运行脚本时看到的错误消息是:

WARNING: Failed to contact Naming Service. Attempting to contact Failover Manager Service...
WARNING: Failed to contact Failover Manager Service, Attempting to contact FMM...
WARNING: Service Fabric cluster may not be connected.
Connect-ServiceFabricCluster : One or more errors occurred.
At MyScript.ps1:218 char:9
+     [void](Connect-ServiceFabricCluster @clusterConnectionParameters)
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Connect-ServiceFabricCluster], AggregateException
    + FullyQualifiedErrorId : CreateClusterConnectionErrorId,Microsoft.ServiceFabric.Powershell.ConnectCluster

My Local.xml Publish Profile,我没有对它进行任何更改,它是Visual Studio在创建项目时创建的默认文件。

<?xml version="1.0" encoding="utf-8"?>
<PublishProfile xmlns="http://schemas.microsoft.com/2015/05/fabrictools">
  <ClusterConnectionParameters />
  <ApplicationParameterFile Path="..\ApplicationParameters\Local.xml" />
</PublishProfile>

我在看到问题时采取的步骤:

  1. 运行脚本以进行部署
  2. 验证一切正常
  3. 重置本地群集
  4. 运行脚本以再次部署
  5. 脚本遇到错误
  6. 多次连接到Azure中的群集时,只有连接到本地群集时才会看到此问题。

    在第二次运行中没有遇到问题,我有什么遗漏吗?我想知道我是否缺少断开连接命令,因为重启会清除任何保留的状态,但是我没有发现任何与服务结构集群命令断开的情况。

    我确实在SO上发现了this post,这似乎是同样的错误,但重现问题的路径是不同的。

1 个答案:

答案 0 :(得分:1)

要解决此问题,我在Local.xml Publish Profile

中指定了ConnectionEndpoint
<?xml version="1.0" encoding="utf-8"?>
<PublishProfile xmlns="http://schemas.microsoft.com/2015/05/fabrictools">
  <ClusterConnectionParameters ConnectionEndpoint="localhost:19000" />
  <ApplicationParameterFile Path="..\ApplicationParameters\Local.xml" />
</PublishProfile>