How to get the executing Service Fabric services (and application) versions at run time? I have tried the context, but both StatefulServiceContext and StatelessServiceContext do not provide that information.
You can use FabricClient to get this information.
For the application version:
var applicationName = new Uri("fabric:/MyApp"); // or use Context.CodePackageActivationContext.ApplicationName
using (var client = new FabricClient())
{
    var applications = await client.QueryManager.GetApplicationListAsync(applicationName).ConfigureAwait(false);
    var version = applications[0].ApplicationTypeVersion;
}
For service versions -
From within a service class:
Context.CodePackageActivationContext.GetServiceManifestVersion()
Or:
var serviceName = new Uri("fabric:/MyApp/MyService"); // or use Context.ServiceName
using (var client = new FabricClient())
{
    var services = await client.QueryManager.GetServiceListAsync(applicationName, serviceName).ConfigureAwait(false);
    var version = services[0].ServiceManifestVersion;
}
Notes:
FabricClient.ApplicationManager.GetApplicationUpgradeProgressAsync and retrive the TargetApplicationTypeVersionFabricClient if you use it often (see remarks here)CodePackageActivationContext also contains the CodePackageVersion, which is different than the version of the service manifestYou can also use PowerShell. To get the application type version:
Get-ServiceFabricApplication -ApplicationName fabric:/MyApplication | Select -expand ApplicationTypeVersion
To get the service manifest version:
Get-ServiceFabricService -ApplicationName fabric:/MyApplication -ServiceName fabric:/MyApplication/MyStatefulService | Select -expand ServiceManifestVersion
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With