简体   繁体   English

无法从网站项目加载要执行的参考程序集

[英]Cannot load a reference assembly for execution from a Web-Site project

Using .NET 4.6.2 and an older Web-Site (not Web-Application) project.使用 .NET 4.6.2 和较旧的网站(不是 Web 应用程序)项目。 If I clear the BIN directory and then build and run it works, but sometimes after multiple builds and runs, it fails with this error.如果我清除 BIN 目录然后构建并运行它,但有时在多次构建和运行后,它会失败并出现此错误。

Server Error in '/' Application.
Cannot load a reference assembly for execution.
....
[BadImageFormatException: Cannot load a reference assembly for execution.]
[BadImageFormatException: Could not load file or assembly 'netfx.force.conflicts' or 
one of its dependencies. Reference assemblies should not be loaded for execution.  
They can only be loaded in the Reflection-only loader context.
(Exception from HRESULT: 0x80131058)]
....

Is there any trick to getting Web-Site projects to work correctly when the libraries they use are beginning to pull in netstandard 2.0?当他们使用的库开始引入 netstandard 2.0 时,有什么技巧可以让网站项目正常工作?

Also, it seems that this assembly binding redirect is necessary to get it to run but nuget adds a redirect to an older version 4.1.1.0.此外,似乎需要此程序集绑定重定向才能使其运行,但 nuget 添加了对旧版本 4.1.1.0 的重定向。 Any suggestions on how to fix that other than manually editing this line after most nuget updates?除了在大多数 nuget 更新后手动编辑此行之外,有关如何解决此问题的任何建议?

  <dependentAssembly>
    <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a"
                      culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
  </dependentAssembly>

Would all these issues go away if the project was migrated to a Web-Application project?如果将项目迁移到 Web 应用程序项目,所有这些问题都会消失吗?

Most likely you are encountering this error because of a mismatch between x86 and x64 compiled assemblies or similarly a mismatch between .NET Framework and .NET Core.您遇到此错误的原因很可能是 x86 和 x64 编译的程序集之间不匹配,或者类似地,.NET Framework 和 .NET Core 之间不匹配。

Here are some troubleshooting options for you that you may not have tried.以下是一些您可能尚未尝试过的故障排除选项。

Option #1选项1

In the AppDomain there is an event handler for when the program is trying to resolve an assembly that is reference.AppDomain中有一个事件处理程序,用于当程序尝试解析引用的程序集时。 If you subscribe to it, you should be able to get more information about what is missing.如果您订阅它,您应该能够获得有关缺失内容的更多信息。 This is how the implementation would look:这是实现的样子:

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

Event Handler:事件处理程序:

private System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    System.Diagnostics.Trace.TraceInformation($"Trying to resolve assebly: {args.Name} requested by {args.RequestingAssembly.FullName}");
    // This event handler allows you to help the find the assembly for the CLR.
    // you can dynamically load the assembly and provide it here. 
    return null;
}

Option #2选项#2

There is also a tools in the .NET SDK for troubleshooting binding issues. .NET SDK 中还有一个用于解决绑定问题的工具。

Here is more information about the Assembly Binding Log Viewer这是有关程序集绑定日志查看器的更多信息

You need to enable it before it will emit any interesting information, but this should get you to the root of your problem, if the AssemblyResolve event doesn't help.您需要在它发出任何有趣的信息之前启用它,但是如果AssemblyResolve事件没有帮助,这应该可以让您找到问题的根源。

Yes, you can to convert web-site to web application:是的,您可以将网站转换为 Web 应用程序:

屏幕抓取

If you can't to see this configuration - probably you have problem with names of classes, for example:如果您看不到此配置 - 可能您对类的名称有疑问,例如:

/Directory/Index.aspx.cs /目录/Index.aspx.cs

/Directory2/Index.aspx.cs /Directory2/Index.aspx.cs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 出现奇怪的 BadImageFormatException:无法加载要执行的参考程序集 - Getting strange BadImageFormatException: Cannot load a reference assembly for execution System.Net.Http - 无法加载文件或程序集,然后无法加载要执行的引用程序集 - System.Net.Http - Could not load file or assembly, then Cannot load a reference assembly for execution Telerik网格设置可用于网站项目,但不能与网络应用程序一起使用 - Telerik Grid Setting working with a web-site project but not with a web-application 预加载Azure网站以增加首次访问加载时间 - Pre-loading Azure web-site to increase first visit load time 如何使用请求后的Windows窗体打开网站? - How to open web-site from windows-forms using post-request? 从 ASP.Net Core Blazor 网站与 Outlook 交互 - Interacting with Outlook from an ASP.Net Core Blazor Web-site 无法加载文件或程序集以供项目参考 - Could not load file or assembly for project reference 将项目添加为参考导致无法加载文件或程序集或其依赖项之一 - Adding Project as Reference results in Cannot load file or assembly or one of its dependencies 如何从执行程序集引用库? - How to reference a library from execution assembly? 在Web项目中添加PresentationCore程序集引用 - Add PresentationCore assembly reference in web project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM