c# - AppDomain.CurrentDomain.SetupInformation.PrivateBinPath 为空

标签 c# .net

当我启动只有一个 AppDomain 的应用程序时,AppDomain.CurrentDomain.SetupInformation.PrivateBinPath一片空白。尽管我在 MyApp.exe.config 中设置了探测路径,如下所示。

我会预料到 AppDomain.CurrentDomain.SetupInformation.PrivateBinPath包含字符串 "Dir1;Dir2;Dir3" .

如何访问在 MyApp.exe.config 中配置的探测路径?

<?xml version="1.0" encoding="utf-8"?>

<configuration>
  <appSettings>
    <add key="Foo" value="Bar" />
  </appSettings>
  <startup>
    <!-- supportedRuntime version="v1.1.4322" / -->
  </startup>

  <runtime>
    <gcConcurrent enabled="true" />
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <publisherPolicy apply="yes" />

      <!-- Please add your subdirectories to the probing path! -->
      <probing privatePath="Dir1;Dir2;Dir3" />
    </assemblyBinding>
  </runtime>
  <system.windows.forms jitDebugging="true" />
</configuration>

更新

正如 Hans Passant 指出的 comment below , SetupInformation.PrivateBinPath没有为主要应用域设置。所以上面的不起作用。你有什么建议来模拟融合在探测路径中搜索程序集的方式,或者至少采取 <probing privatePath="" />从当前的应用配置考虑?我能想到的最好的事情就是阅读 <probing privatePath="" />当当前域是主应用程序域( AppDomain.CurrentDomain.IsDefaultAppDomain()true )时手动从 App.config 中获取。有没有更好的办法?

更新 2

此处需要一些额外的背景信息:此问题发生在 AppDomainAssemblyTypeScanner.GetAssemblyDirectories() 中的 Nancy framework .

Nancy 自动发现并加载第 3 方模块和其他“插件”。默认情况下,这应该通过查看探测路径以与正常链接的程序集加载相同的方式完成(即融合会这样做)。使用 Assembly.Load 加载程序集(与 Assembly.LoadFrom 相反)据我了解,加载程序集的所有 dependent 程序集也必须在应用程序/appdomain 的探测路径中可达。

最佳答案

How can I access the probing path as configured in the MyApp.exe.config

为了保持兼容融合将执行的操作,您可以阅读配置文件有效以获取当前的探测路径:

private static string GetProbingPath()
{
    var configFile = XElement.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
    var probingElement = (
        from runtime 
            in configFile.Descendants("runtime")
        from assemblyBinding 
            in runtime.Elements(XName.Get("assemblyBinding", "urn:schemas-microsoft-com:asm.v1"))
        from probing 
            in assemblyBinding.Elements(XName.Get("probing", "urn:schemas-microsoft-com:asm.v1"))
        select probing)
        .FirstOrDefault();

    return probingElement?.Attribute("privatePath").Value;
}

假设您的问题中的配置文件示例返回: “目录 1;目录 2;目录 3”

关于c# - AppDomain.CurrentDomain.SetupInformation.PrivateBinPath 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33353420/

相关文章:

c# - SMTP Gmail 超时

c# - MvvmCross (Android) 似乎保留了 ViewModels 并且从不丢弃,我错过了什么

c# - 在任务取消时处理 CancellationTokenSource 的代码是否正确?

c# - 如何使用 EF 4.3 迁移运行一系列 sql 脚本?

c# - Blazor.net UI 不渲染任何内容

c# - 模拟 DbContext.set<T>.Add() EF6

c# - 使用嵌套的elasticsearch自动完成映射

c# - .net SqlConnection 即使在 using { } 中也不会关闭

.net - OutputCache 为长期陈旧数据提供服务

c# - 每个类型表中的级联删除 (TPT) EF Core 5