ComVisible .NET 程序集和 app.config
- 我有 .NET 程序集,其中一些类标记为
ComVisible
- 该程序集已使用
regasm /codebase " assembly_path"
注册 - 我有 app.config 名称(实际上 -
MyAssemblyName. dll.config
),位于程序集的文件夹中 - 我通过
ConfigurationManager.AppSettings["SettingName"]
访问程序集中的 appSettings - 我有 VBScript 文件,其中通过
CreateObject("...")
创建我的 COM 对象 - 当创建对象时(从 VBScript),
ConfigurationManager.AppSettings["SettingName"]
返回 null。看起来程序集没有看到配置文件。
我应该怎么做才能使其可行?
- I have .NET assembly with some classes marked as
ComVisible
- This assembly is registered with
regasm /codebase "assembly_path"
- I have app.config name (actually -
MyAssemblyName.dll.config
) which are in assembly's folder - I access to appSettings in my assembly through
ConfigurationManager.AppSettings["SettingName"]
- I have VBScript file which creates my COM object through
CreateObject("...")
- When object is created (from VBScript),
ConfigurationManager.AppSettings["SettingName"]
returns null. It looks like assembly doesn't see config file.
What should I to do to make it workable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如 Komyg 所说,一种可能的方法是直接读取配置文件,而不是使用 ConfigurationManager 的嵌入式行为。对于那些遇到同样问题的人:
您可以使用:
这样您将始终使用位于程序集文件夹中的文件
YourAssemblyName.dll.config
中的读取设置。它还允许您使用app.config
的其他功能(例如appSetting
的file
属性),如果您将使用 XPath 或类似的东西。One possible way, as Komyg said, is to read config file directly, instead of using ConfigurationManager's embedded behavior. For those, who will have the same problem: instead of
you can use:
This way you will always use read settings from file
YourAssemblyName.dll.config
which lays in your assembly's folder. It will allow you to use also another features forapp.config
(likeappSetting
'sfile
attribute), which will no be available if you will use XPath or something like this.我也有同样的问题。需要 app.config 文件的资源管理器 shell 扩展(Com 可见,通过 regasm /codebase 注册)无法找到连接字符串和设置。
我做了什么:
假设配置文件存储在与程序集相同的目录中。
I had the same problem. An explorer shell extension (Com visible, registered via regasm /codebase) which required an app.config file, could not find nor the connection strings neither the settings.
What I did:
That assumes, that the config file is stored in the same directory as the assembly.
我使用了 sputnik 的答案,在一个 IIS 测试环境中运行良好,但在另一个测试环境中则不然。事实证明,设置 APP_CONFIG_FILE 属性后,您可能需要使用反射来触摸 ConfigurationManager 类以使更改生效。我在设置 APP_CONFIG_FILE 属性后使用了此函数:
除此之外,首先保存该属性,然后在完成后恢复它可能是一个好主意:
I used sputnik's answer, worked great in one IIS test environment, but not another. It turns out that after you set APP_CONFIG_FILE property, you may need to use reflection to touch the ConfigurationManager class to make the change stick. I used this function after setting the APP_CONFIG_FILE property:
Beyond this, it's probably a good idea to first save off the property and then restore it when you are done:
所有内容都在同一个文件夹中吗(您的 dll、vb 脚本、配置文件等)?如果没有,您可以尝试在 PATH 环境变量中添加配置文件的完整路径...
此外,如果您在 Internet Explorer 中运行此 VB 脚本,您可能会遇到一些安全问题(通常 IE 不允许您访问到硬盘)。在这种情况下,您可以尝试将配置文件放在桌面上,我不知道为什么,但这似乎是运行 ActiveX 程序时的默认路径,因此对于 VB 脚本来说也可能如此。
最后,您可以向 dll 添加一些调试,例如创建一个简单的文本文件并随后查找它,这样您就可以看到系统实际上在哪里运行您的库。
Is everything on the same folder (your dll, vb script, config file, etc)? If not, you could try to add the full path to the config file in the PATH evironment variable...
Also if you are running this VB Script inside Internet Explorer you might have some security issues (normally the IE doesn't allow you access to the hard drive). In this case you can try to put the config file in your desktop, I don't know why but that seems to be the default path when it runs an ActiveX program, so it might also be true for an VB Script.
Finally you could add some debugging to your dll, something like creating a simple text file and looking for it afterwards, this way you can see where does your system is actually running your library.