请求协助解决Microsoft.Web.Infrastructure程序集加载不匹配错误
错误详情
Server Error in '/' Application. 无法加载文件或程序集“Microsoft.Web.Infrastructure”或其依赖项之一。找到的程序集清单定义与程序集引用不匹配。(异常来自 HRESULT:0x80131040)
异常详细信息: System.IO.FileLoadException: 无法加载文件或程序集“Microsoft.Web.Infrastructure”或其依赖项之一。找到的程序集清单定义与程序集引用不匹配。(异常来自 HRESULT:0x80131040)
程序集加载跟踪: WRN: 程序集绑定日志已关闭。若要启用程序集绑定失败日志记录,请将注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) 设置为 1。注意: 与程序集绑定失败日志记录关联的性能损失。若要关闭此功能,请移除注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog]。
堆栈跟踪:
[FileLoadException: 无法加载文件或程序集“Microsoft.Web.Infrastructure”或其依赖项之一。找到的程序集清单定义与程序集引用不匹配。(异常来自 HRESULT:0x80131040)] [FileLoadException: 无法加载文件或程序集“Microsoft.Web.Infrastructure, Version=2.2.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或其依赖项之一。找到的程序集清单定义与程序集引用不匹配。(异常来自 HRESULT:0x80131040)] System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0 System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +232 System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection) +113 System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +23 System.Reflection.Assembly.Load(String assemblyString) +35 System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +49 [ConfigurationErrorsException: 无法加载文件或程序集“Microsoft.Web.Infrastructure, Version=2.2.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或其依赖项之一。找到的程序集清单定义与程序集引用不匹配。(异常来自 HRESULT:0x80131040)] System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +762 System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +259 System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +167 System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +238 System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies() +78 System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +334 System.Web.Compilation.BuildManager.ExecutePreAppStart() +178 System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +746 [HttpException (0x80004005): 无法加载文件或程序集“Microsoft.Web.Infrastructure, Version=2.2.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或其依赖项之一。找到的程序集清单定义与程序集引用不匹配。(异常来自 HRESULT:0x80131040)] System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +552 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +122 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +732
解决方案
1. 重新安装对应版本的Microsoft.Web.Infrastructure
使用NuGet包管理器安装指定版本的程序集:
- Package Manager Console命令:
Install-Package Microsoft.Web.Infrastructure -Version 2.2.5 - .NET CLI命令:
dotnet add package Microsoft.Web.Infrastructure --version 2.2.5
安装完成后确认项目引用的版本与错误信息中的2.2.5.0一致。
2. 添加程序集绑定重定向
在项目的web.config文件中,于<configuration>节点内添加如下配置,强制将所有旧版本的请求重定向到当前安装的2.2.5.0版本:
<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Microsoft.Web.Infrastructure" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-2.2.5.0" newVersion="2.2.5.0" /> </dependentAssembly> </assemblyBinding> </runtime>
3. 清理并重新生成项目
- 删除项目根目录下的
bin和obj文件夹 - 在Visual Studio中右键解决方案,选择清理解决方案
- 随后右键解决方案,选择重新生成解决方案
- 发布或部署更新后的项目文件到服务器
4. 检查服务器GAC中的程序集冲突
如果服务器全局程序集缓存(GAC)中存在其他版本的Microsoft.Web.Infrastructure,可能会优先加载GAC中的版本导致冲突:
- 以管理员身份打开命令提示符,运行以下命令查看GAC中的相关程序集:
gacutil /l Microsoft.Web.Infrastructure - 若存在与项目版本不符的程序集,使用以下命令卸载(替换
X.X.X.X为对应版本号):gacutil /u Microsoft.Web.Infrastructure, Version=X.X.X.X, PublicKeyToken=31bf3856ad364e35
5. 启用程序集绑定日志排查深层原因
按照错误提示启用绑定日志,获取更详细的加载失败信息:
- 打开注册表编辑器,定位到
HKLM\Software\Microsoft\Fusion - 创建或修改
EnableLog(DWORD类型)值为1 - 重启IIS并触发错误
- 查看日志文件(默认路径:
%windir%\assembly\logs\fusionlog.dll),确认程序集加载时尝试读取的路径和版本,定位冲突来源
内容的提问来源于stack exchange,提问作者Eiffel Felicia

