【问题标题】:Custom App.config section with conflicting filenames具有冲突文件名的自定义 App.config 部分
【发布时间】:2014-01-12 10:52:16
【问题描述】:

该项目有一个名为 MyNamespace.MyConfig 的自定义配置节类。该类存储在 C# 库 MyNamespace.MyAssembly.dll 中。

该项目还有一个控制台应用程序。客户端希望它的调用方式与 dll 相同,即 MyNamespace.MyAssembly.exe。

App.config 文件的定义如下:

 <section name="myConfigSection" type="MyNamespace.MyConfig, MyNamespace.MyAssembly"/>

现在的问题是应用程序找不到 MyConfig 类,因为它在错误的程序集中查找 - 在 exe 文件中。

第一个想法是更具体地说明程序集的名称,因此 ConfigurationManager 知道它应该查看 dll 而不是 exe:

 <section name="myConfigSection" type="MyNamespace.MyConfig, MyNamespace.MyAssembly.dll"/>

不幸的是,这无济于事,应用程序说,它找不到名称为 MyNamespace.MyAssembly.dll 的程序集。

使其工作的唯一方法是将应用程序程序集重命名为 MyNamespace.MyAssembly.Console,但客户端不同意这一点。

我如何告诉 ConfigurationManager 它应该在 MyNamespace.MyAssembly.dll 而不是 MyNamespace.MyAssembly.exe 中查找我的 MyNamespace.MyConfig 类?

【问题讨论】:

    标签: .net app-config .net-assembly


    【解决方案1】:

    不幸的是,在这种情况下,无法指示 .NET 首先搜索 .dll。即使有一个像这样的程序集的全名

    <section name="myConfigSection" type="MyNamespace.MyConfig, MyNamespace.MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=...."/>
    

    它仍然会首先检查当前的 exe,因为它们共享相同的程序集名称。 我建议在不更改项目名称的情况下为您的配置项目使用不同的输出程序集名称。

    然后使用

    <section name="myConfigSection" type="MyNamespace.MyConfig, MyNamespace.MyAssemblyConfig />
    

    在 app.config 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 2011-01-30
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      相关资源
      最近更新 更多