app.config

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guinness Mann

    app.config

    I'm working in VS2003.NET, with C#.

    I use app.config to store the connection string to my SQL Server. This
    works fine.

    Lately I've been taking work home where the connection string is
    different. I deal with this by having app.config.home and
    app.config.work files which I copy over the app.config file as required
    by my location.

    I was wondering if there is another way to do this. In my Microsoft.NET
    file system hierarchy I notice a CONFIG directory with a machine.config
    file in it. Said machine.config file has an example appSettings section
    in it.

    Would that be an appropriate place to store my SQL Server connection
    string? How would I access it? I currently say
    'ConfigurationS ettings.AppSett ings["strCon"]'. How would that change?

    Thank you,

    -- Rick
  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: app.config

    Rick,
    I would recommend a single app.config file. Within this single app.config
    have custom sections that identify distinct 'environments' (home or work).

    There is a predefined configSections section in the app.config that you use
    to define new sections.

    Something like:

    <configuratio n>

    <configSections >
    <section name="myClass1S tuff"
    type="System.Co nfiguration.Dic tionarySectionH andler, System" />
    <sectionGroup name="environme nts">
    <section name="work"
    type="System.Co nfiguration.Sin gleTagSectionHa ndler, System" />
    <section name="home"
    type="System.Co nfiguration.Sin gleTagSectionHa ndler, System" />
    </sectionGroup>
    </configSections>

    <appSettings>
    <add key="key1" value="value1" />
    <add key="environmen t" value="work" />
    </appSettings>

    <myClass1Stuf f>
    <add key="key1" value="value1" />
    </myClass1Stuff>

    <environments >
    <work value1="xyz" value2="abc" value3="edf" value4="ghi" />
    <home value1="xyz" value2="abc" value3="edf" value4="ghi" />
    </environments>

    </configuration>

    If you inherit from DictionarySecti onHandler you can easily change the key
    or value in your section. I've derived from DictionarySecti onHandler to
    change key/value to plugin/type in a few of my projects.

    I use the above environments section to define each of my environments,
    Production, QA, Test, Development. Where each environment points to the
    correct database servers, web servers, message queues, printers any 'per
    environment' settings. The appSettings/environment setting is used to
    identify the current environment in use. Instead of value1, value2, value3,
    value4. I would have connectionStrin g, printer, messageQueue and such that
    are distinct per environment.

    You then need to use System.Configur ation.Configura tionSettings.Ge tConfig to
    get your section, which returns an object based on the type of section
    handler defined for that section (usually HashTable, but can be other object
    types.

    See the following on how to create new sections via the configSections
    section.



    and:


    Also read about the System.Configur ation.Configura tionSettings class and
    other classes in the System.Configur ation namespace.

    If you define a distinct enough custom section you could put it in the
    machine config, however I would put it under something other than
    appSettings, as all applications can read appSettings (technically All .NET
    applications can read all sections of machine config). If I were to put a
    custom section in machine.config I would more than likely use the namespace
    for my assembly as the custom section name. For the above I would define a
    custom section to hold which environment I was using, then

    Hope this helps
    Jay


    "Guinness Mann" <GMann@dublin.c om> wrote in message
    news:MPG.19e228 32c8400ef9896c7 @news.newsguy.c om...[color=blue]
    > I'm working in VS2003.NET, with C#.
    >
    > I use app.config to store the connection string to my SQL Server. This
    > works fine.
    >
    > Lately I've been taking work home where the connection string is
    > different. I deal with this by having app.config.home and
    > app.config.work files which I copy over the app.config file as required
    > by my location.
    >
    > I was wondering if there is another way to do this. In my Microsoft.NET
    > file system hierarchy I notice a CONFIG directory with a machine.config
    > file in it. Said machine.config file has an example appSettings section
    > in it.
    >
    > Would that be an appropriate place to store my SQL Server connection
    > string? How would I access it? I currently say
    > 'ConfigurationS ettings.AppSett ings["strCon"]'. How would that change?
    >
    > Thank you,
    >
    > -- Rick[/color]


    Comment

    • Guinness Mann

      #3
      Re: app.config

      Jay,

      Perhaps I'm dense, but my objective is to be able to zip up a project,
      take it home, and run it without change. As near as I can tell, this
      solution just pushes the problem of what to edit to someplace else. (I
      haven't figured out where, yet :-) )

      You say:[color=blue]
      > all applications can read appSettings (technically All .NET
      > applications can read all sections of machine config).[/color]

      Actually, that sounds perfect to me. At work, all applications need to
      access the same SQL Server. Likewise at home. I can see where in a
      more complex environment with more servers it would be a problem.

      So then, when I call System.Configur ation.AppSettin s["section"] does it
      search for "section" in both places: app.config and machine.config?

      -- Rick


      In article <eZDhgoshDHA.61 6@TK2MSFTNGP11. phx.gbl>,
      Jay_Harlow@emai l.msn.com says...[color=blue]
      > Rick,
      > I would recommend a single app.config file. Within this single app.config
      > have custom sections that identify distinct 'environments' (home or work).
      >
      > There is a predefined configSections section in the app.config that you use
      > to define new sections.
      >
      > Something like:
      >
      > <configuratio n>
      >
      > <configSections >
      > <section name="myClass1S tuff"
      > type="System.Co nfiguration.Dic tionarySectionH andler, System" />
      > <sectionGroup name="environme nts">
      > <section name="work"
      > type="System.Co nfiguration.Sin gleTagSectionHa ndler, System" />
      > <section name="home"
      > type="System.Co nfiguration.Sin gleTagSectionHa ndler, System" />
      > </sectionGroup>
      > </configSections>
      >
      > <appSettings>
      > <add key="key1" value="value1" />
      > <add key="environmen t" value="work" />
      > </appSettings>
      >
      > <myClass1Stuf f>
      > <add key="key1" value="value1" />
      > </myClass1Stuff>
      >
      > <environments >
      > <work value1="xyz" value2="abc" value3="edf" value4="ghi" />
      > <home value1="xyz" value2="abc" value3="edf" value4="ghi" />
      > </environments>
      >
      > </configuration>
      >
      > If you inherit from DictionarySecti onHandler you can easily change the key
      > or value in your section. I've derived from DictionarySecti onHandler to
      > change key/value to plugin/type in a few of my projects.
      >
      > I use the above environments section to define each of my environments,
      > Production, QA, Test, Development. Where each environment points to the
      > correct database servers, web servers, message queues, printers any 'per
      > environment' settings. The appSettings/environment setting is used to
      > identify the current environment in use. Instead of value1, value2, value3,
      > value4. I would have connectionStrin g, printer, messageQueue and such that
      > are distinct per environment.
      >[/color]

      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: app.config

        Rick,[color=blue]
        > So then, when I call System.Configur ation.AppSettin s["section"] does it
        > search for "section" in both places: app.config and machine.config?[/color]

        ConfigurationSe ttings reads machine.config first, then it "merges" what is
        in app.config. I understand that it physically reads the config files once,
        and caches the results returned from ConfigurationSe ttings.GetConfi g,
        avoiding extra overhead of continually reading & searching the files.

        This merging allows app.config to override what is in machine.config. It is
        also part of the reason that every thing in appSettings is normally <add>,
        it can be <remove> or <clear>

        What I tried to say at the bottom of my response is if you use
        machine.config DO NOT USE appSettings. Define your own custom section,
        similar to the "environmen ts" section in my example, just make sure you name
        the section for the namespace of your assembly. Also make sure this name
        follows the .NET guide lines (company.Tehcno logyName).



        To minimize interfering with future .NET upgrades and other class libraries
        that may be installed.

        Hope this helps
        Jay


        "Guinness Mann" <GMann@dublin.c om> wrote in message
        news:MPG.19e267 08183b7ab49896c c@news.newsguy. com...[color=blue]
        > Jay,
        >
        > Perhaps I'm dense, but my objective is to be able to zip up a project,
        > take it home, and run it without change. As near as I can tell, this
        > solution just pushes the problem of what to edit to someplace else. (I
        > haven't figured out where, yet :-) )
        >
        > You say:[color=green]
        > > all applications can read appSettings (technically All .NET
        > > applications can read all sections of machine config).[/color]
        >
        > Actually, that sounds perfect to me. At work, all applications need to
        > access the same SQL Server. Likewise at home. I can see where in a
        > more complex environment with more servers it would be a problem.
        >
        > So then, when I call System.Configur ation.AppSettin s["section"] does it
        > search for "section" in both places: app.config and machine.config?
        >
        > -- Rick
        >
        >
        > In article <eZDhgoshDHA.61 6@TK2MSFTNGP11. phx.gbl>,
        > Jay_Harlow@emai l.msn.com says...[color=green]
        > > Rick,
        > > I would recommend a single app.config file. Within this single[/color][/color]
        app.config[color=blue][color=green]
        > > have custom sections that identify distinct 'environments' (home or[/color][/color]
        work).[color=blue][color=green]
        > >
        > > There is a predefined configSections section in the app.config that you[/color][/color]
        use[color=blue][color=green]
        > > to define new sections.
        > >
        > > Something like:
        > >
        > > <configuratio n>
        > >
        > > <configSections >
        > > <section name="myClass1S tuff"
        > > type="System.Co nfiguration.Dic tionarySectionH andler, System" />
        > > <sectionGroup name="environme nts">
        > > <section name="work"
        > > type="System.Co nfiguration.Sin gleTagSectionHa ndler, System" />
        > > <section name="home"
        > > type="System.Co nfiguration.Sin gleTagSectionHa ndler, System" />
        > > </sectionGroup>
        > > </configSections>
        > >
        > > <appSettings>
        > > <add key="key1" value="value1" />
        > > <add key="environmen t" value="work" />
        > > </appSettings>
        > >
        > > <myClass1Stuf f>
        > > <add key="key1" value="value1" />
        > > </myClass1Stuff>
        > >
        > > <environments >
        > > <work value1="xyz" value2="abc" value3="edf" value4="ghi" />
        > > <home value1="xyz" value2="abc" value3="edf" value4="ghi" />
        > > </environments>
        > >
        > > </configuration>
        > >
        > > If you inherit from DictionarySecti onHandler you can easily change the[/color][/color]
        key[color=blue][color=green]
        > > or value in your section. I've derived from DictionarySecti onHandler to
        > > change key/value to plugin/type in a few of my projects.
        > >
        > > I use the above environments section to define each of my environments,
        > > Production, QA, Test, Development. Where each environment points to the
        > > correct database servers, web servers, message queues, printers any 'per
        > > environment' settings. The appSettings/environment setting is used to
        > > identify the current environment in use. Instead of value1, value2,[/color][/color]
        value3,[color=blue][color=green]
        > > value4. I would have connectionStrin g, printer, messageQueue and such[/color][/color]
        that[color=blue][color=green]
        > > are distinct per environment.
        > >[/color][/color]


        Comment

        • Guinness Mann

          #5
          Re: app.config

          [This followup was posted to microsoft.publi c.dotnet.genera l and a copy
          was sent to the cited author.]

          In article <Ofxi7swhDHA.17 96@TK2MSFTNGP10 .phx.gbl>,
          Jay_Harlow@emai l.msn.com says...[color=blue]
          > ConfigurationSe ttings reads machine.config first, then it "merges" what is
          > in app.config. I understand that it physically reads the config files once,
          > and caches the results returned from ConfigurationSe ttings.GetConfi g,
          > avoiding extra overhead of continually reading & searching the files.
          > ...
          > Hope this helps[/color]

          Thanks, Jay. That was just right!

          -- Rick

          Comment

          Working...