Search code examples
c#.netvisual-studio-2017microservicesazure-service-fabric

Can a non-SF project make use of the applicationmanifest configuration values?


I've got a Service Fabric Application set up in the following way:

Solution
--SF Project
  --ApplicationManifest.xml
--Stateless Project (uses app manifest values)
--Stateless Project (uses app manifest values)
--Class Library (used as a repository by the above two projects)

How can I enable the class library to make use of the ApplicationManifest.xml configuration file from the SF Project?

To allow the projects to be able to use the AppManifest for build/deployment, they simply need to be created like so:

enter image description here

How does a project that does not get added as a Service Fabric project make use of the applicationmanifest?

The Service Fabric projects are able to use the appmanifest by including parameters in settings.xml and servicemanifest (but non-SF projects cannot):

enter image description here


Solution

  • Option #1

    If you need to access the parameters defined in your service's setting.xml, the next should work -

    1. In your non-SF project, install Microsoft.Extensions.Configuration and ServiceFabric.Extensions.Configuration NuGet packages
    2. Wherever you decide to access the parameters, use the next code snippet -

      var builder = new ConfigurationBuilder().AddFabricConfiguration("Config");
      var configuration = builder.Build();
      var section = configuration.GetSection("MyConfigSection");
      var parameterValue = section["MyParameter"];
      

    One note though - you will get access to only one SF service at a time. That's because AddFabricConfiguration() works by calling FabricRuntime.GetActivationContext(), which ties settings being loaded with the SF service you're calling a non-SF code from.

    Option #2

    Next option will work from any place where you could establish connection with the SF. Using code below, you could read any parameter passed into the app manifest -

    var fClient = new FabricClient();
    var namespaceManager = new XmlNamespaceManager(new NameTable());
    namespaceManager.AddNamespace("ns", "http://schemas.microsoft.com/2011/01/fabric");
    var manifest = XDocument.Parse(fClient.ApplicationManager.GetApplicationManifestAsync("YOUR_APP_TYPE_NAME", "YOUR_APP_TYPE_VERSION").Result);         
    var parameterValue = manifest.XPathSelectElement("/ns:ApplicationManifest/ns:Parameters/ns:Parameter[@Name='PARAMETER_NAME']", namespaceManager).Attribute("DefaultValue").Value;