无法加载文件或程序集
Could not load file or asssembly
我正在将 .Net 3.5 控制台应用程序从 vs2008 移植到 vs2017 中的 .Net Core 控制台应用程序(目标框架 netcoreapp1.1)。
该程序通过在给定目录中查找 .dll 并将它们作为程序集加载来加载一些插件。
我已经将插件重建为 netstandard1.6 库。诚然,我对核心、框架和标准之间的差异感到有些困惑。
我正在使用 System.Runtime.Loader (v4.3.0) NuGet 包和以下代码来尝试从给定路径加载程序集:
public static Assembly LoadAssemblyFromPath(string path)
{
AssemblyLoadContext.Default.Resolving += (context, name) =>
{
// avoid loading *.resources dlls, because of: https://github.com/dotnet/coreclr/issues/8416
if (name.Name.EndsWith("resources"))
return null;
string[] foundDlls =
Directory.GetFileSystemEntries(new FileInfo(path).FullName, name.Name + ".dll", SearchOption.AllDirectories);
return foundDlls.Any() ? context.LoadFromAssemblyPath(foundDlls[0]) : context.LoadFromAssemblyName(name);
};
return AssemblyLoadContext.Default.LoadFromAssemblyPath(path);
}
我已验证路径参数正确,并且文件存在,但我仍然遇到 "Could not load file or assembly" 异常。从未引发 Resolving 事件。
任何人都可以对我做错了什么提供任何见解吗?
我最终使用了以下内容:
public static Assembly LoadAssemblyFromPath(string path)
{
string fileNameWithOutExtension = Path.GetFileNameWithoutExtension(path);
bool inCompileLibraries = DependencyContext.Default.CompileLibraries.Any(l => l.Name.Equals(fileNameWithOutExtension, StringComparison.OrdinalIgnoreCase));
bool inRuntimeLibraries = DependencyContext.Default.RuntimeLibraries.Any(l => l.Name.Equals(fileNameWithOutExtension, StringComparison.OrdinalIgnoreCase));
return inCompileLibraries || inRuntimeLibraries
? Assembly.Load(new AssemblyName(fileNameWithOutExtension))
: AssemblyLoadContext.Default.LoadFromAssemblyPath(path);
}
我正在将 .Net 3.5 控制台应用程序从 vs2008 移植到 vs2017 中的 .Net Core 控制台应用程序(目标框架 netcoreapp1.1)。
该程序通过在给定目录中查找 .dll 并将它们作为程序集加载来加载一些插件。
我已经将插件重建为 netstandard1.6 库。诚然,我对核心、框架和标准之间的差异感到有些困惑。
我正在使用 System.Runtime.Loader (v4.3.0) NuGet 包和以下代码来尝试从给定路径加载程序集:
public static Assembly LoadAssemblyFromPath(string path)
{
AssemblyLoadContext.Default.Resolving += (context, name) =>
{
// avoid loading *.resources dlls, because of: https://github.com/dotnet/coreclr/issues/8416
if (name.Name.EndsWith("resources"))
return null;
string[] foundDlls =
Directory.GetFileSystemEntries(new FileInfo(path).FullName, name.Name + ".dll", SearchOption.AllDirectories);
return foundDlls.Any() ? context.LoadFromAssemblyPath(foundDlls[0]) : context.LoadFromAssemblyName(name);
};
return AssemblyLoadContext.Default.LoadFromAssemblyPath(path);
}
我已验证路径参数正确,并且文件存在,但我仍然遇到 "Could not load file or assembly" 异常。从未引发 Resolving 事件。
任何人都可以对我做错了什么提供任何见解吗?
我最终使用了以下内容:
public static Assembly LoadAssemblyFromPath(string path)
{
string fileNameWithOutExtension = Path.GetFileNameWithoutExtension(path);
bool inCompileLibraries = DependencyContext.Default.CompileLibraries.Any(l => l.Name.Equals(fileNameWithOutExtension, StringComparison.OrdinalIgnoreCase));
bool inRuntimeLibraries = DependencyContext.Default.RuntimeLibraries.Any(l => l.Name.Equals(fileNameWithOutExtension, StringComparison.OrdinalIgnoreCase));
return inCompileLibraries || inRuntimeLibraries
? Assembly.Load(new AssemblyName(fileNameWithOutExtension))
: AssemblyLoadContext.Default.LoadFromAssemblyPath(path);
}