I'm trying to explore an assembly that belongs to an ASP.NET Core MVC project. The project was made with .NET 5.
The problem is, when I try to explore the assembly's types, it throws a FileNotFoundException
with the following message: Could not load file or assembly 'Microsoft.AspNetCore.Mvc.Core, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. El sistema no puede encontrar el archivo especificado
Could not load file or assembly 'Microsoft.AspNetCore.Mvc.Core, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. El sistema no puede encontrar el archivo especificado
.
Issuing a dotnet build
command compiles the project without problems.
Exploring the corresponding.csproj file, I found that the Microsoft.AspNetCore.Mvc.Core
dependency is not declared in the project file, but the SDK indicated in the project file's XML root is Microsoft.NET.Sdk.Web
, and I understand that marks the project as an ASP.NET Core project, but I can't find a way to bring the necessary dependencies to load the assembly.
Code as follows.
Code that loads the assembly:
var reader_context = new ReaderLoadContext(artifact_dir);
var assembly_path = ""; //Path of the compiled assembly
var assembly = reader_context.LoadFromAssemblyPath(assembly_path);
//Here's where it crashes
var target_types = assembly.ExportedTypes.Where(t => t.BaseType != null && t.BaseType.Name == "ControllerBase").ToArray();
ReaderLoadContext
's code:
public class ReaderLoadContext : AssemblyLoadContext
{
private AssemblyDependencyResolver _resolver;
public ReaderLoadContext(string readerLocation)
{
_resolver = new AssemblyDependencyResolver(readerLocation);
}
protected override Assembly Load(AssemblyName assemblyName)
{
var assembly_path = _resolver.ResolveAssemblyToPath(assemblyName);
if (assembly_path != null)
{
return LoadFromAssemblyPath(assembly_path);
}
return null;
}
protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
{
var library_path = _resolver.ResolveUnmanagedDllToPath(unmanagedDllName);
if (library_path != null)
{
return LoadUnmanagedDllFromPath(library_path);
}
return IntPtr.Zero;
}
}
Turns out this is a known limitation of AssemblyLoadContext and one of the solutions (more like an ugly workaround) is to add a reference to the missing framework in the application that explores the desired assembly. I added a PackageReference
item in my app's.csproj file and now it properly loads the assembly I want to scan.
I don't know if I should mark this as an answer. In one hand, it does what I need it to. OTOH, it forces the developer to add all possible framework references beforehand just in case, and if Microsoft (or a 3rd party) releases another framework, it requires releasing a new version of the app.
So hopefully someone knows a more elegant solution.
Thanks to @deepak-msft and everyone else for the help.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.