如何在由 asp 经典网页调用的 .Net COM 对象中编辑 ConfigurationManager.App/ConnectionSettings
我正在实例化 .NET COM 对象,并希望更新 ConfigurationManager.AppSettings 和 ConfigurationManager.ConnectionStrings 属性。 我注意到,即使我在程序集配置文件中进行了设置,例如“ComLib.dll.config”,这些设置在运行时也不可用。
我使用以下方法从 ASP 经典网页调用创建 COM 对象:
Dim COMObject
Set COMObject = Server.CreateObject("COMAPI.COMObject")
我使用 VS2010 调试器单步执行 .NET COM 对象 (COMAPI.COMObject) 的构造函数。这就是我检查 ConfigurationManager 以检查加载的内容的要点。
"Assembly.GetExecutingAssembly" 和 "Assembly.GetCallingAssembly" 具有相同的结果,即 "ComLib.dll" 程序集的完整路径注册地点。这正是我所期望的。此位置还包含“ComLib.dll.config” 文件,该文件不会提取到ConfigurationManager 中。
当我尝试使用以下命令清除AppSettings时,会发生异常:
ConfigurationManager.AppSettings.Clear();
异常是:“服务器对象:006~ASP 0177~Server.CreateObject Failed~80131902”
我的想法是,上述异常是 ASP Classic 对“AppSettings 是只读的”的解释,但我不能 100% 确定。
我接下来要加载 "ComLib.dll.config" 文件,然后运行每个 AppSettings 项,将它们添加到 ConfigurationManager.AppSettings< /strong> 设置,但这也会导致异常:“服务器对象:006~ASP 0177~Server.CreateObject Failed~80131902”。
我在 .NET COM 对象中使用的代码如下:
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);
}
我需要在 ConfigurationManager 中配置信息,因为此 .NET COM LIB 用于调用其他 .NET 程序集。其他 .NET 程序集需要配置。
先感谢您。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够获得一些 帮助来自 MSDN 论坛。
基本上,我不是在 ConfigurationManager.AppSettings 上使用 .Clear/.Add/.Remove 而是访问一个设置(如果存在或不存在),并为该键设置一个值:
这会更新 ConfigurationManager.AppSettings。这很棒,因为当我访问应用程序的部分内容时,这些部分超出了最初的“ASP Classic”-> 的范围。 “COM API”调用我仍然能够从 ConfigurationManager 获取应用程序设置。
尽管如此,我仍然不知道如何编辑 ConfigurationManager.ConnectionStrings。
如果我在 ConnectionStrings 上尝试 .Clear / .Add / .Remove,则会发生异常 >。关于这个问题有什么想法吗?
如果我找到更多信息,我会更新这个答案。
作为更新:为了解决没有我的设置的 ConnectionString 设置,我实现了 Unity 容器 IoC 框架。我使用静态实例来托管 IoC 容器并以这种方式解析我的实例。
通过这样做,我可以在 COM API 上针对类类型注册我的接口,这意味着我可以通过构造函数注入传递连接字符串设置,我想要解析实例,因为连接字符串设置在范围内。
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.