Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, October 7, 2013 3:12 AM
I'm being trying to setup a custom section in my app.config for a COM object I'm building with no luck, after spending a couple days; yes a couple of days; looking at sample code explanations from this and other forums I still cannot make it work. ConfigurationManager.getSection allways returns null. This is a simplified version of code I put together to describe the issue:
I have three files
1 - ConfigurationTest.cs
using System.Runtime.InteropServices;
using System.Configuration;
using System.Collections.Specialized;
namespace configurationTest
{
[ClassInterface(ClassInterfaceType.AutoDual)]
[ProgId("Utils.ConfigurationTest")]
public class ConfigurationTest
{
public string passWord = "Passwowrd1";
public ConfigurationTest()
{
}
public string getPassword()
{
configurationHelper config = (configurationHelper)ConfigurationManager.GetSection("credentialsGroup/credentials");
this.passWord = config.PassWord.ToString();
return this.passWord.ToString();
}
}
}
2 - ConfigurationHelper.cs
using System.Configuration;
namespace configurationTest
{
public class configurationHelper : System.Configuration.ConfigurationSection
{
[ConfigurationProperty("passWord", DefaultValue = "Password1", IsRequired = true)]
public string PassWord
{
get { return (string)this["passWord"]; }
set { this["passWord"] = value; }
}
}
}
3 - App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="credentialsGroup">
<section
name ="credentials"
type ="configurationTest.configurationHelper, configurationTest"
allowLocation="true"
allowDefinition="Everywhere"
/>
</sectionGroup>
</configSections>
<credentialsGroup>
<credentials passWord="Password10">
</credentials>
</credentialsGroup>
</configuration>
I tried to follow the steps from http://msdn.microsoft.com/en-us/library/2tw134k3.aspx, when I instantiate the object and call myInstance.GetPassword, Configuration.GetSection("credentialsGroup/credentials") returns null. I have app.config set to "Copy if newer" and I see that it copies to MyAppName.dll.Config in the same directory of MyAppName.dll
Any ideas?
Thank you!
Luis
All replies (6)
Monday, October 7, 2013 2:00 PM âś…Answered | 1 vote
The problem is with your COM object. DLLs do not have config files. The config subsystem will load either the host apps exe.config or web.config if it is a web app. dll.config isn't used even though the IDE will generate one.
In order for your COM object to be used it has to be hosted. If you're making an inproc COM object then the config entries would need to go into the COM client's config file. This can be a maintenance concern so you might do better to simply use some default values and allow the COM object to be configured by the host using an interface. The host can then decide how to persist configuration options.
If your COM object is hosted out of proc then the hosting EXE is where the settings need to go. Once you've added the config settings to the exe's config file it will be read properly.
A final alternative is to manually load the config file using Configuration. You cannot use ConfigurationManager in this case because it only loads the host config but under the hood Configuration is used by everybody. By manually loading your custom config file you can access your config settings. However you may run into a path problem because the working dir of the EXEC is different than your COM object's binary path so you will likely need to provide the full path to the config file based upon where your binary is loaded.
Michael Taylor
http://msmvps.com/blogs/p3net
Monday, October 7, 2013 6:29 AM
I changed the config file to remove the ', configurationTest' from the type of the section:
<sectionGroup name="credentialsGroup">
<section
name ="credentials"
type ="configurationTest.configurationHelper"
allowLocation="true"
allowDefinition="Everywhere"
/>
and it works.
Please remember to 'Mark as Answer' the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.
Monday, October 7, 2013 1:00 PM
Hi Ahmed, thank you for taking the time to look at this issue, unfortunately I've tried that already and gave me the same results. By the way, I tried this on both MS Windows 8 and MS Windows 7 using VS 2010.
Luis
Luis
Monday, October 7, 2013 1:41 PM
Hi Luis,
May be i'll be totally out of the subject but, I remember that I had some problem to read custom sections of my app.config file in a WPF application.
The solution was to add at the very beginning of the app.config a section that "declares" that there's custom sections...
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="MyFirstCustomSection" type="System.Configuration.SingleTagSectionHandler" />
<section name="MySecondCustomSection" type="System.Configuration.SingleTagSectionHandler" />
</configSections>
<!-- any sections here....... -->
<MyFirstCustomSection>
<!-- Anything you want here... -->
</MyFirstCustomSection>
<MySecondCustomSection>
<!-- Anything you want here... -->
</MySecondCustomSection>
</configuration>
Hope this helped.
Jojo
Monday, October 7, 2013 1:45 PM | 1 vote
I've tested it on console application and it was working.
Please remember to 'Mark as Answer' the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.
Monday, October 7, 2013 2:14 PM
Thanks Michael, I learned something new today!
Luis
Luis