Skip to content

Using MetadataLoadContext to load assmebly #4926

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions src/dotnet-svcutil/lib/src/CommandProcessorOptions.cs
Original file line number Diff line number Diff line change
@@ -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))
20 changes: 8 additions & 12 deletions src/dotnet-svcutil/lib/src/TypeLoader.cs
Original file line number Diff line number Diff line change
@@ -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<string>(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<Type>(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)
{
1 change: 1 addition & 0 deletions src/dotnet-svcutil/lib/src/dotnet-svcutil-lib.csproj
Original file line number Diff line number Diff line change
@@ -64,6 +64,7 @@
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.6.0" />
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
<PackageReference Include="System.Reflection.DispatchProxy" Version="4.4.0" />
<PackageReference Include="System.Reflection.MetadataLoadContext" Version="6.0.0" />
</ItemGroup>

<!-- Include resx files here so that arcade will handle them correctly. -->