AppDomain.CurrentDomain.SetupInformation.PrivateBinPath为nullCurrentDomain、AppDomain、SetupInformation

2023-09-04 07:00:18 作者:划船不用浆,一生全靠浪

当我开始我的应用程序,只有一个AppDomain中, AppDomain.CurrentDomain.SetupInformation.PrivateBinPath 为空。虽然我在设置探测路径的 MyApp.exe.config 的,如下图所示。

我会expeceted了 AppDomain.CurrentDomain.SetupInformation.PrivateBinPath 包含字符串方向1,方向2; DIR3

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

 < XML版本=1.0编码=UTF-8&GT?;

<结构>
  <的appSettings>
    <添加键=富值=酒吧/>
  < /的appSettings>
  <启动>
    <! -  supportedRuntime版本=V1.1.4322/  - >
  < /启动>

  <运行>
    < gcConcurrent启用=真/>
    < assemblyBinding的xmlns =瓮:架构 - 微软COM:asm.v1>
      < publisherPolicy适用=YES/>

      <! - 请添加您的子目录探测路径! - >
      <探测privatePath =方向1,方向2; DIR3/>
    < / assemblyBinding>
  < /运行>
  < System.Windows.Forms的jitDebugging =真/>
< /结构>
 

更新

正如汉斯帕桑特指出comment下面, SetupInformation.PrivateBinPath 未设置主应用程序域。因此,上述方法无效。什么是您的建议,以模拟在探测路径,或者至少采取集会的方式融合搜索<探测privatePath =/> 从当前的应用程序配置成帐户?我能想出的最好的事情就是阅读<探测privatePath =/> 从App.config中手动设置在当前域是主要的AppDomain( AppDomain.CurrentDomain.IsDefaultAppDomain())。有没有更好的办法?

更新2

inno setup怎么修改界面快捷方式的路径

下面是什么,这是需要一些额外的背景资料:这个问题发生在AppDomainAssemblyTypeScanner.GetAssemblyDirectories()的南希框架。

南希autodiscovers并加载第三方模块等插件。默认情况下,这是应该做同样的方法将被载入正常连接组件(即作为融合将做到这一点),通过查看探测路径。组件使用装载的Assembly.Load (而不是 Assembly.LoadFrom ),所以据我所知,所有的相关的加载组件的组件必须可在应用程序/应用程序域的太探测路径。

解决方案   

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

要保持兼容融合东西会做,你可以读取配置文件的实际上的获取当前的探测路径:

 私有静态字符串GetProbingPath()
{
    VAR CONFIGFILE = XElement.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
    VAR probingElement =(
        从运行
            在configFile.Descendants(运行)
        从assemblyBinding
            在runtime.Elements(XName.Get(assemblyBinding,瓮:架构 - 微软COM:asm.v1))
        从探测
            在assemblyBinding.Elements(XName.Get(探索,瓮:架构 - 微软COM:asm.v1))
        选择探测)
        .FirstOrDefault();

    返回属性约probingElement(privatePath)的价值?。
}
 

假如在你的问题中的配置文件样本返回: 方向1,方向2; DIR3

When I start my application that only has one AppDomain, AppDomain.CurrentDomain.SetupInformation.PrivateBinPath is null. Even though I have probing paths set in MyApp.exe.config as shown below.

I would have expeceted that AppDomain.CurrentDomain.SetupInformation.PrivateBinPath contains the string "Dir1;Dir2;Dir3".

How can I access the probing path as configured in the 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>

Update

As Hans Passant pointed out the comment below, SetupInformation.PrivateBinPath is not set for the primary appdomain. So the above doesn't work. What would be your suggestion to simulate the way fusion searches for assemblies in the probing path or at least take <probing privatePath="" /> from the current application configuration into account? The best thing I can come up with is to read <probing privatePath="" /> from App.config manually when the current domain is the primary appdomain (AppDomain.CurrentDomain.IsDefaultAppDomain() is true). Is there a better way?

Update 2

Here some additional background information what this is needed for: This problem occured in AppDomainAssemblyTypeScanner.GetAssemblyDirectories() of the Nancy framework.

Nancy autodiscovers and loads 3rd party modules and other "plugins". By default this is supposed to be done same way as normally linked assemblies would be loaded (i.e. as fusion would do it) by looking through the probing paths. Assemblies are loaded using Assembly.Load (as opposed to Assembly.LoadFrom) so as I understand it, all the dependent assemblies of the loaded assemblies must be reachable in the probing path of the application/appdomain too.

解决方案

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

To remain compatible what fusion will do, you can read the config file in effect to get the current probing paths:

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;
}

Supposing the config file sample in your question it returns: "Dir1;Dir2;Dir3"