-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathAssemblyResolver.cs
778 lines (689 loc) · 29.3 KB
/
AssemblyResolver.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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#if NETFRAMEWORK || (NET && !WINDOWS_UWP)
#if NETFRAMEWORK
using System.Runtime.InteropServices.WindowsRuntime;
using System.Security;
using System.Security.Permissions;
#endif
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices;
/// <summary>
/// Helps resolve MSTestFramework assemblies for CLR loader.
/// The idea is that Unit Test Adapter creates App Domain for running tests and sets AppBase to tests dir.
/// Since we don't want to put our assemblies to GAC and they are not in tests dir, we use custom way to resolve them.
/// </summary>
#if NETFRAMEWORK
#if RELEASE
#if NET6_0_OR_GREATER
[Obsolete(Constants.PublicTypeObsoleteMessage, DiagnosticId = "MSTESTOBS")]
#else
[Obsolete(Constants.PublicTypeObsoleteMessage)]
#endif
#endif
public
#else
internal sealed
#endif
class AssemblyResolver :
#if NETFRAMEWORK
MarshalByRefObject,
#endif
IDisposable
{
/// <summary>
/// The assembly name of the dll containing logger APIs(EqtTrace) from the TestPlatform.
/// </summary>
/// <remarks>
/// The reason we have this is because the AssemblyResolver itself logs information during resolution.
/// If the resolver is called for the assembly containing the logger APIs, we do not log so as to prevent a stack overflow.
/// </remarks>
private const string LoggerAssemblyNameLegacy = "Microsoft.VisualStudio.TestPlatform.ObjectModel";
/// <summary>
/// The assembly name of the dll containing logger APIs(EqtTrace) from the TestPlatform.
/// </summary>
/// <remarks>
/// The reason we have this is because the AssemblyResolver itself logs information during resolution.
/// If the resolver is called for the assembly containing the logger APIs, we do not log so as to prevent a stack overflow.
/// </remarks>
private const string LoggerAssemblyName = "Microsoft.TestPlatform.CoreUtilities";
/// <summary>
/// The name of the current assembly resources file.
/// </summary>
/// <remarks>
/// When resolving the resources for the current assembly, we need to make sure that we do not log. Otherwise, we will end
/// up either failing or at least printing warning messages to the user about how we could not load the resources dll even
/// when it's not an error. For example, set a culture outside of supported cultures (e.g. en-gb) and you will have an error
/// saying we could not find en-gb resource dll which is normal. For more information,
/// <see href="https://github.com/microsoft/testfx/issues/1598" />.
/// </remarks>
private const string PlatformServicesResourcesName = "Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.resources";
/// <summary>
/// This will have the list of all directories read from runsettings.
/// </summary>
private readonly Queue<RecursiveDirectoryPath> _directoryList;
/// <summary>
/// The directories to look for assemblies to resolve.
/// </summary>
private readonly List<string> _searchDirectories;
/// <summary>
/// Dictionary of Assemblies discovered to date.
/// </summary>
private readonly Dictionary<string, Assembly?> _resolvedAssemblies = [];
/// <summary>
/// Dictionary of Reflection-Only Assemblies discovered to date.
/// </summary>
private readonly Dictionary<string, Assembly?> _reflectionOnlyResolvedAssemblies = [];
/// <summary>
/// lock for the loaded assemblies cache.
/// </summary>
private readonly Lock _syncLock = new();
private static List<string>? s_currentlyLoading;
private bool _disposed;
/// <summary>
/// Initializes a new instance of the <see cref="AssemblyResolver"/> class.
/// </summary>
/// <param name="directories">
/// A list of directories for resolution path.
/// </param>
/// <remarks>
/// If there are additional paths where a recursive search is required
/// call AddSearchDirectoryFromRunSetting method with that list.
/// </remarks>
public AssemblyResolver(IList<string> directories)
{
Guard.NotNullOrEmpty(directories);
_searchDirectories = [.. directories];
_directoryList = new Queue<RecursiveDirectoryPath>();
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(OnResolve);
#if NETFRAMEWORK
AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += new ResolveEventHandler(ReflectionOnlyOnResolve);
// This is required for winmd resolution for arm built sources discovery on desktop.
WindowsRuntimeMetadata.ReflectionOnlyNamespaceResolve += new EventHandler<NamespaceResolveEventArgs>(WindowsRuntimeMetadataReflectionOnlyNamespaceResolve);
#endif
}
/// <summary>
/// Finalizes an instance of the <see cref="AssemblyResolver"/> class.
/// </summary>
~AssemblyResolver()
{
Dispose(false);
}
/// <summary>
/// The dispose.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#if NETFRAMEWORK
/// <summary>
/// Returns object to be used for controlling lifetime, null means infinite lifetime.
/// </summary>
/// <remarks>
/// Note that LinkDemand is needed by FxCop.
/// </remarks>
/// <returns>
/// The <see cref="object"/>.
/// </returns>
[SecurityCritical]
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
public override object? InitializeLifetimeService() => null;
#endif
/// <summary>
/// It will add a list of search directories path with property recursive/non-recursive in assembly resolver .
/// </summary>
/// <param name="recursiveDirectoryPath">
/// The recursive Directory Path.
/// </param>
#if NETFRAMEWORK
public
#else
internal
#endif
void AddSearchDirectoriesFromRunSetting(List<RecursiveDirectoryPath> recursiveDirectoryPath)
{
// Enqueue elements from the list in Queue
if (recursiveDirectoryPath == null)
{
return;
}
foreach (RecursiveDirectoryPath recPath in recursiveDirectoryPath)
{
_directoryList.Enqueue(recPath);
}
}
#if NETFRAMEWORK
/// <summary>
/// Assembly Resolve event handler for App Domain - called when CLR loader cannot resolve assembly.
/// </summary>
/// <param name="sender"> The sender App Domain. </param>
/// <param name="args"> The args. </param>
/// <returns> The <see cref="Assembly"/>. </returns>
internal Assembly? ReflectionOnlyOnResolve(object sender, ResolveEventArgs args)
=> OnResolveInternal(sender, args, true);
#endif
/// <summary>
/// Assembly Resolve event handler for App Domain - called when CLR loader cannot resolve assembly.
/// </summary>
/// <param name="sender"> The sender App Domain. </param>
/// <param name="args"> The args. </param>
/// <returns> The <see cref="Assembly"/>. </returns>
internal Assembly? OnResolve(object? sender, ResolveEventArgs args)
=> OnResolveInternal(sender, args, false);
/// <summary>
/// Adds the subdirectories of the provided path to the collection.
/// </summary>
/// <param name="path"> Path go get subdirectories for. </param>
/// <param name="searchDirectories"> The search Directories. </param>
internal
#if NET
static
#endif
void AddSubdirectories(string path, List<string> searchDirectories)
{
DebugEx.Assert(!StringEx.IsNullOrEmpty(path), "'path' cannot be null or empty.");
DebugEx.Assert(searchDirectories != null, "'searchDirectories' cannot be null.");
// If the directory exists, get it's subdirectories
if (DoesDirectoryExist(path))
{
// Get the directories in the path provided.
string[] directories = GetDirectories(path);
// Add each directory and its subdirectories to the collection.
foreach (string directory in directories)
{
searchDirectories.Add(directory);
AddSubdirectories(directory, searchDirectories);
}
}
}
/// <summary>
/// The dispose.
/// </summary>
/// <param name="disposing">
/// The disposing.
/// </param>
#if NETFRAMEWORK
protected virtual
#else
private
#endif
void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
// cleanup Managed resources like calling dispose on other managed object created.
AppDomain.CurrentDomain.AssemblyResolve -= OnResolve;
#if NETFRAMEWORK
AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve -= ReflectionOnlyOnResolve;
WindowsRuntimeMetadata.ReflectionOnlyNamespaceResolve -= WindowsRuntimeMetadataReflectionOnlyNamespaceResolve;
#endif
}
// cleanup native resources
_disposed = true;
}
}
/// <summary>
/// Verifies if a directory exists.
/// </summary>
/// <param name="path">The path to the directory.</param>
/// <returns>True if the directory exists.</returns>
/// <remarks>Only present for unit testing scenarios.</remarks>
#if NETFRAMEWORK
protected virtual
#else
private static
#endif
bool DoesDirectoryExist(string path) => Directory.Exists(path);
/// <summary>
/// Gets the directories from a path.
/// </summary>
/// <param name="path">The path to the directory.</param>
/// <returns>A list of directories in path.</returns>
/// <remarks>Only present for unit testing scenarios.</remarks>
#if NETFRAMEWORK
protected virtual
#else
private static
#endif
string[] GetDirectories(string path) => Directory.GetDirectories(path);
/// <summary>
/// Verifies if a file exists.
/// </summary>
/// <param name="filePath">The file path.</param>
/// <returns><c>true</c> if the file exists; <c>false</c> otherwise.</returns>
#if NETFRAMEWORK
protected virtual
#else
private static
#endif
bool DoesFileExist(string filePath) => File.Exists(filePath);
/// <summary>
/// Loads an assembly from the given path.
/// </summary>
/// <param name="path">The path of the assembly.</param>
/// <returns>The loaded <see cref="Assembly"/>.</returns>
#if NETFRAMEWORK
protected virtual
#else
private static
#endif
// This whole class is not used in source generator mode.
#pragma warning disable IL2026 // Members attributed with RequiresUnreferencedCode may break when trimming
Assembly LoadAssemblyFrom(string path) => Assembly.LoadFrom(path);
#pragma warning restore IL2026 // Members attributed with RequiresUnreferencedCode may break when trimming
#if NETFRAMEWORK
/// <summary>
/// Loads an assembly from the given path in a reflection-only context.
/// </summary>
/// <param name="path">The path of the assembly.</param>
/// <returns>The loaded <see cref="Assembly"/>.</returns>
protected virtual Assembly ReflectionOnlyLoadAssemblyFrom(string path) => Assembly.ReflectionOnlyLoadFrom(path);
#endif
/// <summary>
/// It will search for a particular assembly in the given list of directory.
/// </summary>
/// <param name="searchDirectorypaths"> The search directory paths. </param>
/// <param name="name"> The name. </param>
/// <param name="isReflectionOnly"> Indicates whether this is called under a Reflection Only Load context. </param>
/// <returns> The <see cref="Assembly"/>. </returns>
#if NETFRAMEWORK
protected virtual
#else
private
#endif
Assembly? SearchAssembly(List<string> searchDirectorypaths, string name, bool isReflectionOnly)
{
if (searchDirectorypaths == null || searchDirectorypaths.Count == 0)
{
return null;
}
// args.Name is like: "Microsoft.VisualStudio.TestTools.Common, Version=[VersionMajor].0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
AssemblyName? requestedName;
try
{
// Can throw ArgumentException, FileLoadException if arg is empty/wrong format, etc. Should not return null.
requestedName = new AssemblyName(name);
}
catch (Exception ex)
{
SafeLog(
name,
() =>
{
if (EqtTrace.IsInfoEnabled)
{
EqtTrace.Info(
"MSTest.AssemblyResolver.OnResolve: Failed to create assemblyName '{0}'. Reason: {1} ",
name,
ex);
}
});
return null;
}
DebugEx.Assert(!StringEx.IsNullOrEmpty(requestedName.Name), "MSTest.AssemblyResolver.OnResolve: requested name is empty!");
foreach (string dir in searchDirectorypaths)
{
if (StringEx.IsNullOrEmpty(dir))
{
continue;
}
SafeLog(
name,
() =>
{
if (EqtTrace.IsVerboseEnabled)
{
EqtTrace.Verbose("MSTest.AssemblyResolver.OnResolve: Searching assembly '{0}' in the directory '{1}'", requestedName.Name, dir);
}
});
foreach (string extension in new string[] { ".dll", ".exe" })
{
string assemblyPath = Path.Combine(dir, requestedName.Name + extension);
bool isPushed = false;
bool isResource = requestedName.Name.EndsWith(".resources", StringComparison.InvariantCulture);
if (isResource)
{
// Are we recursively looking up the same resource? Note - our backout code will set
// the ResourceHelper's currentlyLoading stack to null if an exception occurs.
if (s_currentlyLoading != null && s_currentlyLoading.Count > 0 && s_currentlyLoading.LastIndexOf(assemblyPath) != -1)
{
SafeLog(
name,
() =>
{
if (EqtTrace.IsInfoEnabled)
{
EqtTrace.Info("MSTest.AssemblyResolver.OnResolve: Assembly '{0}' is searching for itself recursively '{1}', returning as not found.", name, assemblyPath);
}
});
_resolvedAssemblies[name] = null;
return null;
}
s_currentlyLoading ??= new List<string>();
s_currentlyLoading.Add(assemblyPath); // Push
isPushed = true;
}
Assembly? assembly = SearchAndLoadAssembly(assemblyPath, name, requestedName, isReflectionOnly);
if (isResource && isPushed)
{
DebugEx.Assert(s_currentlyLoading is not null, "_currentlyLoading should not be null");
s_currentlyLoading.RemoveAt(s_currentlyLoading.Count - 1); // Pop
}
if (assembly != null)
{
return assembly;
}
}
}
return null;
}
/// <summary>
/// Verifies that found assembly name matches requested to avoid security issues.
/// Looks only at PublicKeyToken and Version, empty matches anything.
/// </summary>
/// <param name="requestedName"> The requested Name. </param>
/// <param name="foundName"> The found Name. </param>
/// <returns> The <see cref="bool"/>. </returns>
private static bool RequestedAssemblyNameMatchesFound(AssemblyName requestedName, AssemblyName foundName)
{
DebugEx.Assert(requestedName != null, "requested assembly name should not be null.");
DebugEx.Assert(foundName != null, "found assembly name should not be null.");
byte[]? requestedPublicKey = requestedName.GetPublicKeyToken();
if (requestedPublicKey != null)
{
byte[]? foundPublicKey = foundName.GetPublicKeyToken();
if (foundPublicKey == null)
{
return false;
}
for (int i = 0; i < requestedPublicKey.Length; ++i)
{
if (requestedPublicKey[i] != foundPublicKey[i])
{
return false;
}
}
}
return requestedName.Version == null || requestedName.Version.Equals(foundName.Version);
}
#if NETFRAMEWORK
/// <summary>
/// Event handler for windows winmd resolution.
/// </summary>
/// <param name="sender"> The sender App Domain. </param>
/// <param name="args"> The args. </param>
private void WindowsRuntimeMetadataReflectionOnlyNamespaceResolve(object sender, NamespaceResolveEventArgs args)
{
// Note: This will throw on pre-Win8 OS versions
IEnumerable<string> fileNames = WindowsRuntimeMetadata.ResolveNamespace(
args.NamespaceName,
null, // Will use OS installed .winmd files, you can pass explicit Windows SDK path here for searching 1st party WinRT types
_searchDirectories); // You can pass package graph paths, they will be used for searching .winmd files with 3rd party WinRT types
foreach (string fileName in fileNames)
{
args.ResolvedAssemblies.Add(Assembly.ReflectionOnlyLoadFrom(fileName));
}
}
#endif
/// <summary>
/// Assembly Resolve event handler for App Domain - called when CLR loader cannot resolve assembly.
/// </summary>
/// <param name="senderAppDomain"> The sender App Domain. </param>
/// <param name="args"> The args. </param>
/// <param name="isReflectionOnly"> Indicates whether this is called under a Reflection Only Load context. </param>
/// <returns> The <see cref="Assembly"/>. </returns>
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Requirement is to handle all kinds of user exceptions and message appropriately.")]
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "senderAppDomain", Justification = "This is an event handler.")]
#pragma warning disable IDE0060 // Remove unused parameter
private Assembly? OnResolveInternal(object? senderAppDomain, ResolveEventArgs args, bool isReflectionOnly)
#pragma warning restore IDE0060 // Remove unused parameter
{
if (StringEx.IsNullOrEmpty(args.Name))
{
Debug.Fail("MSTest.AssemblyResolver.OnResolve: args.Name is null or empty.");
return null;
}
SafeLog(
args.Name,
() =>
{
if (EqtTrace.IsInfoEnabled)
{
EqtTrace.Info("MSTest.AssemblyResolver.OnResolve: Resolving assembly '{0}'", args.Name);
}
});
string assemblyNameToLoad = AppDomain.CurrentDomain.ApplyPolicy(args.Name);
SafeLog(
assemblyNameToLoad,
() =>
{
if (EqtTrace.IsInfoEnabled)
{
EqtTrace.Info("MSTest.AssemblyResolver.OnResolve: Resolving assembly after applying policy '{0}'", assemblyNameToLoad);
}
});
lock (_syncLock)
{
// Since both normal and reflection only cache are accessed in same block, putting only one lock should be sufficient.
if (TryLoadFromCache(assemblyNameToLoad, isReflectionOnly, out Assembly? assembly))
{
return assembly;
}
assembly = SearchAssembly(_searchDirectories, assemblyNameToLoad, isReflectionOnly);
if (assembly != null)
{
return assembly;
}
// required assembly is not present in searchDirectories??
// see, if we can find it in user specified search directories.
while (assembly == null && _directoryList?.Count > 0)
{
// instead of loading whole search directory in one time, we are adding directory on the basis of need
RecursiveDirectoryPath currentNode = _directoryList.Dequeue();
List<string> incrementalSearchDirectory = [];
if (DoesDirectoryExist(currentNode.DirectoryPath))
{
incrementalSearchDirectory.Add(currentNode.DirectoryPath);
if (currentNode.IncludeSubDirectories)
{
// Add all its sub-directory in depth first search order.
AddSubdirectories(currentNode.DirectoryPath, incrementalSearchDirectory);
}
// Add this directory list in this.searchDirectories so that when we will try to resolve some other
// assembly, then it will look in this whole directory first.
_searchDirectories.AddRange(incrementalSearchDirectory);
assembly = SearchAssembly(incrementalSearchDirectory, assemblyNameToLoad, isReflectionOnly);
}
else
{
// generate warning that path does not exist.
SafeLog(
assemblyNameToLoad,
() =>
{
if (EqtTrace.IsWarningEnabled)
{
EqtTrace.Warning(
"MSTest.AssemblyResolver.OnResolve: the directory '{0}', does not exist",
currentNode.DirectoryPath);
}
});
}
}
if (assembly != null)
{
return assembly;
}
// Try for default load for System dlls that can't be found in search paths. Needs to loaded just by name.
try
{
#if NETFRAMEWORK
if (isReflectionOnly)
{
// Put it in the resolved assembly cache so that if the Load call below
// triggers another assembly resolution, then we don't end up in stack overflow.
_reflectionOnlyResolvedAssemblies[assemblyNameToLoad] = null;
assembly = Assembly.ReflectionOnlyLoad(assemblyNameToLoad);
if (assembly != null)
{
_reflectionOnlyResolvedAssemblies[assemblyNameToLoad] = assembly;
}
return assembly;
}
#endif
// Put it in the resolved assembly cache so that if the Load call below
// triggers another assembly resolution, then we don't end up in stack overflow.
_resolvedAssemblies[assemblyNameToLoad] = null;
assembly = Assembly.Load(assemblyNameToLoad);
if (assembly != null)
{
_resolvedAssemblies[assemblyNameToLoad] = assembly;
}
return assembly;
}
catch (Exception ex)
{
SafeLog(
args.Name,
() =>
{
if (EqtTrace.IsInfoEnabled)
{
EqtTrace.Info("MSTest.AssemblyResolver.OnResolve: Failed to load assembly '{0}'. Reason: {1}", assemblyNameToLoad, ex);
}
});
}
return assembly;
}
}
/// <summary>
/// Load assembly from cache if available.
/// </summary>
/// <param name="assemblyName"> The assembly Name. </param>
/// <param name="isReflectionOnly">Indicates if this is a reflection-only context.</param>
/// <param name="assembly"> The assembly. </param>
/// <returns> The <see cref="bool"/>. </returns>
private bool TryLoadFromCache(string assemblyName, bool isReflectionOnly, out Assembly? assembly)
{
bool isFoundInCache = isReflectionOnly
? _reflectionOnlyResolvedAssemblies.TryGetValue(assemblyName, out assembly)
: _resolvedAssemblies.TryGetValue(assemblyName, out assembly);
if (isFoundInCache)
{
SafeLog(
assemblyName,
() =>
{
if (EqtTrace.IsInfoEnabled)
{
EqtTrace.Info("MSTest.AssemblyResolver.OnResolve: Resolved '{0}'", assemblyName);
}
});
return true;
}
return false;
}
/// <summary>
/// Call logger APIs safely. We do not want a stackoverflow when objectmodel assembly itself
/// is being resolved and an EqtTrace message prompts the load of the same dll again.
/// CLR does not trigger a load when the EqtTrace messages are in a lambda expression. Leaving it that way
/// to preserve readability instead of creating wrapper functions.
/// </summary>
/// <param name="assemblyName">The assembly being resolved.</param>
/// <param name="loggerAction">The logger function.</param>
private static void SafeLog(string? assemblyName, Action loggerAction)
{
// Logger assembly was in `Microsoft.VisualStudio.TestPlatform.ObjectModel` assembly in legacy versions and we need to omit it as well.
if (!StringEx.IsNullOrEmpty(assemblyName)
&& !assemblyName.StartsWith(LoggerAssemblyName, StringComparison.Ordinal)
&& !assemblyName.StartsWith(LoggerAssemblyNameLegacy, StringComparison.Ordinal)
&& !assemblyName.StartsWith(PlatformServicesResourcesName, StringComparison.Ordinal))
{
loggerAction.Invoke();
}
}
/// <summary>
/// Search for assembly and if exists then load.
/// </summary>
/// <param name="assemblyPath"> The assembly Path. </param>
/// <param name="assemblyName"> The assembly Name. </param>
/// <param name="requestedName"> The requested Name. </param>
/// <param name="isReflectionOnly"> Indicates whether this is called under a Reflection Only Load context. </param>
/// <returns> The <see cref="Assembly"/>. </returns>
[SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods", MessageId = "System.Reflection.Assembly.LoadFrom", Justification = "The assembly location is figured out from the configuration that the user passes in.")]
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Requirement is to handle all kinds of user exceptions and message appropriately.")]
private Assembly? SearchAndLoadAssembly(string assemblyPath, string assemblyName, AssemblyName requestedName, bool isReflectionOnly)
{
try
{
if (!DoesFileExist(assemblyPath))
{
return null;
}
var foundName = AssemblyName.GetAssemblyName(assemblyPath);
if (!RequestedAssemblyNameMatchesFound(requestedName, foundName))
{
return null; // File exists but version/public key is wrong. Try next extension.
}
Assembly assembly;
#if NETFRAMEWORK
if (isReflectionOnly)
{
assembly = ReflectionOnlyLoadAssemblyFrom(assemblyPath);
_reflectionOnlyResolvedAssemblies[assemblyName] = assembly;
}
else
#endif
{
assembly = LoadAssemblyFrom(assemblyPath);
_resolvedAssemblies[assemblyName] = assembly;
}
SafeLog(
assemblyName,
() =>
{
if (EqtTrace.IsInfoEnabled)
{
EqtTrace.Info("MSTest.AssemblyResolver.OnResolve: Resolved assembly '{0}'", assemblyName);
}
});
return assembly;
}
catch (FileLoadException ex)
{
SafeLog(
assemblyName,
() =>
{
if (EqtTrace.IsInfoEnabled)
{
EqtTrace.Info("MSTest.AssemblyResolver.OnResolve: Failed to load assembly '{0}'. Reason:{1} ", assemblyName, ex);
}
});
// Re-throw FileLoadException, because this exception means that the assembly
// was found, but could not be loaded. This will allow us to report a more
// specific error message to the user for things like access denied.
throw;
}
catch (Exception ex)
{
// For all other exceptions, try the next extension.
SafeLog(
assemblyName,
() =>
{
if (EqtTrace.IsInfoEnabled)
{
EqtTrace.Info("MSTest.AssemblyResolver.OnResolve: Failed to load assembly '{0}'. Reason:{1} ", assemblyName, ex);
}
});
}
return null;
}
}
#endif