AppDomain.CurrentDomain.SetupInformation.PrivateBinPath为null

bit*_*onk 21 .net c#

当我启动只有一个AppDomain的应用程序时,AppDomain.CurrentDomain.SetupInformation.PrivateBinPath为null.即使我在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>
Run Code Online (Sandbox Code Playgroud)

更新

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

更新2

这里需要一些额外的背景信息:Nancy框架的AppDomainAssemblyTypeScanner.GetAssemblyDirectories()中出现此问题.

南希自动发现并加载第三方模块和其他"插件".默认情况下,这应该通过查看探测路径来完成与正常链接的程序集相同的方式(即融合会这样做).使用Assembly.Load(而不是Assembly.LoadFrom)加载程序集,因此据我所知,加载程序集的所有依赖程序集也必须在应用程序/应用程序域的探测路径中可访问.

Sin*_*nix 6

不是答案,不适合作为评论

如上所述,默认appdomain不使用AppDomainSetup进行路径探测配置.而不是这样,探测路径从.appconfig文件中读取,不会暴露给托管代码.

*完整clr上的自托管应用程序可以使用自定义ICustomAppDomainManager(或IHostAssemblyManager)覆盖行为,但这超出了问题的范围.

所以只有三种可能的方法:

  • 首先,你可以Nancy.Bootstrapper.AppDomainAssemblyTypeScanner.LoadAssemblies(somedir, "*.dll")自己打电话
  • 其次,您可以使用自定义专用bin路径集将nancy主机包装到辅助appdomain.
  • 第三:等待https://github.com/NancyFx/Nancy/pull/1846并使用自定义IResourceAssemblyProvider.

在任何情况下,您都需要汇编'目录列表.如果您不想将副本存储为<appSettings>值,则必须自行解析appconfig文件.


g.p*_*dou 6

如何访问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;
}
Run Code Online (Sandbox Code Playgroud)

假设你的问题中的配置文件样本它返回:"Dir1; Dir2; Dir3"