Cannot load a reference assembly for execution from a Web-Site project
Asked Answered
W

2

18

Using .NET 4.6.2 and an older Web-Site (not Web-Application) project. 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.

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?

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. Any suggestions on how to fix that other than manually editing this line after most nuget updates?

  <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?

Would answered 30/8, 2017 at 20:9 Comment(7)
you probably already know this but I'll just say, I've seen this when trying to use a 32 bit dll on an x64 machine. I don't think this goes away when converting from a web site to a web application project type.Prefab
Ian, have you had any luck with this yet? I am have the same issue using the same setup that you have: .NET Standard Library within 4.6.2 ASP.NET Web Api.Pantywaist
@AdamScharp No luck yet, still deleting bin directory manually every time it happens. The RunTime version issue appears to be related to MongoDB in this case.Would
Do you need to shut down Visual Studio in order to / before deleting the bin dir?Developer
The short answer is no. Not with VS2017 Community anyway.Developer
@IanMercer I resolved the problem by deleting the bin folder in my web project folder. I have VS2017 + .NET standard Library 2.02 + .NET 4.6.1Nibelung
@IanMercer Did you try the assembly binding tool? Another thing to check: If this app was upgraded from an earlier .NET version is there a stale framework reference in the web.config file ?Voncile
V
1

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.

Here are some troubleshooting options for you that you may not have tried.

Option #1

In the AppDomain there is an event handler for when the program is trying to resolve an assembly that is reference. 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

There is also a tools in the .NET SDK for troubleshooting binding issues.

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.

Voncile answered 14/6, 2022 at 13:18 Comment(0)
B
-1

Yes, you can to convert web-site to web application:

screen grab

If you can't to see this configuration - probably you have problem with names of classes, for example:

/Directory/Index.aspx.cs

/Directory2/Index.aspx.cs

Beanpole answered 10/1, 2018 at 9:36 Comment(1)
I know you can, but that's not the question.Would

© 2022 - 2024 — McMap. All rights reserved.