当我启动只有一个 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>
Update
正如汉斯·帕桑特指出的下面评论 https://stackoverflow.com/questions/33353420/appdomain-currentdomain-setupinformation-privatebinpath-is-null?noredirect=1#comment54508987_33353420, SetupInformation.PrivateBinPath
未为主应用程序域设置。所以上面的方法是行不通的。您对模拟融合在探测路径中搜索组件的方式有何建议,或者至少采用<probing privatePath="" />
从当前应用程序配置考虑?我能想到的最好的办法就是阅读<probing privatePath="" />
当当前域是主应用程序域时,手动从 App.config 中(AppDomain.CurrentDomain.IsDefaultAppDomain()
is true
)。有没有更好的办法?
Update 2
这里有一些额外的背景信息,这需要什么:这个问题发生在AppDomainAssemblyTypeScanner.GetAssemblyDirectories() https://github.com/NancyFx/Nancy/blob/master/src/Nancy/Bootstrapper/AppDomainAssemblyTypeScanner.cs#L330 of the 南希框架 http://nancyfx.org/.
Nancy 自动发现并加载第 3 方模块和其他“插件”。默认情况下,这应该以与正常链接的程序集加载相同的方式完成(即融合会这样做),通过查看探测路径。程序集使用加载Assembly.Load
(相对于Assembly.LoadFrom
)据我了解,所有依赖的已加载程序集的程序集也必须在应用程序/应用程序域的探测路径中可访问。
如何访问 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”
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)