.NET 4.8下ASP.NET Web API程序集加载失败问题求助
解决ASP.NET Web API部署QA服务器时Microsoft.Web.Infrastructure程序集加载错误
以下是针对该问题的排查和解决步骤:
1. 修正绑定重定向的版本匹配问题
你bin目录中的Microsoft.Web.Infrastructure.dll版本为2.0.34876.0,但web.config里的绑定重定向newVersion设置为2.0.0.0,版本号不匹配会导致重定向规则失效。修改web.config中的重定向配置,让newVersion与实际dll版本一致:
<dependentAssembly> <assemblyIdentity name="Microsoft.Web.Infrastructure" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-2.0.34876.0" newVersion="2.0.34876.0" /> </dependentAssembly>
2. 检查QA服务器GAC中的旧版本程序集
ASP.NET会优先加载全局程序集缓存(GAC)中的程序集,如果QA服务器的GAC里存在Microsoft.Web.Infrastructure的v1版本,会覆盖bin目录中的版本:
- 以管理员身份打开QA服务器的命令提示符,执行命令查看GAC中的相关程序集:
gacutil /l Microsoft.Web.Infrastructure - 如果输出中存在版本1.0.0.0的记录,执行以下命令移除:
gacutil /u Microsoft.Web.Infrastructure,Version=1.0.0.0,PublicKeyToken=31bf3856ad364e35
3. 确认web.config重定向配置的位置正确性
绑定重定向配置必须放在<configuration>→<runtime>→<assemblyBinding>节点下,位置错误会导致配置不生效。检查你的web.config结构是否符合要求:
<configuration> <!-- 其他配置节点 --> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <!-- Microsoft.Web.Infrastructure重定向配置 --> <dependentAssembly> <assemblyIdentity name="Microsoft.Web.Infrastructure" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-2.0.34876.0" newVersion="2.0.34876.0" /> </dependentAssembly> <!-- 其他程序集重定向配置 --> </assemblyBinding> </runtime> </configuration>
4. 启用程序集绑定日志定位问题根源
使用Windows自带的**程序集绑定日志查看器(Fuslogvw.exe)**追踪程序集加载的详细过程,找到请求v1版本程序集的来源:
- 以管理员身份打开Fuslogvw.exe(可通过开始菜单搜索或直接运行)。
- 点击「设置」,选择「绑定失败时记录日志」或「所有绑定记录日志」,指定日志存储路径。
- 重现API调用错误,回到Fuslogvw查看生成的日志,日志会显示请求程序集的来源、尝试加载的版本以及失败原因。
5. 检查IIS应用程序池配置
- 确认QA服务器上的应用程序池CLR版本设置为
.NET CLR版本 v4.0.30319(对应.NET 4.8),运行模式为集成模式。 - 检查应用程序池的身份权限,确保其拥有读取网站bin目录的权限,权限不足可能导致程序集加载异常。
6. 显式指定程序集代码库
如果上述步骤无效,可在web.config的重定向配置中添加<codeBase>节点,强制指定加载bin目录中的程序集:
<dependentAssembly> <assemblyIdentity name="Microsoft.Web.Infrastructure" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-2.0.34876.0" newVersion="2.0.34876.0" /> <codeBase version="2.0.34876.0" href="file:///C:/YourWebAppPath/bin/Microsoft.Web.Infrastructure.dll" /> </dependentAssembly>
替换C:/YourWebAppPath为QA服务器上网站的实际物理路径。
内容的提问来源于stack exchange,提问作者Rick Hodder
相关产品推荐
相关产品推荐

