I am instantiating a .NET COM object and would like to update the ConfigurationManager.AppSettings and ConfigurationManager.ConnectionStrings properties.
I have noticed that even though I have settings in the assembly config file, e.g. “ComLib.dll.config” those settings are not available at runtime.
I make a call to create the COM Object from an ASP Classic Web Page using the following:
Dim COMObject
Set COMObject = Server.CreateObject("COMAPI.COMObject")
I use my VS2010 debugger to step into the Constructor of my .NET COM Object (COMAPI.COMObject). This is the point were I inspect the ConfigurationManager to check what is loaded.
Both “Assembly.GetExecutingAssembly” and “Assembly.GetCallingAssembly” have the same result which is the full path to the “ComLib.dll” assembly registered location. Which is what I expect. This location also has the “ComLib.dll.config” file, which is not pulling through into the ConfigurationManager.
When I try to clear the AppSettings using the following command an exception occurs:
ConfigurationManager.AppSettings.Clear();
The exception is: “Server object: 006~ASP 0177~Server.CreateObject Failed~80131902”
My thoughts are that the above exception is the ASP Classic interpretation of “AppSettings is read-only” but I am not 100% sure.
What I move onto, is loading the “ComLib.dll.config” file and then running over each of the AppSettings items adding them to the ConfigurationManager.AppSettings set but that causes an exception as well: “Server object: 006~ASP 0177~Server.CreateObject Failed~80131902”.
The code I use in the .NET COM Object is as follows:
Assembly comApiAssembly = Assembly.GetExecutingAssembly();
string comApiLocation = comApiAssembly.Location;
configuration = null;
configuration = ConfigurationManager.OpenExeConfiguration(comApiLocation);
var toLoadEnumberable = configuration.AppSettings.Settings.GetEnumerator();
while (toLoadEnumberable.MoveNext())
{
var current = (KeyValueConfigurationElement)toLoadEnumberable.Current;
ConfigurationManager.AppSettings.Add(current.Key, current.Value);
}
I need my configuration information in the ConfigurationManager as this .NET COM LIB is used to make calls to other .NET Assemblies. The other .NET assemblies require configuration.
Thank you in advance.
I was able to get some help from the MSDN forums.
Basically instead of using .Clear/.Add/.Remove on the ConfigurationManager.AppSettings I access a setting, if it exists or not, and set a value for that key:
This updates the ConfigurationManager.AppSettings. This is great because as I access parts of my application which are out of scope from the initial “ASP Classic” -> “COM API” call I am still able to get Application Settings from the ConfigurationManager.
With all this said, I still don’t know how to edit the ConfigurationManager.ConnectionStrings.
If I try .Clear / .Add / .Remove on ConnectionStrings an exception occurs. Any ideas on this issue?
I will update this answer if I find more info.
As an update: To get around the ConnectionString settings not having my settings, I have implemented the Unity Container IoC framework. I use a Static instance to host the IoC container and resolve my instances that way.
By doing this I can register my Interfaces against Class Types at the COM API which means I can pass the Connection String Settings, via Constructor Injection, I want to the resolving instance as the Connection String Settings are in scope.