在运行时获取服务版本

Get service version at runtime

如何获取 运行 时刻正在执行的 Service Fabric 服务(和应用程序)版本?我试过上下文,但 StatefulServiceContextStatelessServiceContext 都没有提供该信息。

也许我误解了你的问题,但是 this.Context.CodePackageActivationContext.CodePackageVersion 呢?

您可以使用 FabricClient 获取此信息。

适用版本:

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;
}

对于服务版本-

来自服务 class:

Context.CodePackageActivationContext.GetServiceManifestVersion()

或者:

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;
}

备注:

  • 在升级期间,您将使用此 API 获得旧版本。如果您需要新版本,请使用 FabricClient.ApplicationManager.GetApplicationUpgradeProgressAsync 并检索 TargetApplicationTypeVersion
  • 如果您经常使用 FabricClient,您可能需要缓存它(参见 remarks here
  • CodePackageActivationContext 还包含 CodePackageVersion,与服务清单的版本不同

查找应用程序版本:

var fabricClient = new FabricClient();
var applicationTypeList = await fabricClient.QueryManager.GetApplicationTypeListAsync(
                             Context.CodePackageActivationContext.ApplicationTypeName);
var applicationType = applicationTypeList.FirstOrDefault();
var applicationVersion = applicationType?.ApplicationTypeVersion;

您也可以使用 PowerShell。要获取应用程序类型版本:

Get-ServiceFabricApplication -ApplicationName fabric:/MyApplication | Select -expand ApplicationTypeVersion

获取服务清单版本:

Get-ServiceFabricService -ApplicationName fabric:/MyApplication -ServiceName fabric:/MyApplication/MyStatefulService | Select -expand ServiceManifestVersion