NuGet 包中的运行时异常引用类型:'无法加载文件或程序集'Foo'。该系统找不到指定的文件。'

Posted

技术标签:

【中文标题】NuGet 包中的运行时异常引用类型:\'无法加载文件或程序集\'Foo\'。该系统找不到指定的文件。\'【英文标题】:Runtime exception referencing type from a NuGet package: 'Could not load file or assembly 'Foo'. The system cannot find the file specified.'NuGet 包中的运行时异常引用类型:'无法加载文件或程序集'Foo'。该系统找不到指定的文件。' 【发布时间】:2021-12-28 21:49:23 【问题描述】:

背景:

    消费应用是使用 .NET Core 3.1 构建的网站。 使用应用程序引用使用 .NET Standard 2.0 构建的 NuGet 包。 消费应用程序引用了最新版本的 NuGet 包,并且构建时没有任何错误或警告。 NuGet 包中的 DLL 在 bin\debug\netcoreapp31\bin\release\netcoreapp31\ 文件夹中可见。 在 IDE 中,可以使用 NuGet 包中的类型的完整 IntelliSense,包括类型、方法和参数的 XML 文档 cmets。 在运行时,第一次引用 NuGet 包中的任何类型时,会引发 FileNotFound 异常,报告无法找到 NuGet 包文件中的 DLL。

我尝试过的:

    验证.csproj 文件中的PackageReference 条目是否正确。由于它是一个 .NET Core 应用程序,因此没有提示路径或程序集绑定重定向会干扰程序集绑定。 正在清除 NuGet 缓存。 删除并重新添加 NuGet 包。 强制 NuGet 通过包管理器控制台重新安装包。 清理和重建解决方案。 启用 Fusion Log Viewer 并梳理日志。这就是事情变得有趣的地方。 日志中没有任何条目表明任何尝试解析或加载程序集,或任何条目表明程序集绑定失败。

异常消息

Could not load file or assembly 'OneCall.FeatureFlags, Version=1.0.0.0, Culture=en-US, PublicKeyToken=null'. The system cannot find the file specified.
   at PTWebAPI.Startup.ConfigureServices(IServiceCollection services) in C:\Dev\PTWebAPI\PTWebAPI\Startup.cs:line 65
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.InvokeCore(Object instance, IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass9_0.<Invoke>g__Startup|0(IServiceCollection serviceCollection)
   at Microsoft.AspNetCore.Hosting.StartupLoader.ConfigureServicesDelegateBuilder`1.<>c__DisplayClass15_0.<BuildStartupServicesFilterPipeline>g__RunPipeline|0(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.Invoke(Object instance, IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass8_0.<Build>b__0(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.StartupLoader.ConfigureServicesDelegateBuilder`1.<>c__DisplayClass14_0.<ConfigureServices>g__ConfigureServicesWithContainerConfiguration|0(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.WebHost.EnsureApplicationServices()
   at Microsoft.AspNetCore.Hosting.WebHost.Initialize()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at PTWebAPI.Program.Main(String[] args) in C:\Dev\PTWebAPI\PTWebAPI\Program.cs:line 15

故障点

public static void Main(string[] args)

   CreateWebHostBuilder(args).Build().Run(); // <-- Right here


问题

在 .NET Core 应用程序中加载程序集时导致此特定异常的原因是什么?如何解决?

【问题讨论】:

你检查“TargetFramework”了吗,我遇到了一些不匹配的问题。 也许......它就像包中的类/属性中的某些东西由于较新的版本和链接而丢失,但由于动态调用而在运行时失败......,是包设置为 [Get] the latest 并拉入您期望的内容而不是 least version?在这里查看我的答案:System.MissingMethodException: Method not found? @JeroenvanLangen TargetFramework 应该不是问题。但是,消费应用程序以 .NET Core 3.1 为目标,而 NuGet 包以 .NET Standard 2.0 为目标。据我了解,.NET Core 应用程序在使用 .NET Standard 2.0 NuGet 包时应该没有任何问题。 值得一试,下一步,是创建一个可重复性最低的示例......也许创建它会提示您真正的问题是什么。 :-/ 您确定您尝试加载的程序集的文化在参考中是相同的吗?我看到您正在尝试加载带有文化 en-us 的程序集,也许 nuget 包中的程序集具有不同的文化,中性说。此外,如果引用程序集经过数字签名,则无法加载未签名的程序集。 【参考方案1】:

问题原来是 NuGet 包本身的文化。

NuGet 包上的区域性设置为“en-US”,而消费程序集上的区域性设置为中性 ("")。这会阻止使用程序集在运行时加载 NuGet 包的程序集。

解决方案是将 NuGet 包上的区域性设置为中性 (""),重新发布包,并更新消费程序集中的包引用。

(感谢Hamlet Hakobyan 为我指明了正确的方向。)

【讨论】:

以上是关于NuGet 包中的运行时异常引用类型:'无法加载文件或程序集'Foo'。该系统找不到指定的文件。'的主要内容,如果未能解决你的问题,请参考以下文章

无法加载从带有标识符的包中的笔尖引用的图像

无法加载从带有标识符的包中的笔尖引用的“”图像

使用 FHIR nuget 包通过引用查找包中的资源

nuget 包中 exe 文件的位置和 nuspec 中匹配的文件标记

TeamCity - MSBuild无法找到NuGet引用

如何调试我创建的 NuGet 包中的代码