使用PowerShell部署Service Fabric。错误-> Get-ServiceFabricClusterManifest:集群连接实例为空。

14

我正在尝试使用PowerShell将Service Fabric应用程序发布到Azure。我想连接到集群,然后调用脚本“Deploy-FabricApplication.ps1”(在Visual Studio中创建新项目时生成的脚本之一)

为此,我创建了一个新的脚本文件,负责连接到集群,然后从同一个脚本文件中调用“Deploy-FabricApplication.ps1”。

启动我的脚本后,我得到以下错误:

Get-ServiceFabricClusterManifest : Cluster connection instance is null
At C:\Program Files\Microsoft SDKs\Service Fabric\Tools\PSModule\ServiceFabricSDK\Publish-UpgradedServiceFabricApplication.ps1:116 char:28
+     $clusterManifestText = Get-ServiceFabricClusterManifest
+                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ResourceUnavailable: (:) [Get-ServiceFabricClusterManifest], NullReferenceException
+ FullyQualifiedErrorId : GetClusterConnectionErrorId,Microsoft.ServiceFabric.Powershell.GetClusterManifest
在连接脚本和Deploy-FabricApplication.ps1中,我调用了“Test-ServiceFabricClusterConnection”,在这两个脚本中它都返回True。此外,在这两种情况下,我也调用了“Get-ServiceFabricClusterManifest”,并且都返回给我清单。但是,当我将“Test-ServiceFabricClusterConnection”添加到“Publish-UpgradedServiceFabricApplication.ps1”时,它返回了与之前提到的相同错误,但是针对“Test-ServiceFabricClusterConnection”的调用。
连接脚本的代码:
Param
(
    [String]
    $PublishProfileFile,

    [String]
    $ApplicationPackagePath,

    [Switch]
    $DeployOnly,

    [Boolean]
    $UnregisterUnusedApplicationVersionsAfterUpgrade,

    [String]
    [ValidateSet('None', 'ForceUpgrade', 'VetoUpgrade')]
    $OverrideUpgradeBehavior = 'None',

    [String]
    [ValidateSet('Never','Always','SameAppTypeAndVersion')]
    $OverwriteBehavior = 'Never',

    [Switch]
    $SkipPackageValidation,

    [String]
    $ConnectionEndpoint,

    [String]
    $ServerCertThumbprint,

    [String]
    $FindType,

    [String]
    $FindValue,

    [String]
    $StoreLocation,

    [String]
    $StoreName
)

$connectArgs = @{ ConnectionEndpoint = $ConnectionEndpoint;  X509Credential = $True;  StoreLocation = $StoreLocation;  StoreName = $StoreName;  ServerCertThumbprint = $ServerCertThumbprint; FindType = $FindType;  FindValue = $FindValue }

try
{
    Connect-ServiceFabricCluster @connectArgs;
    $connection = Get-ServiceFabricClusterConnection;
    Write-Host $connection;
    $m = Get-ServiceFabricClusterManifest
    Write-Host $m;
}
catch [System.Fabric.FabricObjectClosedException]
{
    Write-Warning "Service Fabric cluster may not be connected."
    throw
}

.\Deploy-FabricApplication.ps1 -PublishProfileFile $PublishProfileFile -ApplicationPackagePath $ApplicationPackagePath -OverrideUpgradeBehavior $OverrideUpgradeBehavior -OverwriteBehavior $OverwriteBehavior -DeployOnly:$DeployOnly -UnregisterUnusedApplicationVersionsAfterUpgrade:$UnregisterUnusedApplicationVersionsAfterUpgrade -UseExistingClusterConnection:$true -SkipPackageValidation:$SkipPackageValidation
总之,我不知道如何连接到集群并在Deploy-FabricApplication.ps1中使用它。 谢谢帮助。
1个回答

25

调用Connect-ServiceFabricCluster时,会设置本地的$clusterConnection变量。然后,一些SDK脚本期望该变量被设置,但由于您在不同的作用域中执行脚本,所以这个本地变量对它们不可用。

您可以点运行调用Deploy-FabricApplication.ps1。

. .\Deploy-FabricApplication.ps1 -PublishProfileFile $PublishProfileFile -ApplicationPackagePath $ApplicationPackagePath -OverrideUpgradeBehavior $OverrideUpgradeBehavior -OverwriteBehavior $OverwriteBehavior -DeployOnly:$DeployOnly -UnregisterUnusedApplicationVersionsAfterUpgrade:$UnregisterUnusedApplicationVersionsAfterUpgrade -UseExistingClusterConnection:$true -SkipPackageValidation:$SkipPackageValidation

或者创建一个$global:clusterConnection变量,其中包含本地的$clusterConnection变量。

$global:clusterConnection = $clusterConnection

1
谢谢你的帮助,我已经将局部变量赋值给全局变量,现在看起来它运行正常。 - TomaszMaryniak
如果您重置本地集群,然后尝试重新使用连接,则会出现“错误:...异常:对象已关闭。--> HRESULT 异常:0x80071BFE”。打开新的 PowerShell 窗口即可解决该问题。 - Tim Abell
在重置后运行 Connect-ServiceFabricCluster 会得到以下结果:Connect-ServiceFabricCluster : The object is closed. At line:1 char:1 + Connect-ServiceFabricCluster + CategoryInfo : InvalidOperation: (:) [Connect-ServiceFabricCluster], FabricObjectClosedException + FullyQualifiedErrorId : TestClusterConnectionErrorId,Microsoft.ServiceFabric.Powershell.ConnectCluster - Tim Abell
SF故障排除指南 https://github.com/Microsoft/azure-docs/blob/master/articles/service-fabric/service-fabric-troubleshoot-local-cluster-setup.md - 包括对象关闭错误。 - Tim Abell
与重置后连接失败相关的问题:http://serverfault.com/questions/832238/connect-servicefabriccluster-doesnt-work-after-reset-local-cluster - Tim Abell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接