locked
App.Config RRS feed

  • Question

  • This is weird. Under VS2008 I could add a app.config file to the MStest project and be able to read those settings during the test. I have migrated to VS2010 and this no longer works, I can see that the only config file that has loaded is the machine config.

     

    I am testing a config section handler by the way so I do need to read from a config file.

     

    Any ideas?

    Wednesday, November 17, 2010 11:12 AM

Answers

  • We have the same problem. But I do not like an idea to add params to Microsoft.Moles.VsHost.x86.exe.config. Instead of this I added moles for AppSettings. Something like this:

        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
          System.Configuration.Moles.MConfigurationManager.AppSettingsGet = delegate
            {
              return new NameValueCollection
                {
                  { "UseRealCertificate", "false" },
                  { "TestCertificateFile", "Certificate.p12" },
                  { "TestCertificatePassword", "megapwd" },
                  { "Delay", "0" },
                };
            };
        }
    

    Wednesday, December 29, 2010 11:57 AM

All replies

  • Sort of got this working by adding the entries to the Microsoft.Moles.VsHost.x86.exe.config.

     

    Is there any other way to do this?

     

    I have tested that app.config works if it is not being hosted by Moles.

    Wednesday, November 17, 2010 11:55 AM
  • This is a regression. This thread will be opened until the issue is resolved.
    Jonathan "Peli" de Halleux - Try Pex online at www.pexforfun.com!
    Wednesday, November 17, 2010 2:31 PM
  • VS2008 with tests hosted by Moles has same problem :(
    Saturday, December 25, 2010 9:47 PM
  • We have the same problem. But I do not like an idea to add params to Microsoft.Moles.VsHost.x86.exe.config. Instead of this I added moles for AppSettings. Something like this:

        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
          System.Configuration.Moles.MConfigurationManager.AppSettingsGet = delegate
            {
              return new NameValueCollection
                {
                  { "UseRealCertificate", "false" },
                  { "TestCertificateFile", "Certificate.p12" },
                  { "TestCertificatePassword", "megapwd" },
                  { "Delay", "0" },
                };
            };
        }
    

    Wednesday, December 29, 2010 11:57 AM
  • This works for every section that I've checked:

    [ClassInitialize]
    public static void MyClassInitialize(TestContext testContext)
    {
      System.Configuration.Moles.MConfigurationManager.GetSectionString =
        (string configurationName) =>
        {
          ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
          Assembly assembly = Assembly.GetExecutingAssembly();
          fileMap.ExeConfigFilename = assembly.Location + ".config";
          Configuration config =
            ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
          object section = config.GetSection(configurationName);
          if (section is DefaultSection)
          {
            ConfigurationSection configurationSection = (ConfigurationSection) section;
            Type sectionType =
              Type.GetType(configurationSection.SectionInformation.Type);
            if (sectionType != null)
            {
              IConfigurationSectionHandler sectionHandler =
                (IConfigurationSectionHandler)AppDomain.CurrentDomain.CreateInstanceAndUnwrap
                  (sectionType.Assembly.FullName, sectionType.FullName);
              section =
                sectionHandler.Create(
                  configurationSection.SectionInformation.GetParentSection(),
                  null,
                  XElement.Parse(configurationSection.SectionInformation.GetRawXml()).ToXmlNode());
            }
          }

          return section;
        };
    }

    Saturday, February 12, 2011 10:54 PM
  • If I have user defined section say "mysection" in App.config the line 

     

     

                                IConfigurationSectionHandler sectionHandler =

                                (IConfigurationSectionHandler)AppDomain.CurrentDomain.CreateInstanceAndUnwrap

                                    (sectionType.Assembly.FullName, sectionType.FullName);

     

     

    crashes with following stack trace...

     

     

    StubsTutorialTest.FileReaderStubsTest.<MyClassInitialize>b__0(String configurationName) in C:\Users\vinmacji\Documents\Visual Studio 2010\Projects\StubsTutorial\StubsTutorialTest\FileReaderStubsTest.cs: line 41

    System.Configuration.ConfigurationManager.GetSection(String sectionName)

    StubsTutorialTest.FileReaderStubsTest.CheckValidFileWithMoles() in C:\Users\vinmacji\Documents\Visual Studio 2010\Projects\StubsTutorial\StubsTutorialTest\FileReaderStubsTest.cs: lin

    • Edited by vinmacji Sunday, February 13, 2011 1:40 PM username changed in text
    Sunday, February 13, 2011 5:52 AM
  • Could you post an example of the config section XML as well as the entry in <configSections/>.  Does your ConfigurationSectionHandler implement IConfigurationSectionHandler?  What exception does it get?
    Monday, February 14, 2011 1:53 AM
  • No... ConfigurationSectionHandler did not implement IConfigurationSectionHandler. Now it works.

    Thank you Robin

    Monday, February 14, 2011 11:32 PM
  • I tried the above solution from Robin but I'm getting an exception when an initializing an EntityFramewoork ObjectContext when sectionType is a DbProviderFactoriesConfigurationManager, namely when configurationName is "system.data".  The problem is:

    configurationSection.SectionInformation.GetRawXml()

    is null.  So I work around that by catching the exception and just setting section to null in that case, but later the ObjectContext constructor must be fetching AppSettings (ConfigurationManager.GetSection("appSettings"), which results in this exception:

    "The configuration section 'appSettings' has an unexpected declaration."

    AppSettings was working fine before trying this solution, so i don't think it is a misformatting of the appsettings portion of app.config.

    Indeed at time of exception calling this code returns seemingly valid xml configuration:

    (System.Configuration.ConfigurationManager.GetSection("appSettings") as System.Configuration.ConfigurationSection).SectionInformation.GetRawXml();

    "<appSettings>\r\n <add key=\"AutomationAccountPassword\" value=\"[details omitted]\"/>\r\n <add key=\"ReleaseDb\" value=\"[details omitted]\"/>\r\n <add key=\"CompCentral\" value=\"[details omitted]\"/>\r\n </appSettings>"

    Any ideas?


    Blah
    Tuesday, April 5, 2011 8:49 PM