C# LoaderOptimization.MultiDomain导致AssemblyResolve无法激发
一旦启用了C# LoaderOptimization.MultiDomain导致AssemblyResolve无法激发,c#,.net,appdomain,assemblyresolve,C#,.net,Appdomain,Assemblyresolve,一旦启用了LoaderOptimization.MultiDomain,我就会遇到一些问题。 不知何故,当在某些特定场景中找不到程序集时,它不再触发AssemblyResolve事件 我把它缩小到一个包含两个项目的复制案例。 您可以在此处下载完整的解决方案: 假设我有一个顶级项目: class AppProgram { // Comment this and both Resolve1 and Resolve2 will happen // If enabled, Resolve
LoaderOptimization.MultiDomain
,我就会遇到一些问题。
不知何故,当在某些特定场景中找不到程序集时,它不再触发AssemblyResolve事件
我把它缩小到一个包含两个项目的复制案例。
您可以在此处下载完整的解决方案:
假设我有一个顶级项目:
class AppProgram
{
// Comment this and both Resolve1 and Resolve2 will happen
// If enabled, Resolve2 doesn't happen
[LoaderOptimization(LoaderOptimization.MultiDomain)]
static void Main(string[] args)
{
var applicationPath = AppDomain.CurrentDomain.BaseDirectory;
var appDomainSetup = new AppDomainSetup { ApplicationBase = Directory.GetParent(applicationPath).FullName };
var otherDomain = AppDomain.CreateDomain("other domain", AppDomain.CurrentDomain.Evidence, appDomainSetup);
otherDomain.DoCallBack(TestCallback);
}
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
Console.WriteLine($"Resolve1 {args.Name}");
return null;
}
private static void TestCallback()
{
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
var remoteAssembly = AppDomain.CurrentDomain.Load("ConsoleApplication1");
remoteAssembly.EntryPoint.Invoke(null, new object[] { new string[0] });
}
}
然后,程序集控制台应用程序1具有以下代码:
(请注意,Newtonsoft.Json被引用(w/NuGet),但Copy Local设置为false,这会导致故意的程序集解析和FileNotFoundException)
如果在Main上未设置LoaderOptimization.MultiDomain
属性,则会调用Resolve1(来自顶级程序)和Resolve2(来自库)。
但是,只要设置了LoaderOptimization.multi-domain
,就只调用Resolve1
在我的实际应用程序中,多域对于避免JIT和再次初始化程序集(将第二次启动时间从5秒减少到几乎为零)至关重要。
因此,我想了解这个bug的根本原因,并希望找到解决方法
编辑:即使在Resolve1期间尝试硬编码我的解析,它似乎也无法缓存程序集(加载速度很慢)。
也许多域仅适用于ApplicationBase
中的程序集?
在这种情况下,我想我必须使用PrivateBinPath来优化JIT。
我仍然想知道AssemblyResolve错误是否可以解决,因为仍然有一些额外的程序集从外部路径加载,我不在乎它们是否是JIT。注意:在Microsoft Developer社区提交:注意:在Microsoft Developer社区提交:
class ConsoleProgram
{
static void Main(string[] args)
{
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
try
{
Test();
}
catch (FileNotFoundException)
{
}
}
private static void Test()
{
JsonConvert.SerializeObject(true);
}
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
Console.WriteLine($"Resolve2 {args.Name}");
return null;
}
}