简体   繁体   English

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

[英]How can I get the name of the name of the current Service Fabric application?

Is it possible to get the name of the Service Fabric application where my code is running? 是否可以获取运行我的代码的Service Fabric应用程序的名称?

For example, my ApplicationParameters file has this: 例如,我的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">

I want to retrieve "mytestapp" and use it in my code, but I haven't found anything that would let me do that. 我想检索“mytestapp”并在我的代码中使用它,但我没有找到任何可以让我这样做的东西。 Any ideas? 有任何想法吗?

ApplicationName is present as a string (unfortunately not a URI) as a property on the CodePackageActivationContext. ApplicationName作为字符串(不幸的是不是URI)作为CodePackageActivationContext上的属性存在。

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

CodePackageActivationContext is present on the ServiceContext that every service is passed in its constructor. CodePackageActivationContext存在于ServiceContext上,每个服务都在其构造函数中传递。 Example from stateful services. 有状态服务的示例。

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

If your service is a guest or a container or something that doesn't use the ReliableServices API, then passing the name explicitly as a config setting is probably your best bet. 如果您的服务是访客或容器或不使用ReliableServices API的东西,那么明确地将名称作为配置设置传递可能是您最好的选择。

In my case, I tried communicating the above ApplicationName between projects (Stateful to Stateless in my case), but it was surprisingly painful, to the point it was impossible because there is inconsistency in the value due to the different VM's and nodes being constantly changing. 在我的例子中,我尝试在项目之间传递上述ApplicationName (在我的情况下是Stateful to Stateless),但是令人惊讶的是,它是不可能的,因为由于不同的VM和节点不断变化导致值不一致

I also tried communicating between projects by setting a value on some (static) variables of an external project, that didn't work. 我还尝试通过在外部项目的某些(静态)变量上设置值来实现项目之间的通信,这些变量不起作用。

Lastly I also tried writing the application name to a text file, but this only worked temporarily, because each node keeps an async copy of the text file. 最后,我还尝试将应用程序名称写入文本文件,但这仅暂时起作用,因为每个节点都保留文本文件的异步副本。

My solution was to make use of Environment.GetEnvironmentVariable("Fabric_ApplicationName") , which worked perfectly. 我的解决方案是使用Environment.GetEnvironmentVariable("Fabric_ApplicationName") ,它完美地运行。

See here . 看到这里

I hope this helps. 我希望这有帮助。

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

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