簡體   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