【问题标题】:Dependent Assembly loading via configuration通过配置加载依赖程序集
【发布时间】:2019-01-30 21:45:42
【问题描述】:

我有一个 Visual Studio 项目,它运行得非常好。 但是对于将不同 dll 放置在不同文件夹中的部署,出现了新的客户端要求。

我们有一个框架 dll,可以在不同的项目中使用。此框架 dll 依赖于一些第三方 dll。因此,当我从我的项目中使用这个 dll 时,每个依赖的 dll 都会在构建时复制到我的本地,因为 CopyLocal 属性为 true。

但是现在有了新的要求,我们不能将 CopyLocal 属性设置为 True。客户端不需要任何 dll 的本地副本,而是希望在某个位置与框架相关的 dll。当我这样做时,相关的 DLL 不会被加载。

我知道我有两个选择:

  1. 我可以将它们放入 GAC,但我不想这样做,因为我希望它们支持 xcopy。
  2. 使用反射(但我不确定这是不是正确的方法)

我们可以使用配置做任何事情吗?

【问题讨论】:

  • 或许将framework dll的路径添加到path环境变量中。

标签: c# visual-studio deployment frameworks


【解决方案1】:

您可以使用<probing> configuration element 配置程序集探测路径:

指定公共语言运行时在加载程序集时搜索的应用程序基子目录。

来自 MSDN 的示例:

<configuration>  
   <runtime>  
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
         <probing privatePath="bin;bin2\subbin;bin3"/>  
      </assemblyBinding>  
   </runtime>  
</configuration>  

但是,如果有问题的程序集位于应用程序库之外(“这是执行应用程序的根位置”),您将拥有 <codeBase> configuration element

指定公共语言运行时可以在哪里找到程序集。

来自 MSDN 的示例:

<configuration>  
   <runtime>  
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
         <dependentAssembly>  
            <assemblyIdentity name="myAssembly"  
                              publicKeyToken="32ab4ba45e0a69a1"  
                              culture="neutral" />  
            <codeBase version="2.0.0.0"  
                      href="http://www.litwareinc.com/myAssembly.dll"/>  
         </dependentAssembly>  
      </assemblyBinding>  
   </runtime>  
</configuration>  

有关运行时如何定位程序集的详细信息,您可以参考this MSDN article

正如 OP 所指出的,不幸的是,codeBase 元素仅适用于强命名程序集。对于私有程序集,您需要一种解决方法。一些可行的想法可以在in this discussion找到例如:

我已经测试了后者并且可以确认它有效:

AppDomain.CurrentDomain.AssemblyResolve += (s, e) =>
    Assembly.LoadFile(Path.Combine(Settings.Default.AssemblyPath, Path.ChangeExtension(e.Name.Substring(0, e.Name.IndexOf(',')), ".dll")));

【讨论】:

  • 我认为探测工作在同一个应用程序目录中。但我一定会试试这个。谢谢
  • 这再次不起作用,因为我认为我不能强命名第三方 dll。如果不给他们强名称,我们可能无法使用它。如果我没记错的话。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-06
  • 2021-01-15
  • 1970-01-01
  • 2014-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多