1

I have a class library in which I load dynamic assemblies. How can I load these assemblies using the new MetaDataLoadContext and MetaDataAssemblyResolver?

Code:

// Retrieve the domain assembly used by the compilation
Assembly baseAssembly = typeof(MyType).Assembly;

// Retrieve the location of the assembly and the referenced assemblies used by the domain
AssemblyName[] baseAssemblyReferences = baseAssembly.GetReferencedAssemblies();

var paths = new List<string>();

foreach (var item in baseAssemblyReferences)
{
    paths.Add(item.Name + ".dll");
}

paths.Remove("mscorlib.dll");
paths.Remove("System.Runtime.dll");
paths.Remove("netstandard.dll");
paths.Remove("System.Core.dll");
paths.Remove("System.dll");

// Create PathAssemblyResolver that can resolve assemblies using the created list.
var resolver = new PathAssemblyResolver(paths);

var mlc = new MetadataLoadContext(resolver, baseAssmebly.GetName().Name);

I am getting an error:

Could not find assembly 'MYAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. Either explicitly load this assembly using a method such as LoadFromAssemblyPath() or use a Metadata AssemblyResolver that returns a valid assembly

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
tRuEsAtM
  • 3,517
  • 6
  • 43
  • 83
  • I have the exact same problem: https://stackoverflow.com/questions/74914267/windsor-resolve-issue-asp-net-core-web-api-net-version-6-cannot-register-my Please, what was your solution? – codingjoe Jan 05 '23 at 18:25

0 Answers0