0
votes

I developed a Windows service. It uses a MyService.exe.config file for configuration, that looks like this (simplified with just one setting, Prop1):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="MyNamespace.MyService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <applicationSettings>
    <MyNamespace.MyService.Properties.Settings>

      <setting name="Prop1" serializeAs="String">
        <value>Foo</value>
      </setting>

    </MyNamespace.MyService.Properties.Settings>
  </applicationSettings>
</configuration>

When I deploy to a customer production environment I need to add more settings manually on the config file, for instance Prop2:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="MyNamespace.MyService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <applicationSettings>
    <MyNamespace.MyService.Properties.Settings>

      <setting name="Prop1" serializeAs="String">
        <value>Foo</value>
      </setting>

      <setting name="Prop2" serializeAs="String">
        <value>Bar</value>
      </setting>

    </MyNamespace.MyService.Properties.Settings>
  </applicationSettings>
</configuration>

Now if I start the service this lines of code:

log.Debug(Properties.Settings.Default["Prop1"].ToString());
log.Debug(Properties.Settings.Default["Prop2"].ToString());

produce following output:

Foo
Impossibile trovare la proprietà di impostazione 'Prop2'.
   in System.Configuration.SettingsBase.GetPropertyValueByName(String propertyName)
   in System.Configuration.SettingsBase.get_Item(String propertyName)
   in System.Configuration.ApplicationSettingsBase.GetPropertyValue(String propertyName)
   in System.Configuration.ApplicationSettingsBase.get_Item(String propertyName)
   in ...

The error in Italian means "Cannot find settings property 'Prop2'".

How can I read settings added to app.config after compile time?

I'm wondering whether it is not possible to add new settings to app.config when the application is already deployed, because every setting must be compiled and made available statically in Properties.Settings.Default. So to achieve what I want should I use a settings file managed by me, like re-inventing the wheel?

1
what config did you changed? Root one or the one in bin folder?xszaboj
I don't know what you mean. I changed MyService.exe.config, which resides in the same folder as MyService.exe.bluish
that should be fine than... Did you restart the service after config change?xszaboj
Yes, I edited it with the service stopped and then I started it.bluish
I assume that you know what config options are to be added?Aamir Masood

1 Answers

-1
votes

Well, I had posted an idea I thought would work but went back and tested myself and it didn't. D***.

So here's a suggestion I do use when the regular Settings stuff doesn't work.

Create an XML file structured as DataSet (the easiest way to do this is write a short program that creates a DatSet, populates it, and saves it as XML.

DataSet mydataset = new DataSet("myDataSet");
DataTable mydatatable = new DataTable("myDataTable");
mydatatable.Columns.Add(new DataColumn("myColumn1"));
DataRow newrow = mydatatable.NewRow();
newrow["myColumn1"] = "somedata";
mydatatable.Rows.Add(newrow);
mydataset.Tables.Add(mydatatable);
mydataset.WriteXML(@"C:\mydataset.xml", XmlWriteMode.WriteSchema);

This creates your basic XML file.

Very Important!!!!! To serialize a DataSet and/or DataTable you MUST give them names when you create them:

new DataSet("mydataset")

Then, as long as you don't violate the integrity of the XML structure, you can add rows (and even columns) to you XML file any time you wish using your favorite text editor.

In your application you simply read the XML file back into a DataSet and iterate through your DataTable rows (and through each DataTable if you created more than one) and process accordingly.

DataSet myDataSet = new DataSet("mydataset");
myDataSet.ReadXml(@"C:\mydataset.xml")
foreach (DataTable t in myDataSet.Tables)
{
    foreach (DataRow r in t.Rows)
    {
        //process here
    }            
}

Very Important!!!!! To process the data you MUST use the names for the DataSet and DataTable from the XML file.

Hope this helps. It has worked for me many times when I couldn't use or couldn't access the built-in settings handler. It does add a little bit more time to your coding but....

John