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

Connect-ServiceFabricCluster call fails when called twice

我正在创建一个脚本,我可以使用它来将我的 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

我的 Local.xml 发布配置文件,我没有对其进行任何更改,它是我创建项目时创建的默认文件 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. 脚本遇到错误

我在多次连接到 Azure 中的集群时没有看到这个问题,只有在连接到我的本地集群时才看到这个问题。

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

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

为了解决这个问题,我在 Local.xml 发布配置文件

中指定了 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>