none
What's the difference behind the scene between ConfigurationManager.GetSection and Configuration.GetSection? RRS feed

  • Question

  • Hi everyone and thanks for reading my post.


    I have an asp.net app that reads configuration file where I have custom class section as shown below.
    <configuration>
    <configSections>
    <section name="exceptionManagement" type="Microsoft.ApplicationBlocks.ExceptionManagement.ExceptionManagerSectionHandler, Microsoft.ApplicationBlocks.ExceptionManagement"/>
    </configSections>
    <exceptionManagement mode="on">
    <publisher mode="on" assembly="NDA_ExceptionManagement" type="NDA_ExceptionManagement.SentinelPublisher" SentinelURL="http://localhost/Sentinel/Sentinel_WS/Event/EventWS.asmx" critical_SEVERITY="on" major_SEVERITY="on" minor_SEVERITY="on" warning_SEVERITY="on" debug_SEVERITY="on"/>
    </exceptionManagement>
    <appSettings>
     
    When I read it with
     
    ConfigurationManager, then it returns an object and I cast it and save it to setting. This worked perfectly so far.
    ExceptionManagementSettings setting = (ExceptionManagementSettings)ConfigurationManager.GetSection("exceptionManagement");
     
    However, I'm currently working on a test project using nUnit and passing config file path(filePath) to the class that loads settings, so I wrote the following code.
     
    ExeConfigurationFileMap maps = new ExeConfigurationFileMap();
    maps.ExeConfigFilename = filePath;
     
    Configuration confs = ConfigurationManager.OpenMappedExeConfiguration(maps, System.Configuration.ConfigurationUserLevel.None);
    ConfigurationSection section = confs.GetSection("exceptionManagement");
     
    The problem here is that GetSection now returns ConfigurationSection and I cannot cast it to
    ExceptionManagementSettings.
    I tried to deserialize the xml, but had no luck so far. Can anyone give me a suggestion?

    Here's the ExceptionManagementSettings class, FYI.

    using System;
    using System.Collections;

    namespace Microsoft.ApplicationBlocks.ExceptionManagement
    {
        public class ExceptionManagementSettings
        {
            public ExceptionManagementSettings();

            public ExceptionManagementMode Mode { get; set; }
            public ArrayList Publishers { get; }

            public void AddPublisher(PublisherSettings publisher);
        }
    }


    Thanks. K.

    • Edited by UptownGuy Thursday, May 20, 2010 1:35 PM
    Wednesday, May 19, 2010 2:31 PM

Answers

All replies

  • i think you need to go for Configurationmanager rather than just Configuration just because the later is form old versions of .Net and will be considered as obsolete. From the new versions we have to use Configurationmanager
    • Proposed as answer by Skpananghat Thursday, May 20, 2010 7:43 AM
    Thursday, May 20, 2010 7:43 AM
  • Thanks for you suggestion.

    Because my project is a test project and executing dll using nUnit, Configurationmanager does not work.

    If I run this web app, then Configurationmanager gets settings, but if I run dll through nUnit and dll invokes methods in the web app, then Configurationmanager gets nothing. I think it only gets settings from the current domain config file, but in my case, that is nUnit, not the web app.


    Kun
    Thursday, May 20, 2010 1:34 PM
  • Hi Kun,

    You should be used to use MSDN, it's a powerful tool.

    And the defination of these two methods in MSDN show the different clearly:


    Hope this helpful to you! If you have any further quetions, please feel free to let me know.
    Please mark the right answer at right time.
    Bset Regards,
    Tracy
    • Marked as answer by SamAgain Thursday, May 27, 2010 10:11 AM
    Thursday, May 27, 2010 3:08 AM
  • Why not copying your config file in the nUnit domain config file...?

    You could also use the configuration manager to load the good configuration file using ConfigurationManager.OpenMappedExeConfiguration method...

    see http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.openmappedexeconfiguration(v=VS.80).aspx

    • Marked as answer by SamAgain Thursday, May 27, 2010 10:11 AM
    Thursday, May 27, 2010 3:19 AM
  • We keep introducing and changing web.config with new project, so I didn't want to re-copy it whenever it changes.

    I am currently using OpenMappedExeConfiguration. The problem is that it does not work with custom class. With configurationmanager, ExceptionManagementSettings is deserialized to an object and I can use it, but it does nto work with OpenMappedExeConfiguration.

    I showed how I did with codes on my first post. Thanks.


    Kun
    Tuesday, July 13, 2010 1:05 PM
  • Thanks for your help. I did some investigation about these two.

    What I'm really looking for is why ConfigurationManager deserialize a custom class successfully, whereas Configuration(got from OpenMappedExeConfiguration) cannot. As you can see from my code, I have to use OpenMappedExeConfiguration because of the location of configuration file. How can I reconstrut the custom class in this case?

    Thanks again.


    Kun
    Tuesday, July 13, 2010 1:11 PM