From 783fa3240e7001b0b1663c4c0bae1fe8029762d5 Mon Sep 17 00:00:00 2001 From: Zhaodong Tian Date: Fri, 30 Sep 2022 02:57:01 -0700 Subject: [PATCH 1/2] Using MetadataLoadContext to load assmebly --- .../lib/src/CommandProcessorOptions.cs | 24 +++++++++---------- src/dotnet-svcutil/lib/src/TypeLoader.cs | 15 +++++++----- .../lib/src/dotnet-svcutil-lib.csproj | 1 + 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/dotnet-svcutil/lib/src/CommandProcessorOptions.cs b/src/dotnet-svcutil/lib/src/CommandProcessorOptions.cs index 130dc62a16b..466407aa31d 100644 --- a/src/dotnet-svcutil/lib/src/CommandProcessorOptions.cs +++ b/src/dotnet-svcutil/lib/src/CommandProcessorOptions.cs @@ -9,6 +9,7 @@ using System.IO; using System.Linq; using System.Reflection; +using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; @@ -807,25 +808,22 @@ private void LoadReferencedAssemblies() { // we should not load the ServiceModel assemblies as types will clash with the private code types. var loadableReferences = this.References.Where(r => !TargetFrameworkHelper.ServiceModelPackages.Any(s => s.Name == r.Name)); + + string projFolder = Path.Combine(this.BootstrapPath.FullName, nameof(SvcutilBootstrapper)); + DirectoryInfo directoryInfo = new DirectoryInfo(projFolder); + + foreach (ProjectDependency reference in loadableReferences) { Assembly assembly = null; - if (this.ToolContext == OperationalContext.Infrastructure) - { - string projFolder = Path.Combine(this.BootstrapPath.FullName, nameof(SvcutilBootstrapper)); - DirectoryInfo directoryInfo = new DirectoryInfo(projFolder); - FileInfo assemblyFile = directoryInfo.GetFiles(reference.AssemblyName + ".*", SearchOption.AllDirectories).FirstOrDefault(); - if (assemblyFile != null) - { - assembly = Assembly.LoadFrom(assemblyFile.FullName); - } - } - else + FileInfo assemblyFile = directoryInfo.GetFiles(reference.AssemblyName + ".*", SearchOption.AllDirectories).FirstOrDefault(); + + if (assemblyFile != null) { - assembly = TypeLoader.LoadAssembly(reference.AssemblyName); + assembly = TypeLoader.LoadAssembly(assemblyFile.FullName); } - + if (assembly != null) { if (!this.ReferencedAssemblies.Contains(assembly)) diff --git a/src/dotnet-svcutil/lib/src/TypeLoader.cs b/src/dotnet-svcutil/lib/src/TypeLoader.cs index e13935fea20..77904fc1afa 100644 --- a/src/dotnet-svcutil/lib/src/TypeLoader.cs +++ b/src/dotnet-svcutil/lib/src/TypeLoader.cs @@ -5,8 +5,8 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Reflection; +using System.Runtime.InteropServices; using System.Runtime.Serialization; namespace Microsoft.Tools.ServiceModel.Svcutil @@ -20,13 +20,19 @@ static public Assembly LoadAssembly(string path) string DotDll = ".dll"; string DotExe = ".exe"; + string[] runtimeAssemblies = Directory.GetFiles(RuntimeEnvironment.GetRuntimeDirectory(), "*.dll"); + var paths = new List(runtimeAssemblies); + + var resolver = new PathAssemblyResolver(paths); + var mlc = new MetadataLoadContext(resolver); + if (path.EndsWith(DotDll, StringComparison.OrdinalIgnoreCase) || path.EndsWith(DotExe, StringComparison.OrdinalIgnoreCase)) { path = path.Remove(path.Length - DotDll.Length, DotDll.Length); } try { - return Assembly.Load(new AssemblyName(path)); + return mlc.LoadFromAssemblyPath(path); } catch (Exception ex) { @@ -49,10 +55,7 @@ static public Type[] LoadTypes(Assembly assembly, Verbosity verbosity) listType.AddRange(Array.FindAll(rtle.Types, delegate (Type t) { return t != null; - })); - - //type.Module or type.Assembly could throw if multiple assembly with same name get referenced but only one version get restored. - listType = listType.Except(GetUnAvailableTypes(listType)).ToList(); + })); if (verbosity > Verbosity.Normal) { diff --git a/src/dotnet-svcutil/lib/src/dotnet-svcutil-lib.csproj b/src/dotnet-svcutil/lib/src/dotnet-svcutil-lib.csproj index 9d724e1d977..b8e8c27c2fd 100644 --- a/src/dotnet-svcutil/lib/src/dotnet-svcutil-lib.csproj +++ b/src/dotnet-svcutil/lib/src/dotnet-svcutil-lib.csproj @@ -64,6 +64,7 @@ + From 7432768da1b3867af93366e2693cf344d91ae491 Mon Sep 17 00:00:00 2001 From: Zhaodong Tian Date: Thu, 13 Oct 2022 03:02:23 -0700 Subject: [PATCH 2/2] Fixed --- src/dotnet-svcutil/lib/src/TypeLoader.cs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/dotnet-svcutil/lib/src/TypeLoader.cs b/src/dotnet-svcutil/lib/src/TypeLoader.cs index 77904fc1afa..1548f1a1412 100644 --- a/src/dotnet-svcutil/lib/src/TypeLoader.cs +++ b/src/dotnet-svcutil/lib/src/TypeLoader.cs @@ -17,19 +17,12 @@ internal static class TypeLoader static public Assembly LoadAssembly(string path) { - string DotDll = ".dll"; - string DotExe = ".exe"; - string[] runtimeAssemblies = Directory.GetFiles(RuntimeEnvironment.GetRuntimeDirectory(), "*.dll"); var paths = new List(runtimeAssemblies); var resolver = new PathAssemblyResolver(paths); var mlc = new MetadataLoadContext(resolver); - - if (path.EndsWith(DotDll, StringComparison.OrdinalIgnoreCase) || path.EndsWith(DotExe, StringComparison.OrdinalIgnoreCase)) - { - path = path.Remove(path.Length - DotDll.Length, DotDll.Length); - } + try { return mlc.LoadFromAssemblyPath(path);