CrnaStena CrnaStena - 1 month ago 20
C# Question

Custom ConfigurationSection implementation for Service Fabric

Is there an equivalent to

System.Configuration.ConfigurationSection
in Service Fabric?

I am trying to port existing application to Service Fabric and I cannot find any examples of how to implement configuration sections.

For example if I have in
app.config
:

<configSections>
<sectionGroup name="MySection" type="Company.Core.MyConfiguration">
<section name="Watermark" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>

<MySection>
<Watermark>
<add key="FileName" value="some.png" />
</Watermark>
</MySection>


And implementation of
ConfigurationSection
:

public class MyConfiguration : ConfigurationSection
{
private readonly Watermark _watermark = new Watermark();

public WatermarkConfiguration WatermarkConfiguration
{
get
{
return new WatermarkConfiguration
{
FileName = _watermark.FileName
};
}
}

public class Watermark
{
private readonly NameValueCollection _watermark =
ConfigurationManager.GetSection("MySection/Watermark") as NameValueCollection;

[ConfigurationProperty("FileName")]
public string FileName
{
get { return _watermark["FileName"]; }
set { _watermark["FileName"] = value; }
}
}
}

public class WatermarkConfiguration
{
public string FileName { get; set; }
}


Then in the code I can reference this configuration like:

var myConfiguration = new MyConfiguration();
var watermarkName = myConfiguration.WatermarkConfiguration.FileName;


I am hoping someone has come across this before and could share an insight into their implementation changes.

Answer

No, it doesn't have extendable ConfigurationSections. Service config is done through configuration packages in Service Fabric - basically a version-able set of config files which is part of your overall service package that you deploy to a cluster (see here for more info on this structure).

The config package contains whatever config files you want in any format - XML, JSON, YAML, whatever. There is one special config file you can use called Settings.xml. Service Fabric has C# APIs that look a lot like the System.Configuration classes to access settings in that file.

For example, give this config in Settings.xml:

<?xml version="1.0" encoding="utf-8" ?>
<Settings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2011/01/fabric">

  <Section Name="IoTHubConfigInformation">
    <Parameter Name="ConnectionString" Value="blah" />
  </Section>

</Settings>

You would do this:

string iotHubConnectionString =
            this.Context.CodePackageActivationContext
                .GetConfigurationPackageObject("Config")
                .Settings
                .Sections["IoTHubConfigInformation"]
                .Parameters["ConnectionString"]
                .Value;

But you can't define your own sections like you do with System.Configuration. Instead, you'll have to provide some wrapper classes.