Service Fabric Rest API to change Service Instance Count

PowerShell to achive it:

1
2
3
4
5
6
$clusterFQDN = "jackysf3.eastasia.cloudapp.azure.com"
$clusterEndpoint = $clusterFQDN+':19000'
$certThumbprint = (Get-ChildItem -Path Cert:\CurrentUser\My | where {$_.Subject -like "*$clusterFQDN*" }).Thumbprint
Connect-ServiceFabricCluster -ConnectionEndpoint $clusterEndpoint -KeepAliveIntervalInSec 10 -X509Credential -ServerCertThumbprint $certThumbprint -FindType FindByThumbprint -FindValue $certThumbprint -StoreLocation CurrentUser -StoreName My

Update-ServiceFabricService -Stateless fabric:/ServiceFabricApp1/Stateless1 -InstanceCount 5

Service Fabric Rest API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
static async Task MainAsync()
{
Func<CancellationToken, Task<SecuritySettings>> GetSecurityCredentials = (ct) =>
{
// get the X509Certificate2 either from Certificate store or from file.
var clientCert = new System.Security.Cryptography.X509Certificates.X509Certificate2(@"C:\Users\jchiou\jackysf320191031213903.pfx", "Password01!");
var remoteSecuritySettings = new RemoteX509SecuritySettings(new List<string> { "8AB797576F2EF93574631A6BD7C5B381C86A60C4" });
return Task.FromResult<SecuritySettings>(new X509SecuritySettings(clientCert, remoteSecuritySettings));
};

// create client using ServiceFabricClientBuilder.UseX509Security
var client = new ServiceFabricClientBuilder()
.UseEndpoints(new Uri(@"https://jackysf3.eastasia.cloudapp.azure.com:19080"))
.UseX509Security(GetSecurityCredentials)
.BuildAsync().GetAwaiter().GetResult();

await client.Services.UpdateServiceAsync("ServiceFabricApp1~Stateless1", new StatelessServiceUpdateDescription(instanceCount: 5, defaultMoveCost: Microsoft.ServiceFabric.Common.MoveCost.Zero, flags: "1"));

var serviceDesc = await client.Services.GetServiceDescriptionAsync("ServiceFabricApp1~Stateless1");

}

Make sure to add “Flags” parameter, without it you will get HTTP 200 and nothing change.

Flags indicating whether other properties are set. Each of the associated properties corresponds to a flag, specified below, which, if set, indicate that the property is specified. This property can be a combination of those flags obtained using bitwise ‘OR’ operator. For example, if the provided value is 6 then the flags for ReplicaRestartWaitDuration (2) and QuorumLossWaitDuration (4) are set.

For more information:
https://docs.microsoft.com/en-us/rest/api/servicefabric/sfclient-model-statefulserviceupdatedescription#flags

https://github.com/microsoft/service-fabric-client-dotnet

Enjoy. 2019-11-1 By Jacky