You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

切换分支遇System.Collections.Immutable加载错误,除重装包外有何替代方案?

问题描述

在两个分支间切换开发,其中一个分支新增了System.Collections.Immutable包。切回另一分支时,始终出现如下错误:

"Server Error in '/' Application."
Could not load file or assembly 'System.Collections.Immutable, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
"Description": An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

"Exception Details": System.IO.FileNotFoundException: Could not load file or assembly 'System.Collections.Immutable, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

"Source Error":

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

"Assembly Load Trace": The following information can be helpful to determine why the assembly 'System.Collections.Immutable, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' could not be loaded.


=== Pre-bind state information ===
LOG: "DisplayName" = System.Collections.Immutable, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
 (Fully-specified)
LOG: "Appbase" = file:///C:/SomePath
LOG: "Initial PrivatePath" = C:\SomePath\bin
Calling assembly : Microsoft.CodeAnalysis.Workspaces, Version=4.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: "Using application configuration file": C:\tret\dsdsdsdsd\web.config
LOG: "Using host configuration file": C:\Users\user\Documents\IISExpress\config\aspnet.config
LOG: "Using machine configuration file from": C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: "Post-policy reference": System.Collections.Immutable, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: The same bind was seen before, and was failed with hr = 0x80070002.

[FileNotFoundException: Could not load file or assembly 'System.Collections.Immutable, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.]
   System.ModuleHandle.ResolveMethod(RuntimeModule module, Int32 methodToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount) +0
   System.ModuleHandle.ResolveMethodHandleInternalCore(RuntimeModule module, Int32 methodToken, IntPtr[] typeInstantiationContext, Int32 typeInstCount, IntPtr[] methodInstantiationContext, Int32 methodInstCount) +162
   System.ModuleHandle.ResolveMethodHandleInternal(RuntimeModule module, Int32 methodToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext) +96
   System.Reflection.CustomAttributeData..ctor(RuntimeModule scope, CustomAttributeRecord caRecord) +79
   System.Reflection.CustomAttributeData.GetCustomAttributes(RuntimeModule module, Int32 tkTarget) +119
   System.Reflection.CustomAttributeData.GetCustomAttributesInternal(RuntimeAssembly target) +83
   Owin.Loader.DefaultLoader.SearchForStartupAttribute(String friendlyName, IList`1 errors, Boolean& conflict) +226
   Owin.Loader.DefaultLoader.GetDefaultConfiguration(String friendlyName, IList`1 errors) +68
   Owin.Loader.DefaultLoader.LoadImplementation(String startupName, IList`1 errorDetails) +89
   Owin.Loader.DefaultLoader.Load(String startupName, IList`1 errorDetails) +30
   Microsoft.Owin.Host.SystemWeb.OwinBuilder.GetAppStartup() +165
   Microsoft.Owin.Host.SystemWeb.OwinHttpModule.InitializeBlueprint() +37
   System.Threading.LazyInitializer.EnsureInitializedCore(T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory) +139
   Microsoft.Owin.Host.SystemWeb.OwinHttpModule.Init(HttpApplication context) +160
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +584
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +168
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +277
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +369

[HttpException (0x80004005): Could not load file or assembly 'System.Collections.Immutable, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +532
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +111
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +724

目前只能通过运行Update-Package -Reinstall恢复应用,但该操作耗时约一小时,求其他替代方案。

替代方案

1. 清理输出目录并重新构建

  • 手动删除项目下的bin和obj目录,这两个目录里的残留编译文件是导致依赖冲突的常见原因。
  • 或者在Visual Studio里依次执行清理解决方案(生成 -> 清理解决方案)和生成解决方案(生成 -> 生成解决方案)。
  • 用命令行的话,直接运行:
    dotnet clean
    dotnet build
    

2. 仅处理相关依赖包

不用全量重装所有包,只针对引发问题的依赖操作:

  • 打开Package Manager Console,先卸载多余的包:
    Uninstall-Package System.Collections.Immutable -Force
    
  • 然后重新安装调用它的主包,自动拉取匹配版本的依赖:
    Install-Package Microsoft.CodeAnalysis.Workspaces -Version 4.2.0.0
    

3. 修复Web.config的绑定重定向

切换分支后,Web.config可能残留了新增分支的绑定规则,导致系统找错版本:

  • 打开项目的web.config,找到<assemblyBinding>节点下的System.Collections.Immutable配置段,比如:
    <dependentAssembly>
      <assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
    </dependentAssembly>
    
  • 如果目标分支不需要这个包,直接删掉这段配置;如果需要低版本,修改newVersion为对应版本,或者直接移除重定向规则让系统自动匹配。

4. 清理NuGet缓存后重新还原

缓存的旧包可能导致冲突,清理后重新拉取:

  • 执行命令清理缓存:
    nuget locals all -clear
    
  • 然后还原项目依赖:
    nuget restore
    
    这个操作比全量重装快很多,只下载必要的包。

5. 配置分支独立包目录

从根源避免分支间的依赖干扰,给每个分支配置单独的NuGet包目录:

  • 在项目根目录的NuGet.Config里添加:
    <config>
      <add key="globalPackagesFolder" value="..\packages\branch-[你的分支名]" />
    </config>
    
    替换[你的分支名]为实际分支名称,这样不同分支的包会存在独立目录,切换时不会互相影响。

内容的提问来源于stack exchange,提问作者tRuEsAtM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 15:33:34