Ubuntu下DotNet无法加载TappCommon 1.0.18.0程序集求助
问题描述
在Ubuntu环境中使用.NET开发多个微服务,多数服务运行正常,但某一特定微服务启动时抛出错误,提示无法加载程序集TappCommon, Version=1.0.18.0, Culture=neutral, PublicKeyToken=null。已确认所有直接引用的NuGet库均为1.0.20版本,需排查旧版本引用的来源。
错误日志
ASPNETCORE_ENVIRONMENT=Local AppSettingsFile=appsettings.json AppSettingsFile=appsettings.Local.json crit: Microsoft.AspNetCore.Hosting.Diagnostics[6] Application startup exception Could not load file or assembly 'TappCommon, Version=1.0.18.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified. System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Could not load file or assembly 'TappCommon, Version=1.0.18.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified. at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module) at System.Reflection.RuntimeAssembly.get_DefinedTypes() at Microsoft.AspNetCore.Mvc.ApplicationParts.AssemblyPart.get_Types() at Microsoft.AspNetCore.Mvc.Controllers.ControllerFeatureProvider.PopulateFeature(IEnumerable`1 parts, ControllerFeature feature) at Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager.PopulateFeature[TFeature](TFeature feature) at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider.GetControllerTypes() at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider.GetDescriptors() at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider.OnProvidersExecuting(ActionDescriptorProviderContext context) at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.UpdateCollection() at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.Initialize() at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointDataSourceBase.<>c__DisplayClass11_0.<Subscribe>b__0() at Microsoft.Extensions.Primitives.ChangeToken.ChangeTokenRegistration`1..ctor(Func`1 changeTokenProducer, Action`1 changeTokenConsumer, TState state) at Microsoft.Extensions.Primitives.ChangeToken.OnChange(Func`1 changeTokenProducer, Action changeTokenConsumer) at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointDataSourceBase.Subscribe() at Microsoft.AspNetCore.Mvc.Routing.ControllerActionEndpointDataSource..ctor(ControllerActionEndpointDataSourceIdProvider dataSourceIdProvider, IActionDescriptorCollectionProvider actions, ActionEndpointFactory endpointFactory, OrderedEndpointsSequenceProvider orderSequence) at Microsoft.AspNetCore.Builder.ControllerEndpointRouteBuilderExtensions.GetOrCreateDataSource(IEndpointRouteBuilder endpoints) at Microsoft.AspNetCore.Builder.ControllerEndpointRouteBuilderExtensions.MapControllers(IEndpointRouteBuilder endpoints) at Notification.WebApi.Startup.<>c.<Configure>b__7_1(IEndpointRouteBuilder endpoints) in /home/abel/Projects/TappEngine/LiveInvesting/Notification/Presentation/Notification.WebApi/Startup.cs:line 134 at Microsoft.AspNetCore.Builder.EndpointRoutingApplicationBuilderExtensions.UseEndpoints(IApplicationBuilder builder, Action`1 configure) at Notification.WebApi.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env) in /home/abel/Projects/TappEngine/LiveInvesting/Notification/Presentation/Notification.WebApi/Startup.cs:line 132 at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& 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.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder) at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.<Build>b__0(IApplicationBuilder builder) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass15_0.<UseStartup>b__1(IApplicationBuilder app) at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder) at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app) at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken) System.IO.FileNotFoundException: Could not load file or assembly 'TappCommon, Version=1.0.18.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified. File name: 'TappCommon, Version=1.0.18.0, Culture=neutral, PublicKeyToken=null' Unhandled exception. System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Could not load file or assembly 'TappCommon, Version=1.0.18.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified. at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module) at System.Reflection.RuntimeAssembly.get_DefinedTypes() at Microsoft.AspNetCore.Mvc.ApplicationParts.AssemblyPart.get_Types() at Microsoft.AspNetCore.Mvc.Controllers.ControllerFeatureProvider.PopulateFeature(IEnumerable`1 parts, ControllerFeature feature) at Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager.PopulateFeature[TFeature](TFeature feature) at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider.GetControllerTypes() at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider.GetDescriptors() at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider.OnProvidersExecuting(ActionDescriptorProviderContext context) at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.UpdateCollection() at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.Initialize() at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointDataSourceBase.<>c__DisplayClass11_0.<Subscribe>b__0() at Microsoft.Extensions.Primitives.ChangeToken.ChangeTokenRegistration`1..ctor(Func`1 changeTokenProducer, Action`1 changeTokenConsumer, TState state) at Microsoft.Extensions.Primitives.ChangeToken.OnChange(Func`1 changeTokenProducer, Action changeTokenConsumer) at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointDataSourceBase.Subscribe() at Microsoft.AspNetCore.Mvc.Routing.ControllerActionEndpointDataSource..ctor(ControllerActionEndpointDataSourceIdProvider dataSourceIdProvider, IActionDescriptorCollectionProvider actions, ActionEndpointFactory endpointFactory, OrderedEndpointsSequenceProvider orderSequence) at Microsoft.AspNetCore.Builder.ControllerEndpointRouteBuilderExtensions.GetOrCreateDataSource(IEndpointRouteBuilder endpoints) at Microsoft.AspNetCore.Builder.ControllerEndpointRouteBuilderExtensions.MapControllers(IEndpointRouteBuilder endpoints) at Notification.WebApi.Startup.<>c.<Configure>b__7_1(IEndpointRouteBuilder endpoints) in /home/abel/Projects/TappEngine/LiveInvesting/Notification/Presentation/Notification.WebApi/Startup.cs:line 134 at Microsoft.AspNetCore.Builder.EndpointRoutingApplicationBuilderExtensions.UseEndpoints(IApplicationBuilder builder, Action`1 configure) at Notification.WebApi.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env) in /home/abel/Projects/TappEngine/LiveInvesting/Notification/Presentation/Notification.WebApi/Startup.cs:line 132 at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& 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.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder) at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.<Build>b__0(IApplicationBuilder builder) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass15_0.<UseStartup>b__1(IApplicationBuilder app) at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder) at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app) at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken) at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken) at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token) at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token) at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host) at Notification.WebApi.Program.Main(String[] args) in /home/abel/Projects/TappEngine/LiveInvesting/Notification/Presentation/Notification.WebApi/Program.cs:line 9 System.IO.FileNotFoundException: Could not load file or assembly 'TappCommon, Version=1.0.18.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified. File name: 'TappCommon, Version=1.0.18.0, Culture=neutral, PublicKeyToken=null'
排查方法
1. 分析依赖树,定位间接引用来源
在问题微服务的项目目录下执行以下命令,查看所有直接和间接的NuGet依赖:
dotnet list package --include-transitive | grep TappCommon
该命令会列出所有包含TappCommon的依赖项,包括间接引用的包,从中可以定位到哪个包引用了1.0.18.0版本。
如果需要更详细的还原日志,执行:
dotnet msbuild /t:Restore /v:d > restore.log
然后在restore.log中搜索"TappCommon",查看版本匹配过程和来源。
2. 检查项目依赖清单文件
打开项目目录下的obj/project.assets.json文件(NuGet还原后的依赖清单),搜索"TappCommon":
- 查看
version字段确认实际使用的版本 - 查看
dependencies节点,找到引用该版本的父依赖包
同时检查项目的.csproj文件,确认所有PackageReference中TappCommon的版本都是1.0.20,没有硬编码旧版本。
3. 启用程序集绑定日志,追踪加载过程
在Ubuntu终端中设置以下环境变量,开启.NET程序集绑定日志:
export COMPlus_LogEnable=1 export COMPlus_LogLevel=1 export COMPlus_LogToFile=1 export COMPlus_LogPath=/tmp/dotnet_binding.log
然后启动微服务,运行失败后查看/tmp/dotnet_binding.log,日志中会详细记录程序集加载的每一步,包括尝试加载的版本、查找路径和失败原因,能精准定位旧版本请求的来源。
4. 清理编译残留和NuGet缓存
- 手动删除项目的
bin和obj文件夹,重新编译 - 清理NuGet本地缓存,避免旧版本缓存干扰:
dotnet nuget locals all --clear
执行后重新运行dotnet restore和dotnet build。
5. 检查本地项目引用
如果TappCommon是本地项目(而非NuGet包),检查解决方案中其他引用了该项目的微服务,确认这些项目是否正确引用了最新版本的TappCommon,且输出的dll版本为1.0.20。
内容的提问来源于stack exchange,提问作者ancm

