Azure Service Fabric - Application Settings

So far, I got used to store and retrieve the application settings with the following code:



ConfigurationManager.AppSettings[“StorageConnectionString”]
CaseInsensitiveConfigurationManager.AppSettings.Get("StorageConnectionString")
CloudConfigurationManager.GetSetting("StorageConnectionString")


And other variations...



With Azure Service Fabric (ASF) it is a bit more involved, but offers finer granularity over your application settings. The following describes how to store and subsequently retrieve, the application settings in ASF (I added some colors so it will be easier to read. I hope that you are not color blind :-)




Storing values:




In ~\ApplicationPackageRoot\ApplicationManifest.xml


<Parameters>
  <Parameter Name="StorageConnectionString" DefaultValue=" Your Default Value" />
</Parameters>



The Service Fabric application model enables services to include configuration packages that contain custom key-value pairs that are readable at run time. The values of these settings can also be differentiated by environment by specifying a ConfigOverride in the application manifest (source). For the order in which ASF overrides application setting, see the comment at the end.


<ServiceManifestVersion="1.0.0" />
  <ConfigOverrides>
     <ConfigOverride Name="Config">
        <Settings>
          <Section Name="StorageConfig">
          <Parameter Name="StorageConnectionString" Value="[StorageConnectionString]" />
        </Section>
        </Settings>
      </ConfigOverride>
    </ConfigOverrides>
  </ServiceManifestImport>
     <ServiceManifestImport>
  <ServiceManifestRef ServiceManifestName="YourPkg" ServiceManifestVersion="1.0.0" />
</ServiceManifestImport>






In ~\ApplicationParameters\Cloud.xml


<Parameters>
  <Parameter Name="StorageConnectionString" Value="your value" />
</Parameters>


You can add the same to your local XMLs (Local.1Node.xml & Local.5Node.xml)




Retrieving values:


Within your actor / service, you can retrieve a value this way:



var storageConfig = ActorService.Context.CodePackageActivationContext.GetConfigurationPackageObject("Config");
var storageConnectionString = storageConfig.Settings.Sections["StorageConfig"].Parameters["StorageConnectionString"].Value;




Important:

Service Fabric will always choose from the application parameter file first (if specified), then the application manifest, and finally the configuration package (source)

Comments