-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
BinderTracingTest.ResolutionFlow.cs
736 lines (677 loc) · 43.6 KB
/
BinderTracingTest.ResolutionFlow.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.Loader;
using Xunit;
using ResolutionStage = BinderTracingTests.ResolutionAttempt.ResolutionStage;
using ResolutionResult = BinderTracingTests.ResolutionAttempt.ResolutionResult;
namespace BinderTracingTests
{
partial class BinderTracingTest
{
private static CustomALC alcInstance = new CustomALC("StaticInstance");
private static Assembly loadedAssembly;
// Matching assembly in load context:
// ResolutionAttempted : FindInLoadContext (CustomALC) [Success]
[BinderTest(isolate: true, testSetup: nameof(LoadSubdirectoryAssembly_InstanceALC))]
public static BindOperation FindInLoadContext_CustomALC()
{
var assemblyName = new AssemblyName(SubdirectoryAssemblyName);
Assembly asm = alcInstance.LoadFromAssemblyName(assemblyName);
return new BindOperation()
{
AssemblyName = assemblyName,
AssemblyLoadContext = alcInstance.ToString(),
Success = true,
ResultAssemblyName = asm.GetName(),
ResultAssemblyPath = asm.Location,
Cached = false,
ResolutionAttempts = new List<ResolutionAttempt>()
{
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, alcInstance, ResolutionResult.Success, asm)
}
};
}
// Matching assembly in load context:
// ResolutionAttempted : FindInLoadContext (DefaultALC) [Success]
[BinderTest(isolate: true, testSetup: nameof(UseDependentAssembly))]
public static BindOperation FindInLoadContext_DefaultALC()
{
var assemblyName = new AssemblyName(DependentAssemblyName);
Assembly asm = AssemblyLoadContext.Default.LoadFromAssemblyName(assemblyName);
return new BindOperation()
{
AssemblyName = assemblyName,
AssemblyLoadContext = DefaultALC,
Success = true,
ResultAssemblyName = asm.GetName(),
ResultAssemblyPath = asm.Location,
Cached = false,
ResolutionAttempts = new List<ResolutionAttempt>()
{
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, AssemblyLoadContext.Default, ResolutionResult.Success, asm)
}
};
}
// Incompatible version in load context:
// ResolutionAttempted : FindInLoadContext (CustomALC) [IncompatibleVersion]
// ResolutionAttempted : AssemblyLoadContextLoad (CustomALC) [AssemblyNotFound]
// ResolutionAttempted : FindInLoadContext (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : ApplicationAssemblies (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : DefaultAssemblyLoadContextFallback (CustomALC) [AssemblyNotFound]
// ResolutionAttempted : AssemblyLoadContextResolvingEvent (CustomALC) [AssemblyNotFound]
// ResolutionAttempted : AppDomainAssemblyResolveEvent (CustomALC) [AssemblyNotFound]
[BinderTest(isolate: true, testSetup: nameof(LoadSubdirectoryAssembly_InstanceALC),
activeIssue: "https://github.com/dotnet/runtime/issues/68521")] // Emit-based Invoke causes an extra load.
public static BindOperation FindInLoadContext_CustomALC_IncompatibleVersion()
{
var assemblyName = new AssemblyName($"{SubdirectoryAssemblyName}, Version=4.3.2.1");
Assert.Throws<FileNotFoundException>(() => alcInstance.LoadFromAssemblyName(assemblyName));
return new BindOperation()
{
AssemblyName = assemblyName,
AssemblyLoadContext = alcInstance.ToString(),
Success = false,
Cached = false,
ResolutionAttempts = new List<ResolutionAttempt>()
{
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, alcInstance, ResolutionResult.IncompatibleVersion, loadedAssembly),
GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextLoad, alcInstance, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.ApplicationAssemblies, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.DefaultAssemblyLoadContextFallback, alcInstance, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextResolvingEvent, alcInstance, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.AppDomainAssemblyResolveEvent, alcInstance, ResolutionResult.AssemblyNotFound)
}
};
}
// Incompatible version in load context:
// ResolutionAttempted : FindInLoadContext (DefaultALC) [IncompatibleVersion]
// ResolutionAttempted : AssemblyLoadContextResolvingEvent (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : AppDomainAssemblyResolveEvent (DefaultALC) [AssemblyNotFound]
[BinderTest(isolate: true, testSetup: nameof(UseDependentAssembly))]
public static BindOperation FindInLoadContext_DefaultALC_IncompatibleVersion()
{
var assemblyName = new AssemblyName($"{DependentAssemblyName}, Version=4.3.2.1");
Assert.Throws<FileNotFoundException>(() => AssemblyLoadContext.Default.LoadFromAssemblyName(assemblyName));
return new BindOperation()
{
AssemblyName = assemblyName,
AssemblyLoadContext = DefaultALC,
Success = false,
Cached = false,
ResolutionAttempts = new List<ResolutionAttempt>()
{
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, AssemblyLoadContext.Default, ResolutionResult.IncompatibleVersion, UseDependentAssembly()),
GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextResolvingEvent, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.AppDomainAssemblyResolveEvent, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound)
}
};
}
// Successful load through application assemblies search:
// ResolutionAttempted : FindInLoadContext (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : ApplicationAssemblies (DefaultALC) [Success]
[BinderTest(isolate: true)]
public static BindOperation ApplicationAssemblies()
{
var assemblyName = new AssemblyName(DependentAssemblyName);
Assembly asm = AssemblyLoadContext.Default.LoadFromAssemblyName(assemblyName);
return new BindOperation()
{
AssemblyName = assemblyName,
AssemblyLoadContext = DefaultALC,
Success = true,
ResultAssemblyName = asm.GetName(),
ResultAssemblyPath = asm.Location,
Cached = false,
ResolutionAttempts = new List<ResolutionAttempt>()
{
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.ApplicationAssemblies, AssemblyLoadContext.Default, ResolutionResult.Success, asm)
}
};
}
// Incompatible version in load context:
// ResolutionAttempted : FindInLoadContext (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : ApplicationAssemblies (DefaultALC) [IncompatibleVersion]
// ResolutionAttempted : AssemblyLoadContextResolvingEvent (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : AppDomainAssemblyResolveEvent (DefaultALC) [AssemblyNotFound]
[BinderTest(isolate: true)]
public static BindOperation ApplicationAssemblies_IncompatibleVersion()
{
var assemblyName = new AssemblyName($"{DependentAssemblyName}, Version=4.3.2.1");
Assert.Throws<FileNotFoundException>(() => AssemblyLoadContext.Default.LoadFromAssemblyName(assemblyName));
return new BindOperation()
{
AssemblyName = assemblyName,
AssemblyLoadContext = DefaultALC,
Success = false,
Cached = false,
ResolutionAttempts = new List<ResolutionAttempt>()
{
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.ApplicationAssemblies, AssemblyLoadContext.Default, ResolutionResult.IncompatibleVersion, new AssemblyName($"{DependentAssemblyName}, Version=1.0.0.0"), Helpers.GetAssemblyInAppPath(DependentAssemblyName)),
GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextResolvingEvent, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.AppDomainAssemblyResolveEvent, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound)
}
};
}
// Mismatched assembly name from platform assemblies:
// ResolutionAttempted : FindInLoadContext (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : ApplicationAssemblies (DefaultALC) [MismatchedAssemblyName]
// ResolutionAttempted : AssemblyLoadContextResolvingEvent (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : AppDomainAssemblyResolveEvent (DefaultALC) [AssemblyNotFound]
[BinderTest(isolate: true,
additionalLoadsToTrack: new string[] { DependentAssemblyName + "_Copy" },
activeIssue: "https://github.com/dotnet/runtime/issues/68521")] // Emit-based Invoke causes AssemblyNotFound instead of MismatchedAssemblyName.
public static BindOperation ApplicationAssemblies_MismatchedAssemblyName()
{
var assemblyName = new AssemblyName($"{DependentAssemblyName}_Copy, Culture=neutral, PublicKeyToken=null");
string assemblyPath = Helpers.GetAssemblyInAppPath(assemblyName.Name);
try
{
File.Copy(Helpers.GetAssemblyInAppPath(DependentAssemblyName), assemblyPath, true);
Assert.Throws<FileNotFoundException>(() => AssemblyLoadContext.Default.LoadFromAssemblyName(assemblyName));
}
finally
{
File.Delete(assemblyPath);
}
return new BindOperation()
{
AssemblyName = assemblyName,
AssemblyLoadContext = DefaultALC,
Success = false,
Cached = false,
ResolutionAttempts = new List<ResolutionAttempt>()
{
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.ApplicationAssemblies, AssemblyLoadContext.Default, ResolutionResult.MismatchedAssemblyName, UseDependentAssembly().GetName(), assemblyPath),
GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextResolvingEvent, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.AppDomainAssemblyResolveEvent, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound)
}
};
}
// Successful load through AssemblyLoadContext.Load:
// ResolutionAttempted : FindInLoadContext (CustomALC) [AssemblyNotFound]
// ResolutionAttempted : AssemblyLoadContextLoad (CustomALC) [Success]
[BinderTest]
public static BindOperation AssemblyLoadContextLoad()
{
var assemblyName = new AssemblyName(SubdirectoryAssemblyName);
var assemblyPath = Helpers.GetAssemblyInSubdirectoryPath(assemblyName.Name);
CustomALC alc = new CustomALC(nameof(AssemblyLoadContextLoad));
alc.EnableLoad(assemblyName.Name, assemblyPath);
Assembly asm = alc.LoadFromAssemblyName(assemblyName);
return new BindOperation()
{
AssemblyName = assemblyName,
AssemblyLoadContext = alc.ToString(),
Success = true,
ResultAssemblyName = asm.GetName(),
ResultAssemblyPath = asm.Location,
Cached = false,
ResolutionAttempts = new List<ResolutionAttempt>()
{
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, alc, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextLoad, alc, ResolutionResult.Success, asm)
}
};
}
// Exception thrown in AssemblyLoadContext.Load:
// ResolutionAttempted : FindInLoadContext (CustomALC) [AssemblyNotFound]
// ResolutionAttempted : AssemblyLoadContextLoad (CustomALC) [Exception]
[BinderTest]
public static BindOperation AssemblyLoadContextLoad_Exception()
{
var assemblyName = new AssemblyName(SubdirectoryAssemblyName);
var assemblyPath = Helpers.GetAssemblyInSubdirectoryPath(assemblyName.Name);
CustomALC alc = new CustomALC(nameof(AssemblyLoadContextLoad), true /*throwOnLoad*/);
AssertExtensions.ThrowsWithInnerException<FileLoadException, Exception>(() => alc.LoadFromAssemblyName(assemblyName));
return new BindOperation()
{
AssemblyName = assemblyName,
AssemblyLoadContext = alc.ToString(),
Success = false,
Cached = false,
ResolutionAttempts = new List<ResolutionAttempt>()
{
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, alc, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextLoad, alc, $"Exception on Load in '{alc.ToString()}'")
}
};
}
// Successful load through default ALC fallback:
// ResolutionAttempted : FindInLoadContext (CustomALC) [AssemblyNotFound]
// ResolutionAttempted : AssemblyLoadContextLoad (CustomALC) [AssemblyNotFound]
// ResolutionAttempted : FindInLoadContext (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : ApplicationAssemblies (DefaultALC) [Success]
// ResolutionAttempted : DefaultAssemblyLoadContextFallback (CustomALC) [Success]
[BinderTest(isolate: true, additionalLoadsToTrack: new string[] { "System.Xml" })]
public static BindOperation DefaultAssemblyLoadContextFallback()
{
var assemblyName = new AssemblyName("System.Xml");
CustomALC alc = new CustomALC(nameof(DefaultAssemblyLoadContextFallback));
Assembly asm = alc.LoadFromAssemblyName(assemblyName);
return new BindOperation()
{
AssemblyName = assemblyName,
AssemblyLoadContext = alc.ToString(),
Success = true,
ResultAssemblyName = asm.GetName(),
ResultAssemblyPath = asm.Location,
Cached = false,
ResolutionAttempts = new List<ResolutionAttempt>()
{
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, alc, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextLoad, alc, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.ApplicationAssemblies, AssemblyLoadContext.Default, ResolutionResult.Success, asm),
GetResolutionAttempt(assemblyName, ResolutionStage.DefaultAssemblyLoadContextFallback, alc, ResolutionResult.Success, asm)
}
};
}
// Successful load through satellite assembly resolution logic:
// ResolutionAttempted : FindInLoadContext (CustomALC) [AssemblyNotFound]
// ResolutionAttempted : AssemblyLoadContextLoad (CustomALC) [AssemblyNotFound]
// ResolutionAttempted : ResolveSatelliteAssembly (CustomALC) [Success]
[BinderTest]
public static BindOperation ResolveSatelliteAssembly()
{
AssemblyName assemblyName = new AssemblyName($"{DependentAssemblyName}.resources");
assemblyName.CultureInfo = SatelliteCulture;
CustomALC alc = new CustomALC(nameof(ResolveSatelliteAssembly));
alc.LoadFromAssemblyPath(Helpers.GetAssemblyInAppPath(DependentAssemblyName));
Assembly asm = alc.LoadFromAssemblyName(assemblyName);
return new BindOperation()
{
AssemblyName = assemblyName,
AssemblyLoadContext = alc.ToString(),
Success = true,
ResultAssemblyName = asm.GetName(),
ResultAssemblyPath = asm.Location,
Cached = false,
ResolutionAttempts = new List<ResolutionAttempt>()
{
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, alc, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextLoad, alc, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.ResolveSatelliteAssembly, alc, ResolutionResult.Success, asm)
}
};
}
// Successful load through AssemblyLoadContext.Resolving event (Custom ALC):
// ResolutionAttempted : FindInLoadContext (CustomALC) [AssemblyNotFound]
// ResolutionAttempted : AssemblyLoadContextLoad (CustomALC) [AssemblyNotFound]
// ResolutionAttempted : FindInLoadContext (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : ApplicationAssemblies (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : DefaultAssemblyLoadContextFallback (CustomALC) [AssemblyNotFound]
// ResolutionAttempted : AssemblyLoadContextResolvingEvent (CustomALC) [Success]
[BinderTest(isolate: true,
activeIssue: "https://github.com/dotnet/runtime/issues/68521")] // Emit-based Invoke causes an extra load.
public static BindOperation AssemblyLoadContextResolvingEvent_CustomALC()
{
var assemblyName = new AssemblyName(SubdirectoryAssemblyName);
CustomALC alc = new CustomALC(nameof(AssemblyLoadContextResolvingEvent_CustomALC));
using (var handlers = new Handlers(HandlerReturn.RequestedAssembly, alc))
{
Assembly asm = alc.LoadFromAssemblyName(assemblyName);
return new BindOperation()
{
AssemblyName = assemblyName,
AssemblyLoadContext = alc.ToString(),
Success = true,
ResultAssemblyName = asm.GetName(),
ResultAssemblyPath = asm.Location,
Cached = false,
ResolutionAttempts = new List<ResolutionAttempt>()
{
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, alc, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextLoad, alc, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.ApplicationAssemblies, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.DefaultAssemblyLoadContextFallback, alc, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextResolvingEvent, alc, ResolutionResult.Success, asm)
},
AssemblyLoadContextResolvingHandlers = handlers.Invocations
};
}
}
// Successful load through AssemblyLoadContext.Resolving event (default ALC):
// ResolutionAttempted : FindInLoadContext (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : ApplicationAssemblies (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : AssemblyLoadContextResolvingEvent (DefaultALC) [Success]
[BinderTest(isolate: true)]
public static BindOperation AssemblyLoadContextResolvingEvent_DefaultALC()
{
var assemblyName = new AssemblyName(SubdirectoryAssemblyName);
using (var handlers = new Handlers(HandlerReturn.RequestedAssembly, AssemblyLoadContext.Default))
{
Assembly asm = AssemblyLoadContext.Default.LoadFromAssemblyName(assemblyName);
return new BindOperation()
{
AssemblyName = assemblyName,
AssemblyLoadContext = DefaultALC,
Success = true,
ResultAssemblyName = asm.GetName(),
ResultAssemblyPath = asm.Location,
Cached = false,
ResolutionAttempts = new List<ResolutionAttempt>()
{
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.ApplicationAssemblies, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextResolvingEvent, AssemblyLoadContext.Default, ResolutionResult.Success, asm)
},
AssemblyLoadContextResolvingHandlers = handlers.Invocations
};
}
}
// Exception in AssemblyLoadContext.Resolving event handler (Custom ALC):
// ResolutionAttempted : FindInLoadContext (CustomALC) [AssemblyNotFound]
// ResolutionAttempted : AssemblyLoadContextLoad (CustomALC) [AssemblyNotFound]
// ResolutionAttempted : FindInLoadContext (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : ApplicationAssemblies (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : DefaultAssemblyLoadContextFallback (CustomALC) [AssemblyNotFound]
// ResolutionAttempted : AssemblyLoadContextResolvingEvent (CustomALC) [Exception]
[BinderTest(isolate: true,
activeIssue: "https://github.com/dotnet/runtime/issues/68521")] // Emit-based Invoke causes an extra load.
public static BindOperation AssemblyLoadContextResolvingEvent_CustomALC_Exception()
{
var assemblyName = new AssemblyName(SubdirectoryAssemblyName);
CustomALC alc = new CustomALC(nameof(AssemblyLoadContextResolvingEvent_CustomALC_Exception));
using (var handlers = new Handlers(HandlerReturn.Exception, alc))
{
AssertExtensions.ThrowsWithInnerException<FileLoadException, BinderTestException>(() => alc.LoadFromAssemblyName(assemblyName));
return new BindOperation()
{
AssemblyName = assemblyName,
AssemblyLoadContext = alc.ToString(),
Success = false,
Cached = false,
ResolutionAttempts = new List<ResolutionAttempt>()
{
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, alc, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextLoad, alc, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.ApplicationAssemblies, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.DefaultAssemblyLoadContextFallback, alc, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextResolvingEvent, alc, "Exception in handler for AssemblyLoadContext.Resolving")
},
AssemblyLoadContextResolvingHandlers = handlers.Invocations
};
}
}
// Exception in AssemblyLoadContext.Resolving event handler (default ALC):
// ResolutionAttempted : FindInLoadContext (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : ApplicationAssemblies (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : AssemblyLoadContextResolvingEvent (DefaultALC) [Exception]
[BinderTest(isolate: true)]
public static BindOperation AssemblyLoadContextResolvingEvent_DefaultALC_Exception()
{
var assemblyName = new AssemblyName(SubdirectoryAssemblyName);
using (var handlers = new Handlers(HandlerReturn.Exception, AssemblyLoadContext.Default))
{
AssertExtensions.ThrowsWithInnerException<FileLoadException, BinderTestException>(() => AssemblyLoadContext.Default.LoadFromAssemblyName(assemblyName));
return new BindOperation()
{
AssemblyName = assemblyName,
AssemblyLoadContext = DefaultALC,
Success = false,
Cached = false,
ResolutionAttempts = new List<ResolutionAttempt>()
{
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.ApplicationAssemblies, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextResolvingEvent, AssemblyLoadContext.Default, "Exception in handler for AssemblyLoadContext.Resolving")
},
AssemblyLoadContextResolvingHandlers = handlers.Invocations
};
}
}
// Successful load through AppDomain.AssemblyResolve event (Custom ALC):
// ResolutionAttempted : FindInLoadContext (CustomALC) [AssemblyNotFound]
// ResolutionAttempted : AssemblyLoadContextLoad (CustomALC) [AssemblyNotFound]
// ResolutionAttempted : FindInLoadContext (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : ApplicationAssemblies (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : DefaultAssemblyLoadContextFallback (CustomALC) [AssemblyNotFound]
// ResolutionAttempted : AssemblyLoadContextResolvingEvent (CustomALC) [AssemblyNotFound]
// ResolutionAttempted : AppDomainAssemblyResolveEvent (CustomALC) [Success]
[BinderTest(isolate: true,
activeIssue: "https://github.com/dotnet/runtime/issues/68521")] // Emit-based Invoke causes an extra load.
public static BindOperation AppDomainAssemblyResolveEvent_CustomALC()
{
var assemblyName = new AssemblyName(SubdirectoryAssemblyName);
CustomALC alc = new CustomALC(nameof(AppDomainAssemblyResolveEvent_CustomALC));
using (var handlers = new Handlers(HandlerReturn.RequestedAssembly))
{
Assembly asm = alc.LoadFromAssemblyName(assemblyName);
return new BindOperation()
{
AssemblyName = assemblyName,
AssemblyLoadContext = alc.ToString(),
Success = true,
ResultAssemblyName = asm.GetName(),
ResultAssemblyPath = asm.Location,
Cached = false,
ResolutionAttempts = new List<ResolutionAttempt>()
{
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, alc, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextLoad, alc, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.ApplicationAssemblies, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.DefaultAssemblyLoadContextFallback, alc, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextResolvingEvent, alc, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.AppDomainAssemblyResolveEvent, alc, ResolutionResult.Success, asm)
},
AppDomainAssemblyResolveHandlers = handlers.Invocations
};
}
}
// Successful load through AppDomain.AssemblyResolve event (default ALC):
// ResolutionAttempted : FindInLoadContext (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : ApplicationAssemblies (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : AssemblyLoadContextResolvingEvent (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : AppDomainAssemblyResolveEvent (DefaultALC) [Success]
[BinderTest(isolate: true)]
public static BindOperation AppDomainAssemblyResolveEvent_DefaultALC()
{
var assemblyName = new AssemblyName(SubdirectoryAssemblyName);
using (var handlers = new Handlers(HandlerReturn.RequestedAssembly))
{
Assembly asm = AssemblyLoadContext.Default.LoadFromAssemblyName(assemblyName);
return new BindOperation()
{
AssemblyName = assemblyName,
AssemblyLoadContext = DefaultALC,
Success = true,
ResultAssemblyName = asm.GetName(),
ResultAssemblyPath = asm.Location,
Cached = false,
ResolutionAttempts = new List<ResolutionAttempt>()
{
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.ApplicationAssemblies, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextResolvingEvent, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.AppDomainAssemblyResolveEvent, AssemblyLoadContext.Default, ResolutionResult.Success, asm)
},
AppDomainAssemblyResolveHandlers = handlers.Invocations
};
}
}
// Exception in AppDomain.AssemblyResolve event handler:
// ResolutionAttempted : FindInLoadContext (CustomALC) [AssemblyNotFound]
// ResolutionAttempted : AssemblyLoadContextLoad (CustomALC) [AssemblyNotFound]
// ResolutionAttempted : FindInLoadContext (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : ApplicationAssemblies (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : DefaultAssemblyLoadContextFallback (CustomALC) [AssemblyNotFound]
// ResolutionAttempted : AssemblyLoadContextResolvingEvent (CustomALC) [AssemblyNotFound]
// ResolutionAttempted : AppDomainAssemblyResolveEvent (CustomALC) [Exception]
[BinderTest(isolate: true,
activeIssue: "https://github.com/dotnet/runtime/issues/68521")] // Emit-based Invoke causes an extra load.
public static BindOperation AppDomainAssemblyResolveEvent_Exception()
{
var assemblyName = new AssemblyName(SubdirectoryAssemblyName);
CustomALC alc = new CustomALC(nameof(AppDomainAssemblyResolveEvent_Exception));
using (var handlers = new Handlers(HandlerReturn.Exception))
{
AssertExtensions.ThrowsWithInnerException<FileLoadException, BinderTestException>(() => alc.LoadFromAssemblyName(assemblyName));
return new BindOperation()
{
AssemblyName = assemblyName,
AssemblyLoadContext = alc.ToString(),
Success = false,
Cached = false,
ResolutionAttempts = new List<ResolutionAttempt>()
{
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, alc, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextLoad, alc, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.ApplicationAssemblies, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.DefaultAssemblyLoadContextFallback, alc, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.AssemblyLoadContextResolvingEvent, alc, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.AppDomainAssemblyResolveEvent, alc, "Exception in handler for AppDomain.AssemblyResolve")
},
AppDomainAssemblyResolveHandlers = handlers.Invocations
};
}
}
// Assembly is found in app path when attempted to load through full path:
// ResolutionAttempted : FindInLoadContext (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : ApplicationAssemblies (DefaultALC) [Success]
[BinderTest(isolate: true)]
public static BindOperation LoadFromAssemblyPath_FoundInAppPath()
{
var assemblyName = new AssemblyName(DependentAssemblyName);
var assemblyPath = Helpers.GetAssemblyInSubdirectoryPath(assemblyName.Name);
Assembly asm = AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath);
Assert.NotEqual(assemblyPath, asm.Location);
return new BindOperation()
{
AssemblyName = asm.GetName(),
AssemblyPath = assemblyPath,
AssemblyLoadContext = DefaultALC,
RequestingAssembly = CoreLibName,
RequestingAssemblyLoadContext = DefaultALC,
Success = true,
ResultAssemblyName = asm.GetName(),
ResultAssemblyPath = asm.Location,
Cached = false,
ResolutionAttempts = new List<ResolutionAttempt>()
{
GetResolutionAttempt(asm.GetName(), ResolutionStage.FindInLoadContext, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(asm.GetName(), ResolutionStage.ApplicationAssemblies, AssemblyLoadContext.Default, ResolutionResult.Success, asm),
}
};
}
// Assembly with different MVID is found in load context when attempted to load through full path:
// ResolutionAttempted : FindInLoadContext (DefaultALC) [Failure]
[BinderTest(isolate: true, testSetup: nameof(UseDependentAssembly))]
public static BindOperation LoadFromAssemblyPath_FoundInLoadContext_DifferentMvid()
{
var assemblyPath = Helpers.GetAssemblyInSubdirectoryPath($"{DependentAssemblyName}_V2");
string errorMessage = null;
try
{
AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath);
}
catch (FileLoadException e)
{
errorMessage = e.Message;
}
var assemblyName = new AssemblyName($"{DependentAssemblyName}, Version=2.0.0.0");
return new BindOperation()
{
AssemblyName = assemblyName,
AssemblyPath = assemblyPath,
AssemblyLoadContext = DefaultALC,
RequestingAssembly = CoreLibName,
RequestingAssemblyLoadContext = DefaultALC,
Success = false,
Cached = false,
ResolutionAttempts = new List<ResolutionAttempt>()
{
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, AssemblyLoadContext.Default, ResolutionResult.Failure, new AssemblyName($"{DependentAssemblyName}, Version=1.0.0.0"), Helpers.GetAssemblyInAppPath(DependentAssemblyName), errorMessage),
}
};
}
// Incompatible version is found in app path when attempted to load through full path:
// ResolutionAttempted : FindInLoadContext (DefaultALC) [AssemblyNotFound]
// ResolutionAttempted : ApplicationAssemblies (DefaultALC) [IncompatibleVersion]
[BinderTest(isolate: true)]
public static BindOperation LoadFromAssemblyPath_FoundInAppPath_IncompatibleVersion()
{
var assemblyPath = Helpers.GetAssemblyInSubdirectoryPath($"{DependentAssemblyName}_V2");
Assert.Throws<FileLoadException>(() => AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath));
var assemblyName = new AssemblyName($"{DependentAssemblyName}, Version=2.0.0.0");
return new BindOperation()
{
AssemblyName = assemblyName,
AssemblyPath = assemblyPath,
AssemblyLoadContext = DefaultALC,
RequestingAssembly = CoreLibName,
RequestingAssemblyLoadContext = DefaultALC,
Success = false,
Cached = false,
ResolutionAttempts = new List<ResolutionAttempt>()
{
GetResolutionAttempt(assemblyName, ResolutionStage.FindInLoadContext, AssemblyLoadContext.Default, ResolutionResult.AssemblyNotFound),
GetResolutionAttempt(assemblyName, ResolutionStage.ApplicationAssemblies, AssemblyLoadContext.Default, ResolutionResult.IncompatibleVersion, new AssemblyName($"{DependentAssemblyName}, Version=1.0.0.0"), Helpers.GetAssemblyInAppPath(DependentAssemblyName)),
}
};
}
private static ResolutionAttempt GetResolutionAttempt(AssemblyName assemblyName, ResolutionStage stage, AssemblyLoadContext alc, string exceptionMessage)
{
return GetResolutionAttempt(assemblyName, stage, alc, ResolutionResult.Exception, null, null, exceptionMessage);
}
private static ResolutionAttempt GetResolutionAttempt(AssemblyName assemblyName, ResolutionStage stage, AssemblyLoadContext alc, ResolutionResult result, Assembly resultAssembly = null)
{
AssemblyName resultAssemblyName = null;
string resultAssemblyPath = string.Empty;
if (resultAssembly != null)
{
resultAssemblyName = resultAssembly.GetName();
resultAssemblyPath = resultAssembly.Location;
}
return GetResolutionAttempt(assemblyName, stage, alc, result, resultAssemblyName, resultAssemblyPath);
}
private static ResolutionAttempt GetResolutionAttempt(AssemblyName assemblyName, ResolutionStage stage, AssemblyLoadContext alc, ResolutionResult result, AssemblyName resultAssemblyName, string resultAssemblyPath, string errorMessage = null)
{
var attempt = new ResolutionAttempt()
{
AssemblyName = assemblyName,
Stage = stage,
AssemblyLoadContext = alc == AssemblyLoadContext.Default ? DefaultALC : alc.ToString(),
Result = result,
ResultAssemblyName = resultAssemblyName,
ResultAssemblyPath = resultAssemblyPath
};
if (!string.IsNullOrEmpty(errorMessage))
{
attempt.ErrorMessage = errorMessage;
}
else
{
switch (result)
{
case ResolutionAttempt.ResolutionResult.AssemblyNotFound:
attempt.ErrorMessage = "Could not locate assembly";
break;
case ResolutionAttempt.ResolutionResult.IncompatibleVersion:
attempt.ErrorMessage = $"Requested version {assemblyName.Version} is incompatible with found version {resultAssemblyName.Version}";
break;
case ResolutionAttempt.ResolutionResult.MismatchedAssemblyName:
attempt.ErrorMessage = $"Requested assembly name '{assemblyName.FullName}' does not match found assembly name '{resultAssemblyName.FullName}'";
break;
}
}
return attempt;
}
private static void LoadSubdirectoryAssembly_InstanceALC()
{
string assemblyPath = Helpers.GetAssemblyInSubdirectoryPath(SubdirectoryAssemblyName);
loadedAssembly = alcInstance.LoadFromAssemblyPath(assemblyPath);
}
}
}