14

I am trying to publish Service Fabric application to Azure with powershell. I want to connect to the cluster and then call script "Deploy-FabricApplication.ps1"(one that is generated when new project is created in visual studio)

To do that I have created new script file which is responsible for connecting with cluster and then from that same script file I am calling "Deploy-FabricApplication.ps1".

After launching my script I am getting following error:

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

In connection script and Deploy-FabricApplication.ps1 I am calling "Test-ServiceFabricClusterConnection" and in both scripts it returns True also in both scripts I am calling "Get-ServiceFabricClusterManifest" and in both cases it returns to me manifest but when I have added "Test-ServiceFabricClusterConnection" to "Publish-UpgradedServiceFabricApplication.ps1" then it has returned that same error as mentioned befor but for "Test-ServiceFabricClusterConnection" call.

Code for connecting script:

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

To summarize, I have no idea how to establish connection to cluster and then use it in Deploy-FabricApplication.ps1

Thanks for help

TomaszMaryniak
  • 198
  • 1
  • 8

1 Answers1

25

When calling Connect-ServiceFabricCluster a local $clusterConnection variable is set. Some of the SDK scripts then expect that variable to be set, but because you're executing your script in different scope, that local variable isn't available to them.

You can either dot source the call to Deploy-FabricApplication.ps1

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

or create a $global:clusterConnection variable that contains the local $clusterConnection variable

$global:clusterConnection = $clusterConnection
charisk
  • 3,190
  • 2
  • 24
  • 18
  • 1
    Thanks for help, I have assigned local variable to global and it looks that now it works – TomaszMaryniak Jun 30 '16 at 09:17
  • If you then reset your local cluster, and try and re-use the connection you get `Error: ...Exception: The object is closed. --> Exception from HRESULT: 0x80071BFE`. Starting a new powershell window fixes it. – Tim Abell Feb 13 '17 at 09:29
  • And running `Connect-ServiceFabricCluster` after a reset gets you: `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 Feb 13 '17 at 09:39
  • SF troubleshooting guide https://github.com/Microsoft/azure-docs/blob/master/articles/service-fabric/service-fabric-troubleshoot-local-cluster-setup.md - includes object closed error. – Tim Abell Feb 13 '17 at 10:14
  • related question for post-reset fail http://serverfault.com/questions/832238/connect-servicefabriccluster-doesnt-work-after-reset-local-cluster – Tim Abell Feb 13 '17 at 11:32