如何获取当前 Service Fabric 应用程序的名称?
How can I get the name of the name of the current Service Fabric application?
是否可以获取我的代码所在的 Service Fabric 应用程序的名称运行?
例如,我的 ApplicationParameters 文件有这个:
<Application xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="fabric:/mytestapp" xmlns="http://schemas.microsoft.com/2011/01/fabric">
我想检索 "mytestapp" 并在我的代码中使用它,但我还没有找到任何可以让我这样做的东西。有什么想法吗?
ApplicationName 在 CodePackageActivationContext 中作为字符串(不幸的是不是 URI)作为 属性 存在。
// System.Fabric.CodePackageActivationContext
public string ApplicationName
{
get;
internal set;
}
CodePackageActivationContext 存在于每个服务在其构造函数中传递的 ServiceContext 中。来自有状态服务的示例。
public StatefulService(StatefulServiceContext serviceContext)
: this(serviceContext, new ReliableStateManager(serviceContext, null))
{
}
如果您的服务是来宾或容器或不使用 ReliableServices API 的服务,那么将名称显式传递为配置设置可能是最好的选择。
在我的例子中,我尝试在项目之间(在我的例子中是有状态到无状态)传达上面的 ApplicationName
,但它出奇地痛苦,以至于不可能,因为有 由于不同的 VM 和节点不断变化,因此值不一致 。
我还尝试通过在外部项目的某些(静态)变量上设置一个值来在项目之间进行通信,但这没有用。
最后我还尝试将应用程序名称写入文本文件,但这只是暂时有效,因为每个节点都保留文本文件的异步副本。
我的解决方案 是利用 Environment.GetEnvironmentVariable("Fabric_ApplicationName")
,效果很好。
见here。
希望对您有所帮助。
是否可以获取我的代码所在的 Service Fabric 应用程序的名称运行?
例如,我的 ApplicationParameters 文件有这个:
<Application xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="fabric:/mytestapp" xmlns="http://schemas.microsoft.com/2011/01/fabric">
我想检索 "mytestapp" 并在我的代码中使用它,但我还没有找到任何可以让我这样做的东西。有什么想法吗?
ApplicationName 在 CodePackageActivationContext 中作为字符串(不幸的是不是 URI)作为 属性 存在。
// System.Fabric.CodePackageActivationContext
public string ApplicationName
{
get;
internal set;
}
CodePackageActivationContext 存在于每个服务在其构造函数中传递的 ServiceContext 中。来自有状态服务的示例。
public StatefulService(StatefulServiceContext serviceContext)
: this(serviceContext, new ReliableStateManager(serviceContext, null))
{
}
如果您的服务是来宾或容器或不使用 ReliableServices API 的服务,那么将名称显式传递为配置设置可能是最好的选择。
在我的例子中,我尝试在项目之间(在我的例子中是有状态到无状态)传达上面的 ApplicationName
,但它出奇地痛苦,以至于不可能,因为有 由于不同的 VM 和节点不断变化,因此值不一致 。
我还尝试通过在外部项目的某些(静态)变量上设置一个值来在项目之间进行通信,但这没有用。
最后我还尝试将应用程序名称写入文本文件,但这只是暂时有效,因为每个节点都保留文本文件的异步副本。
我的解决方案 是利用 Environment.GetEnvironmentVariable("Fabric_ApplicationName")
,效果很好。
见here。
希望对您有所帮助。