启用OwinStartup致ASP.NET WebForm应用抛出FileLoadException求助
基于.NET Framework 4.8的ASP.NET WebForm应用,搭建了独立的SignalR Hub Web应用,控制台客户端可正常连接,但将web.config中owin:AutomaticAppStartup设为true时触发程序集加载错误,设为false则应用正常启动。当前项目中System.Runtime.dll版本为4.8.9,疑似有组件引用了System.Runtime 7.0.0.0但无法定位来源。
相关代码
Startup.cs
[assembly: OwinStartup(typeof(webapp.Startup))] namespace webapp { public class Startup { public void Configuration(IAppBuilder app) { app.MapSignalR(); } } }
web.config关键配置
<add key="owin:AutomaticAppStartup" value="true" />
错误信息
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.[FileLoadException: Could not load file or assembly 'System.Runtime, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)]
[HttpException (0x80004005): Could not load file or assembly 'System.Runtime, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)]
1. 校验NuGet包兼容性
确保安装的是适配.NET Framework的SignalR和Owin包:
- 卸载所有针对.NET Core/.NET 5+的包(如
Microsoft.AspNetCore.SignalR、Microsoft.AspNetCore.Owin) - 安装适配.NET Framework的官方包:
Microsoft.AspNet.SignalR(核心SignalR包)Microsoft.Owin、Microsoft.Owin.Host.SystemWeb(Owin宿主依赖)
2. 添加程序集绑定重定向
在web.config的<runtime>节点下添加绑定重定向,将高版本的System.Runtime映射到当前项目的4.8.9版本:
<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="4.8.9.0" /> </dependentAssembly> </assemblyBinding> </runtime>
3. 清理并重建项目
- 删除项目根目录下的
bin和obj文件夹 - 清理NuGet缓存:在Visual Studio中依次点击「工具」→「NuGet包管理器」→「包管理器设置」→「清除所有NuGet缓存」
- 重新还原NuGet包并编译项目
内容的提问来源于stack exchange,提问作者waynemodz

