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..1548f1a1412 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 @@ -17,16 +17,15 @@ 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); - if (path.EndsWith(DotDll, StringComparison.OrdinalIgnoreCase) || path.EndsWith(DotExe, StringComparison.OrdinalIgnoreCase)) - { - path = path.Remove(path.Length - DotDll.Length, DotDll.Length); - } + var resolver = new PathAssemblyResolver(paths); + var mlc = new MetadataLoadContext(resolver); + try { - return Assembly.Load(new AssemblyName(path)); + return mlc.LoadFromAssemblyPath(path); } catch (Exception ex) { @@ -49,10 +48,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 @@ +