在运行时获取服务版本

4

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


您想获取部署到ASF的所有服务/应用程序列表,还是仅获取当前代码正在执行的服务/应用程序? - Philip Pittle
在当前的一个中。下面的答案总结了它。对于应用程序必须查询。 - Sean Feldman
4个回答

7
您可以使用 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;
}

针对服务版本 -

在一个服务类内部:

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,可以考虑缓存它(请参见此处的说明
  • CodePackageActivationContext还包含CodePackageVersion,这与服务清单的版本不同

1
ServiceManifestVersionCodePackageVersion是一个很好的观点。因此,对于服务版本,为什么不使用Context.CodePackageActivationContext.GetServiceManifestVersion()呢? - Sean Feldman
@EliArbel "您可能想要缓存FabricClient" - 客户端创建成本高吗? - Philip Pittle
@PhilipPittle 来自 MSDN强烈建议您尽可能共享 FabricClients。这是因为 FabricClient 具有多个优化功能,例如缓存和批处理,否则您将无法充分利用它们。 - Eli Arbel

2
您可以使用PowerShell。获取应用程序类型的版本:
Get-ServiceFabricApplication -ApplicationName fabric:/MyApplication | Select -expand ApplicationTypeVersion

获取服务清单版本:

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

0
也许我误解了你的问题,但是 this.Context.CodePackageActivationContext.CodePackageVersion 怎么样?

那是应用程序版本还是服务版本? - Sean Feldman
那是服务版本。 - Raghu
应用程序的等价物是什么?考虑到应用程序与其服务是分开认证的,一定有相应的东西。 - Sean Feldman
我已经发布了如何发现应用程序版本的方法,尽管感觉有些hacky,但在SF应用程序下服务不能无查询集群的情况下检索应用程序版本。 - Sean Feldman

0

查找应用程序版本:

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

这里可能会得到不准确的结果。列表中的第一项可能不是当前实例所使用的应用程序版本。看看我的回答。 - Eli Arbel

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