如何在c#代码中访问applicationmanifest中指定的applicationtypename

问题描述 投票:0回答:1

如何在运行时访问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。

c# .net azure service azure-service-fabric
1个回答
0
投票
  • 在 Service Fabric 服务中,应用程序清单中指定的
    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>
© www.soinside.com 2019 - 2024. All rights reserved.