How to access applicationtypename specified in applicationmanifest in c#code

74 Views Asked by At

How do I access ApplicationTypeName specified in the applicationmanifest in c#code at runtime

I tried var applicationTypeName = FabricRuntime.GetActivationContext().ApplicationTypeName; var applicationName = FabricRuntime.GetActivationContext().ApplicationName; if (tracer != null) { tracer.Log($"applicationTypeName:{applicationTypeName}"); tracer.Log($"applicationName:{applicationName}"); } But this is giving me service nametype not application nametype.

<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">

In this example, I want xyz to be fetched.

1

There are 1 best solutions below

0
Suresh Chikkam On
  • In Service Fabric services, the ApplicationTypeName specified in the application manifest is not directly accessible from within the service code.

We can dynamically retrieve and inspect the application manifest at runtime.

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}");
        }
    }
}
  • Using context.CodePackageActivationContext directly to access the activation context within the StatelessService.

Here check this SO link, he used FabricClient to query for the application manifest and then deserialize it into an object based on the XSD schema. By this he is able to access properties of that object, such as 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>