0

I have a utility application which allows user to import/export data from various databases. Each new task is added by adding a custom config section in app.config as shown below. Over the time the app.config file has grown very large & to keep things simple, I wanted user to be able to add new task in separate file. I have read options where appSettings or connectionString settings can be moved to different config file by specifying configSource attribute. Is it possible to do the same for custom sections?

Example of custom section which I use internally for task configuration.

  <configSections>
    <sectionGroup name="tasks">
      <sectionGroup name="ImportData">
        <section name="transferType" type="System.Configuration.NameValueSectionHandler"/>
        <section name="srcConnectionString" type="System.Configuration.NameValueSectionHandler"/>
        <section name="srcProcedure" type="System.Configuration.NameValueSectionHandler"/>
        <section name="destConnectionString" type="System.Configuration.NameValueSectionHandler"/>
        <section name="destTable" type="System.Configuration.NameValueSectionHandler"/>
      </sectionGroup>
    </sectionGroup>
  </configSections>
  <tasks>
    <ImportData>
      <transferType>
        <add key="transferType" value="database" />
      </transferType>
      <srcConnectionString>
        <add key="srcConnectionString" value="sourceDB" />
      </srcConnectionString>
      <srcProcedure>
        <add key="srcProcedure" value="sourceProcedureName" />
      </srcProcedure>
      <destConnectionString>
        <add key="destConnectionString" value="destConnection" />
      </destConnectionString>
      <destTable>
        <add key="destTable" value="TableName" />
      </destTable>
    </ImportData>
  </tasks>

The section value is read like

NameValueCollection transferSection = (NameValueCollection)ConfigurationManager.GetSection("ImportData/transferType");

How to add new sections in separate file & still continue to be able read the same way.

Jay
  • 408
  • 2
  • 6
  • 27
  • this might help https://stackoverflow.com/questions/11121135/is-it-possible-to-override-both-appsettings-and-connectionstrings-in-a-local-con?rq=1 – Bombinosh Apr 02 '19 at 11:48
  • @Bombinosh yes, I saw that already. The connectionStrings has configSource attribute where they have specified the external file. I wanted same with Sections – Jay Apr 02 '19 at 12:02
  • You can do the same for custom section https://stackoverflow.com/questions/5713147/custom-configurationsection-to-external-config – Bombinosh Apr 02 '19 at 13:36
  • @Bombinosh thanks for sharing the link. I think I can make use of it. – Jay Apr 02 '19 at 13:44

1 Answers1

1

This isn't feasible out of the box according to .NET's documentation.

An alternative would be to restructure your web.config:

<configSections>
    <sectionGroup name="tasks">
        <section name="import" />
        <section name="export" />
    </sectionGroup>
</configSections>
<tasks>
    <import configSource="import.config" />
    <export configSource="export.config" />
</tasks>

You can then add your custom import/export tasks in those external files.

TimAlonso
  • 488
  • 4
  • 19
  • Thanks, I will try to work it around. Since its a old utility, I would not want to have separate config file for each task, ( I have 45 so far & more to come). I wanted to break it such a way to have max 10 tasks in one config file, making it manageable – Jay Apr 02 '19 at 12:31