Skip to content

Commit

Permalink
The default load assembly is now whichever loaded the assembly of the…
Browse files Browse the repository at this point in the history
… task factory, instead of hard-coded AssemblyLoadContext.Default (in anticipation of dotnet/msbuild#1754 ).
  • Loading branch information
Stanislav Muhametsin committed Jun 3, 2017
1 parent 107109c commit 7b9988d
Showing 1 changed file with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using NuGet.Packaging;
using System.Xml.Linq;
using System.IO;
using System.Runtime.Loader;

namespace UtilPack.NuGet.MSBuild
{
Expand Down Expand Up @@ -56,11 +57,12 @@ String[] repoPaths
}

// We must subclass System.Runtime.Loader.AssemblyLoadContext, since the default load context does not seem to launch Resolve event for System.* assemblies (and thus we never catch load request for e.g. System.Threading.Tasks.Extensions assembly, since UtilPack references newer than is in e.g. 1.1.2 app folder).
private sealed class NuGetTaskLoadContext : System.Runtime.Loader.AssemblyLoadContext, IDisposable
private sealed class NuGetTaskLoadContext : AssemblyLoadContext, IDisposable
{
private readonly CommonAssemblyRelatedHelper _helper;
private readonly ConcurrentDictionary<AssemblyName, Lazy<Assembly>> _loadedAssemblies;
private readonly ISet<String> _frameworkAssemblySimpleNames;
private readonly AssemblyLoadContext _defaultLoadContext;

public NuGetTaskLoadContext(
CommonAssemblyRelatedHelper helper,
Expand All @@ -70,6 +72,7 @@ String[] repoPaths
)
{
this._helper = helper;
this._defaultLoadContext = GetLoadContext( this.GetType().GetTypeInfo().Assembly );
this._loadedAssemblies = new ConcurrentDictionary<AssemblyName, Lazy<Assembly>>(
ComparerFromFunctions.NewEqualityComparer<AssemblyName>(
( x, y ) => String.Equals( x.Name, y.Name ) && String.Equals( x.CultureName, y.CultureName ) && x.Version.Equals( y.Version ) && ArrayEqualityComparer<Byte>.ArrayEquality( x.GetPublicKeyToken(), y.GetPublicKeyToken() ),
Expand Down Expand Up @@ -127,7 +130,7 @@ protected override Assembly Load( AssemblyName assemblyName )
// Otherwise we will get exceptions in Release build, since e.g. UtilPack will have exactly same full assembly name (since its public key won't be null), and since this assembly was loaded using LoadFromAssemblyPath by MSBuild, it will fail to resolve the dependencies (at least System.Threading.Tasks.Extensions).
try
{
retVal = Default.LoadFromAssemblyName( assemblyName );
retVal = this._defaultLoadContext.LoadFromAssemblyName( assemblyName );
}
catch
{
Expand Down

0 comments on commit 7b9988d

Please sign in to comment.