繁体   English   中英

如何获取当前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作为字符串(不幸的是不是URI)作为CodePackageActivationContext上的属性存在。

// System.Fabric.CodePackageActivationContext
public string ApplicationName
{
    get;
    internal set;
}

CodePackageActivationContext存在于ServiceContext上,每个服务都在其构造函数中传递。 有状态服务的示例。

public StatefulService(StatefulServiceContext serviceContext)
    : this(serviceContext, new ReliableStateManager(serviceContext, null))
{
}

如果您的服务是访客或容器或不使用ReliableServices API的东西,那么明确地将名称作为配置设置传递可能是您最好的选择。

在我的例子中,我尝试在项目之间传递上述ApplicationName (在我的情况下是Stateful to Stateless),但是令人惊讶的是,它是不可能的,因为由于不同的VM和节点不断变化导致值不一致

我还尝试通过在外部项目的某些(静态)变量上设置值来实现项目之间的通信,这些变量不起作用。

最后,我还尝试将应用程序名称写入文本文件,但这仅暂时起作用,因为每个节点都保留文本文件的异步副本。

我的解决方案是使用Environment.GetEnvironmentVariable("Fabric_ApplicationName") ,它完美地运行。

看到这里

我希望这有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM