locked
Inconsistent behavior in CPP CLR console application and CSharp console application using custom configuration file RRS feed

  • Question

  • Hi, DotNet Folks

    I am trying to use custom configration file in CPP CLR console application and found a problem. Look at this lib in CSharp and it is compiled as TestConf.dll.

    using

     

    System;

    using

     

    System.Collections.Generic;

    using

     

    System.Configuration;

    using

     

    System.Text;

    namespace

     

    TestConf

    {

     

    public class CustomConfig :

    ConfigurationSection

    {

     

    private static CustomConfig m_Singleton = ConfigurationManager.GetSection("CustomConfig") as CustomConfig

    ;

     

    public static CustomConfig

    Settings

    {

     

    get

    {

     

    return

    m_Singleton;

    }

    }

    [

    ConfigurationProperty(

    "Foo1"

    , DefaultValue =

    "Foo1.Value"

    , IsRequired =

    true

    )]

     

    public string

    Foo1

    {

     

    get { return (string)this["Foo1"

    ]; }

     

    set { this["Foo1"] = value

    ; }

    }

    }

    }

    If I use this DLL in CPP CLR application as:

    // CPPTest.cpp : main project file.

    using

     

    namespace

    System;

    using

     

    namespace

    System::Configuration;

    using

     

    namespace

    TestConf;

     

    int

     

    main(array

    <System::String ^> ^args)

    {

    System::String^ Attr = TestConf::CustomConfig::Settings->Foo1;

    Console::WriteLine(L

    "Hello World"

    );

     

    return

    0;

    }

    with app.config as:

    <?

     

    xml version="1.0" encoding="utf-8"

    ?>

    <

     

    configuration

    >

    <

     

    configSections

    >

    <

     

    section name="CustomConfig" type="

    TestConf.CustomConfig,

    TestConf

     

    "

    />

    </

     

    configSections

    >

     

    <

     

    CustomConfig

     

     

    Foo1="Foo1.Value"

    />

    </

     

    configuration

    >

    Application failed to read CustomConfig section and throws exception.

     

    But if I use TestConf.dll in a CShap application with the same app.config, it works.

    using

     

    System;

    using

     

    System.Collections.Generic;

    using

     

    TestConf;

    namespace

     

    CSharpTest

    {

     

    static class

    Program

    {

     

    ///

    <summary>

     

    ///

    The main entry point for the application.

     

    ///

    </summary>

     

    static void

    Main()

    {

     

    String attr1 = TestConf.CustomConfig

    .Settings.Foo1;

    }

    }

    }

    Could you help me find out what I did wrong in CPP project or confirm there is bug in CLR CPP using custom configurations.

    Thanks.

     

     

     

    Friday, September 23, 2011 4:18 PM

Answers

  • It works fine for me. Here is your repro simplified and formatted:

    1.

    // File:  Library.cs
    // Build: csc.exe /t:library Library.cs
    using System.Configuration;
    
    public class CustomConfig : ConfigurationSection
    {
        private static CustomConfig m_Singleton = ConfigurationManager.GetSection("CustomConfig") as CustomConfig; 
        public static CustomConfig Settings 
        {
            get { return m_Singleton; }
        }
        [ConfigurationProperty("Foo1", DefaultValue="Foo1.Value", IsRequired=true)] 
        public string Foo1
        {
            get { return (string)this["Foo1"]; }
            set { this["Foo1"] = value; }
        }
    }
    
    

    2.

    // File:  TestCSharp.cs
    // Build: csc.exe /r:Library.dll TestCSharp.cs
    class Program
    {
        static void Main()
        {
            System.Console.WriteLine(CustomConfig.Settings.Foo1);
        }
    }
    
    

    3.

    // File:  TestCpp.cpp
    // Build: cl.exe /clr TestCpp.cpp
    #using <System.Configuration.dll>
    #using <Library.dll>
    
    int main(array <System::String ^> ^args)
    {
        System::Console::WriteLine(CustomConfig::Settings->Foo1);
        return 0;
    }
    
    

    4. Config files for both executables (TestCSharp.exe.config and TestCpp.exe.config)

    <?xml version="1.0" encoding="utf-8"?> 
    <configuration> 
        <configSections>
            <section name="CustomConfig" type="CustomConfig, Library"/>
        </configSections>
        <CustomConfig Foo1="Foo1.Value"/> 
    </configuration>
    
    

    5. Output

    TestCsharp.exe
    Foo1.Value
    
    TestCpp.exe
    Foo1.Value

    -Karel

    • Marked as answer by Paul Zhou Monday, October 3, 2011 5:57 AM
    Saturday, September 24, 2011 6:04 AM