在ASP.NET MVC 5中,我使用BuildManager.GetReferencedAssemblies()
方法将所有程序集获取到bin文件夹中,并在依赖项启动之前将其加载,因此可以扫描和注入所有dll。
ASP.NET Core中是否有替代方法?
我尝试了这段代码,但是它开始确实给了我加载错误,例如找不到文件异常。
foreach (var compilationLibrary in deps.CompileLibraries)
{
foreach (var resolveReferencePath in compilationLibrary.ResolveReferencePaths())
{
Console.WriteLine($"\t\tReference path: {resolveReferencePath}");
dlls.Add(resolveReferencePath);
}
}
dlls = dlls.Distinct().ToList();
var infolder = dlls.Where(x => x.Contains(Directory.GetCurrentDirectory())).ToList();
foreach (var item in infolder)
{
try
{
Assembly.LoadFile(item);
}
catch (System.IO.FileLoadException loadEx)
{
} // The Assembly has already been loaded.
catch (BadImageFormatException imgEx)
{
} // If a BadImageFormatException exception is thrown, the file is not an assembly.
catch (Exception ex)
{
}
}
答案 0 :(得分:0)
我设法创建了一个解决方案,但是我不确定它是否能解决所有情况。
我将发布为“在我的机器解决方案上工作”
最大的不同是从Assembly.LoadFile到AssemblyLoadContext.Default.LoadFromAssemblyPath的更改
我用这些文字作为研究
https://natemcmaster.com/blog/2018/07/25/netcore-plugins/ https://github.com/dotnet/coreclr/blob/v2.1.0/Documentation/design-docs/assemblyloadcontext.md
var dlls = DependencyContext.Default.CompileLibraries
.SelectMany(x => x.ResolveReferencePaths())
.Distinct()
.Where(x => x.Contains(Directory.GetCurrentDirectory()))
.ToList();
foreach (var item in dlls)
{
try
{
AssemblyLoadContext.Default.LoadFromAssemblyPath(item);
}
catch (System.IO.FileLoadException loadEx)
{
} // The Assembly has already been loaded.
catch (BadImageFormatException imgEx)
{
} // If a BadImageFormatException exception is thrown, the file is not an assembly.
catch (Exception ex)
{
}
}