.NET 4.8 ASP.NET网站已存在System.Collections.Immutable程序集仍加载失败求助
Hi,最近碰到一个特别头疼的问题,想请各位帮忙支支招:我有一个ASP.NET网站(不是Web应用程序),目标框架是.NET 4.8。前阵子用NuGet更新了一波包之后,在VS2022里点「开始执行(不调试)」时,Chrome加载起始页直接弹出运行时错误,可编译环节完全没报任何错。错误提示是:
Could not load file or assembly 'System.Collections.Immutable, Version=1.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
关键是System.Collections.Immutable 1.2.1.0版本的文件明明就躺在网站的bin目录里啊!我试过把packages.config回滚到之前正常运行的版本,重启VS,甚至删掉了C:\Users\username\AppData\Local\Temp\Temporary ASP.NET Files里的所有临时文件,结果还是报同样的错。而且没更新包之前根本就没有这个文件,程序跑的好好的,感觉就是NuGet更新搞出来的幺蛾子。搜了一圈没找到解决办法,特别疑惑——为啥文件明明在那儿,还是加载失败?
附上我的packages.config内容:
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="Antlr" version="3.5.0.2" targetFramework="net45" /> <package id="EntityFramework" version="6.5.1" targetFramework="net48" /> <package id="jQuery" version="3.5.1" targetFramework="net45" /> <package id="jQuery.Validation" version="1.19.1" targetFramework="net45" /> <package id="Microsoft.AspNet.Identity.Core" version="2.2.4" targetFramework="net45" /> <package id="Microsoft.AspNet.Identity.EntityFramework" version="2.2.4" targetFramework="net45" /> <package id="Microsoft.AspNet.Identity.Owin" version="2.2.4" targetFramework="net45" /> <package id="Microsoft.AspNet.Mvc" version="5.3.0" targetFramework="net48" /> <package id="Microsoft.AspNet.Razor" version="3.3.0" targetFramework="net48" /> <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net45" /> <package id="Microsoft.AspNet.WebApi" version="5.3.0" targetFramework="net48" /> <package id="Microsoft.AspNet.WebApi.Client" version="6.0.0" targetFramework="net48" /> <package id="Microsoft.AspNet.WebApi.Core" version="5.3.0" targetFramework="net48" /> <package id="Microsoft.AspNet.WebApi.WebHost" version="5.3.0" targetFramework="net48" /> <package id="Microsoft.AspNet.WebPages" version="3.3.0" targetFramework="net48" /> <package id="Microsoft.Bcl" version="1.1.10" targetFramework="net45" /> <package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net45" /> <package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="net45" /> <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="2.0.1" targetFramework="net48" /> <package id="Microsoft.jQuery.Unobtrusive.Validation" version="4.0.0" targetFramework="net48" /> <package id="Microsoft.Net.Http" version="2.2.29" targetFramework="net45" /> <package id="Microsoft.Owin" version="4.2.2" targetFramework="net45" /> <package id="Microsoft.Owin.Host.SystemWeb" version="4.2.2" targetFramework="net48" /> <package id="Microsoft.Owin.Security" version="4.2.2" targetFramework="net45" /> <package id="Microsoft.Owin.Security.Cookies" version="4.2.2" targetFramework="net45" /> <package id="Microsoft.Owin.Security.Facebook" version="4.2.2" targetFramework="net45" /> <package id="Microsoft.Owin.Security.Google" version="4.2.2" targetFramework="net45" /> <package id="Microsoft.Owin.Security.MicrosoftAccount" version="4.2.2" targetFramework="net45" /> <package id="Microsoft.Owin.Security.OAuth" version="4.2.2" targetFramework="net45" /> <package id="Microsoft.Owin.Security.Twitter" version="4.2.2" targetFramework="net45" /> <package id="Microsoft.Web.Infrastructure" version="2.0" targetFramework="net45" /> </packages>
我自己琢磨了几个可能的排查方向,但还没试出结果,也想问问大家有没有遇到过类似情况:
- 会不会是程序集绑定重定向的问题?可能某个依赖包要求的版本和bin里的不一致,虽然文件在,但CLR找的是别的版本?有没有试过在web.config里加个绑定重定向配置?比如:
<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-1.2.1.0" newVersion="1.2.1.0" /> </dependentAssembly> </assemblyBinding> </runtime> - 有没有可能是NuGet缓存搞的鬼?我回滚了packages.config,但本地NuGet缓存里可能还有旧的/冲突的包?可以试试清空NuGet缓存:在VS里点「工具→选项→NuGet包管理器→常规」,然后点「清空所有NuGet缓存」,之后重新还原包。
- ASP.NET网站是动态编译的,会不会是开发服务器的虚拟目录配置有问题?比如VS里的IIS Express指向的bin目录不是最新的?可以右键网站→「属性页→起始选项」,检查下本地服务器的配置,或者切换到内置开发服务器试试?
- 还有,会不会是GAC里的同名程序集干扰?.NET 4.8有些程序集会默认装在GAC里,如果GAC里的版本和bin里的不一致,CLR可能优先加载GAC里的,导致版本不匹配?可以去
C:\Windows\Microsoft.NET\assembly里找找有没有System.Collections.Immutable的其他版本。
内容来源于stack exchange

