如何在运行时访问c#代码中applicationmanifest中指定的ApplicationTypeName
我尝试了
var applicationTypeName = FabricRuntime.GetActivationContext().ApplicationTypeName; var applicationName = FabricRuntime.GetActivationContext().ApplicationName; if (tracer != null) { tracer.Log($"applicationTypeName:{applicationTypeName}"); tracer.Log($"applicationName:{applicationName}"); }
但这给了我服务名称类型而不是应用程序名称类型。
<ApplicationManifest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ApplicationTypeName="xyz" ApplicationTypeVersion="1.0.0" xmlns="http://schemas.microsoft.com/2011/01/fabric">
在此示例中,我希望获取 xyz。
ApplicationTypeName
无法从服务代码中直接访问。我们可以在运行时动态检索和检查应用程序清单。
using System.Fabric;
class YourService : StatelessService
{
public YourService(StatelessServiceContext context)
: base(context)
{
try
{
var activationContext = context.CodePackageActivationContext;
if (activationContext != null)
{
var applicationTypeName = activationContext.ApplicationTypeName;
ServiceEventSource.Current.ServiceMessage(this, $"ApplicationTypeName: {applicationTypeName}");
}
else
{
// Log an error or handle the null activationContext case
ServiceEventSource.Current.ServiceMessage(this, "Error: Activation context is null.");
}
}
catch (Exception ex)
{
// Log an error or handle the exception
ServiceEventSource.Current.ServiceMessage(this, $"An error occurred: {ex.Message}");
}
}
}
context.CodePackageActivationContext
访问 StatelessService
中的激活上下文。这里检查这个SOlink,他使用
FabricClient
来查询应用程序清单,然后将其反序列化为基于XSD架构的对象。通过这种方式,他能够访问该对象的属性,例如 ApplicationTypeName
。
ServiceManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<ServiceManifest ...>
<ServiceTypes>
<StatelessServiceType ...>
<!-- Other configuration settings -->
<ConfigurationOverrides>
<ConfigOverrides Name="Config">
<Settings>
<Section Name="ApplicationInfo">
<Parameter Name="ApplicationTypeName" Value="xyz" />
</Section>
</Settings>
</ConfigOverrides>
</ConfigurationOverrides>
</StatelessServiceType>
</ServiceTypes>
<!-- ... -->
</ServiceManifest>