From dfeae7e80efe29c35104d9e8457a7100054bb7d0 Mon Sep 17 00:00:00 2001 From: Jeff Kluge Date: Thu, 15 Dec 2016 11:52:21 -0800 Subject: [PATCH 1/2] Add AssemblyResolve handler for in-memory assemblies in CodeTaskFactory Closes #954 --- src/XMakeTasks/CodeTaskFactory.cs | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/XMakeTasks/CodeTaskFactory.cs b/src/XMakeTasks/CodeTaskFactory.cs index 6fb9a56841e..071d244e80d 100644 --- a/src/XMakeTasks/CodeTaskFactory.cs +++ b/src/XMakeTasks/CodeTaskFactory.cs @@ -30,6 +30,37 @@ namespace Microsoft.Build.Tasks /// public class CodeTaskFactory : ITaskFactory { + /// + /// This dictionary keeps track of custom references to compiled assemblies. The in-memory assembly is loaded from a byte + /// stream and as such its dependencies cannot be found unless they are in the MSBuild.exe directory or the GAC. They + /// cannot be found even if they are already loaded in the AppDomain. This dictionary knows the FullName of the assembly + /// and a reference to the assembly itself. In the handler, the dictionary + /// is used to return the loaded assemblies as a way to allow custom references that are not in the normal assembly Load + /// context. + /// + private static readonly IDictionary s_knownReferenceAssemblies = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); + + static CodeTaskFactory() + { + // The handler is not detached because it only returns assemblies for custom references that cannot be found in the normal Load context + AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainOnAssemblyResolve; + } + + /// + /// Handles the event to return assemblies loaded from custom references. + /// + /// + /// + /// + private static Assembly CurrentDomainOnAssemblyResolve(object sender, ResolveEventArgs args) + { + Assembly assembly = null; + // Return the assembly loaded from the custom reference if the FullName matches what is being looked for + s_knownReferenceAssemblies.TryGetValue(args.Name, out assembly); + + return assembly; + } + /// /// Default assemblies names to reference during inline code compilation - from the .NET Framework /// @@ -657,6 +688,7 @@ private void AddReferenceAssemblyToReferenceList(List referenceAssemblyL if (candidateAssembly != null) { candidateAssemblyLocation = candidateAssembly.Location; + s_knownReferenceAssemblies.Add(candidateAssembly.FullName, candidateAssembly); } } catch (BadImageFormatException e) From c4b6dcccf8f087450580426b9cc09a63f493e25c Mon Sep 17 00:00:00 2001 From: Jeff Kluge Date: Fri, 16 Dec 2016 14:04:06 -0800 Subject: [PATCH 2/2] Fix issue of adding the same assembly multiple times --- src/XMakeTasks/CodeTaskFactory.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/XMakeTasks/CodeTaskFactory.cs b/src/XMakeTasks/CodeTaskFactory.cs index 071d244e80d..7303f598029 100644 --- a/src/XMakeTasks/CodeTaskFactory.cs +++ b/src/XMakeTasks/CodeTaskFactory.cs @@ -688,7 +688,7 @@ private void AddReferenceAssemblyToReferenceList(List referenceAssemblyL if (candidateAssembly != null) { candidateAssemblyLocation = candidateAssembly.Location; - s_knownReferenceAssemblies.Add(candidateAssembly.FullName, candidateAssembly); + s_knownReferenceAssemblies[candidateAssembly.FullName] = candidateAssembly; } } catch (BadImageFormatException e)