Web API项目在Visual Studio正常运行,Rider中启动报错
问题:Rider运行Web API项目时加载AMLP程序集失败
我尝试用Rider运行Web API启动项目,但在浏览器打开对应URL时出现以下错误:
Could not load file or assembly 'AMLP' or one of its dependencies. An attempt was made to load a program with an incorrect format. 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.BadImageFormatException: Could not load file or assembly 'AMLP' or one of its dependencies. An attempt was made to load a program with an incorrect format. 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 'AMLP' could not be loaded. WRN: Assembly binding logging is turned OFF. To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1. Note: There is some performance penalty associated with assembly bind failure logging. To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog]. Stack Trace: [BadImageFormatException: Could not load file or assembly 'AMLP' or one of its dependencies. An attempt was made to load a program with an incorrect format.] 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.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +37 System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +159 System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection) +80 System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +22 System.Reflection.Assembly.Load(String assemblyString) +29 System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +39 [ConfigurationErrorsException: Could not load file or assembly 'AMLP' or one of its dependencies. An attempt was made to load a program with an incorrect format.] System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +777 System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +229 System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +140 System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +176 System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies() +99 System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +310 System.Web.Compilation.BuildManager.ExecutePreAppStart() +165 System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +590 [HttpException (0x80004005): Could not load file or assembly 'AMLP' or one of its dependencies. An attempt was made to load a program with an incorrect format.] System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +10087352 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +99 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +263
该项目在Visual Studio中运行正常,访问相同URL无任何问题。已尝试复制.vs文件夹中的applicationhost.config用于Rider,以及将Rider的MSBuild.exe切换为Visual Studio版本,但均未解决问题。
错误界面截图:
解决方法
这个错误是典型的程序集架构不匹配问题(32位/64位冲突),Visual Studio和Rider默认运行架构可能不一致,导致无法加载特定架构的AMLP程序集或其依赖。按以下步骤排查:
统一项目目标平台
- 在Rider中右键项目 → 属性 → 生成 → 目标平台,设置为和Visual Studio完全一致的选项(比如x64或x86,避免选Any CPU)。
- 检查所有引用的项目、第三方程序集,确保它们的目标平台与主项目对齐。
调整IIS Express运行架构
- 打开Rider的运行配置(Run → Edit Configurations),选中你的Web API配置。
- 在“配置”选项卡中找到“应用程序池”,勾选/取消“启用32位应用程序”,和Visual Studio中的设置保持一致。
清理重建解决方案
- 删除项目根目录下的bin、obj文件夹,然后在Rider中依次执行“清理解决方案”和“重新生成解决方案”,确保所有程序集都是用正确架构编译的。
对齐.NET运行环境
- 打开Rider设置(File → Settings → Build, Execution, Deployment → Toolset and Build),在.NET CLI executable选项中选择和Visual Studio相同版本的SDK路径。
启用绑定日志精准排查
- 按错误提示修改注册表:打开注册表编辑器,定位到
HKLM\Software\Microsoft\Fusion,创建或修改DWORD值EnableLog为1。 - 重新运行项目,查看
%TEMP%\fusionlog下的日志文件,可精准定位哪个依赖项存在架构不匹配问题。
- 按错误提示修改注册表:打开注册表编辑器,定位到
内容的提问来源于stack exchange,提问作者Nayeer Mahiuddin
相关产品推荐
相关产品推荐

