|
3 | 3 |
|
4 | 4 | using System;
|
5 | 5 | using System.Collections.Generic;
|
6 |
| -using System.Diagnostics; |
7 | 6 | using System.IO;
|
8 | 7 | using System.Reflection;
|
9 | 8 | using Microsoft.Build.Framework;
|
10 | 9 | using Microsoft.DotNet.PackageValidation;
|
11 | 10 | using Microsoft.NET.Build.Tasks;
|
12 | 11 | using NuGet.RuntimeModel;
|
| 12 | +#if NETCOREAPP |
| 13 | +using System.Runtime.Loader; |
| 14 | +#endif |
13 | 15 |
|
14 | 16 | namespace Microsoft.DotNet.Compatibility
|
15 | 17 | {
|
@@ -43,14 +45,23 @@ public class ValidatePackage : TaskBase
|
43 | 45 |
|
44 | 46 | public override bool Execute()
|
45 | 47 | {
|
| 48 | +#if NETCOREAPP |
| 49 | + AssemblyLoadContext currentContext = AssemblyLoadContext.GetLoadContext(Assembly.GetExecutingAssembly()); |
| 50 | + currentContext.Resolving += ResolverForRoslyn; |
| 51 | +#else |
46 | 52 | AppDomain.CurrentDomain.AssemblyResolve += ResolverForRoslyn;
|
| 53 | +#endif |
47 | 54 | try
|
48 | 55 | {
|
49 | 56 | return base.Execute();
|
50 | 57 | }
|
51 | 58 | finally
|
52 | 59 | {
|
| 60 | +#if NETCOREAPP |
| 61 | + currentContext.Resolving -= ResolverForRoslyn; |
| 62 | +#else |
53 | 63 | AppDomain.CurrentDomain.AssemblyResolve -= ResolverForRoslyn;
|
| 64 | +#endif |
54 | 65 | }
|
55 | 66 | }
|
56 | 67 |
|
@@ -103,19 +114,41 @@ protected override void ExecuteCore()
|
103 | 114 | }
|
104 | 115 | }
|
105 | 116 |
|
| 117 | +#if NETCOREAPP |
| 118 | + private Assembly ResolverForRoslyn(AssemblyLoadContext context, AssemblyName assemblyName) |
| 119 | + { |
| 120 | + return LoadRoslyn(assemblyName, path => context.LoadFromAssemblyPath(path)); |
| 121 | + } |
| 122 | +#else |
106 | 123 | private Assembly ResolverForRoslyn(object sender, ResolveEventArgs args)
|
107 | 124 | {
|
108 | 125 | AssemblyName name = new(args.Name);
|
109 |
| - if (name.Name == "Microsoft.CodeAnalysis" || name.Name == "Microsoft.CodeAnalysis.CSharp") |
| 126 | + return LoadRoslyn(name, path => Assembly.LoadFrom(path)); |
| 127 | + } |
| 128 | +#endif |
| 129 | + |
| 130 | + private Assembly LoadRoslyn(AssemblyName name, Func<string, Assembly> loadFromPath) |
| 131 | + { |
| 132 | + const string codeAnalysisName = "Microsoft.CodeAnalysis"; |
| 133 | + const string codeAnalysisCsharpName = "Microsoft.CodeAnalysis.CSharp"; |
| 134 | + if (name.Name == codeAnalysisName || name.Name == codeAnalysisCsharpName) |
110 | 135 | {
|
111 |
| - Assembly asm = Assembly.LoadFrom(Path.Combine(RoslynAssembliesPath, $"{name.Name}.dll")); |
112 |
| - Version version = asm.GetName().Version; |
113 |
| - if (version < name.Version) |
| 136 | + Assembly asm = loadFromPath(Path.Combine(RoslynAssembliesPath, $"{name.Name}.dll")); |
| 137 | + Version resolvedVersion = asm.GetName().Version; |
| 138 | + if (resolvedVersion < name.Version) |
114 | 139 | {
|
115 |
| - throw new Exception(string.Format(Resources.UpdateSdkVersion, version, name.Version)); |
| 140 | + throw new Exception(string.Format(Resources.UpdateSdkVersion, resolvedVersion, name.Version)); |
116 | 141 | }
|
| 142 | + |
| 143 | + // Being extra defensive but we want to avoid that we accidentally load two different versions of either |
| 144 | + // of the roslyn assemblies from a different location, so let's load them both on the first request. |
| 145 | + Assembly _ = name.Name == codeAnalysisName ? |
| 146 | + loadFromPath(Path.Combine(RoslynAssembliesPath, $"{codeAnalysisCsharpName}.dll")) : |
| 147 | + loadFromPath(Path.Combine(RoslynAssembliesPath, $"{codeAnalysisName}.dll")); |
| 148 | + |
117 | 149 | return asm;
|
118 | 150 | }
|
| 151 | + |
119 | 152 | return null;
|
120 | 153 | }
|
121 | 154 | }
|
|
0 commit comments