CrnaStena CrnaStena - 1 year ago 150
C# Question

Custom ConfigurationSection implementation for Service Fabric

Is there an equivalent to

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

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

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

And implementation of

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

public WatermarkConfiguration WatermarkConfiguration
return new WatermarkConfiguration
FileName = _watermark.FileName

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

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 Source

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="" xmlns:xsi="" xmlns="">

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


You would do this:

string iotHubConnectionString =

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download