如何获取当前Service Fabric应用程序的名称?

bwm*_*ens 3 c# azure-service-fabric

是否可以获取运行我的代码的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">
Run Code Online (Sandbox Code Playgroud)

我想检索"mytestapp"并在我的代码中使用它,但我没有找到任何可以让我这样做的东西.有任何想法吗?

mas*_*der 5

ApplicationName作为字符串(不幸的是不是URI)作为CodePackageActivationContext上的属性存在.

// System.Fabric.CodePackageActivationContext
public string ApplicationName
{
    get;
    internal set;
}
Run Code Online (Sandbox Code Playgroud)

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

public StatefulService(StatefulServiceContext serviceContext)
    : this(serviceContext, new ReliableStateManager(serviceContext, null))
{
}
Run Code Online (Sandbox Code Playgroud)

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