如何:使用 ConfigurationSection 而不通过 GetSection 调用加载它
我想加载特定的 ConfigurationSection,但 CLR 加载程序集的方式给我带来了一些麻烦:
我的 CustomConfigurationSection 定义位于特定程序集上,程序集加载的整个过程无法找到该程序集,因为我正在使用外部工具它基本上加载我的程序集,通过反射发现有关它的一些信息,然后尝试“安装”它。 非常类似于尝试安装 Windows 服务时的 installutil。
我快要疯了,因为 ConfigurationManager 试图在原始进程的位置下找到我的 ConfigurationSection 所需的程序集。 我确信这一点,因为我正在使用 SysInternals Process Monitor。 有人可以提供一些解决方法或指示吗?
谢谢!
I would like to load a specific ConfigurationSection but the way the CLR loads the assemblies is giving me some trouble:
My CustomConfigurationSection definition is on a specific assembly which cannot be found by the overall process of assembly loading, because I'm using an external tool which basically loads my assembly, discovers some information about it via reflection and then tries to "install" it. Very much like the installutil when trying to install a windows service.
I'm going nuts because the ConfigurationManager tries to find the required assembly for my ConfigurationSection under the location of the original process. I know this for sure as I'm using SysInternals Process Monitor. Can someone provide some workaround or directions?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您知道程序集的路径,那么您应该尝试 ConfigurationManager.OpenExeConfiguration(exePath)。
If you know the path to your assembly, then you should try ConfigurationManager.OpenExeConfiguration(exePath).
如果您的程序集需要反序列化您的自定义配置部分,但 CLR 找不到该程序集,那么我认为您运气不好(或者我误解了问题?)。
有什么方法可以让 CLR 找到您的程序集(也许提供提示路径)? 如果没有,也许您最好使用单独的 XML 文件来存储此数据,而不是使用 app.config/web.config。
If your assembly is needed to deserialize your custom configuration section, but the CLR can't find the assembly, then I think you're out of luck (or am I misunderstanding the problem?).
Is there any way you can get the CLR to find your assembly (providing a hint path maybe)? If not, maybe you'd be better off using a separate XML file for this data instead of using app.config/web.config.
为什么在加载程序集(定义配置部分)之前尝试访问配置部分? 您是否使用配置部分来定义程序集的位置? 如果是这样,那么您正在使用循环引用。
定义自定义配置部分的代码可以非常独立。 它可以是它自己的组件。 我建议将此代码分离到其自己的程序集中,并将其安装在 GAC 或运行时路径中。 我不知道为什么您需要一个外部工具来“加载”读取自定义配置部分所需的代码。
Why are you trying to access the configuration section before your assembly (that defines the configuration section) has been loaded? Are you using the configuration section to define where your assembly is? If so then you're playing around with circular references.
The code to define a custom configuration section can be very stand-alone. It can be its own assembly. I'd suggest segregating this code into its own assembly and installing it in the GAC or in the runtime path. I don't know why you'd need an external tool to "load" the code needed to read a custom configuration section.
我面临着类似的问题。 多个 DLL 动态加载到主应用程序中。 其中一些 dll 需要配置文件,我使用默认的 ConfigurationManager 来处理它。 我可以成功检索正确的文件(基于后缀为“.config”的 dll 名称)并使用 AppSettings 和 ConnectionStrings 中的设置。
现在我正在尝试加载自定义配置部分。 运行时抱怨在 dll 中找不到节类型。 我已经在配置文件(在 configSections 条目中)中指定了正确的 dll,并且我知道该 dll 已加载,因为该 dll 实际上是插件本身。 但仍然; 看起来运行时只使用 GAC/bin 目录中的类型来查找配置部分。
简而言之:我尝试加载一个自定义配置部分,该配置部分在与尝试加载它的代码相同的 dll 中指定,但它不起作用。
I'm facing a similar problem. Several DLL's get loaded into a main application dynamiccaly. Some of these dll's require a configuration file and I'm using the default ConfigurationManager to handle that. I can succesfully retrieve the correct file (based on the dll's name postfixed with ".config") and use settings from AppSettings and ConnectionStrings.
Now I'm trying to load a custom configuration section. The runtime complains about the section type not being found in the dll. I've specified the correct dll in the config file (in the configSections entry) and I know the dll is loaded because that dll is in fact the plugin itself. But still; it looks like the runtime only uses types from GAC/bin directories to look for configuration sections.
So briefly: I try to load a custom configuration section which is specified in the same dll as the code which is trying to load it, but it doesn't work.