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

15

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?

c#
asp.net
web-site-project
.net-standard
asked on Stack Overflow Aug 30, 2017 by Ian Mercer

2 Answers

1

You are trying to load an unmanaged dynamic link library or executable (such as a Windows system DLL) as for .NET Framework assembly. The following example illustrates this by using the Assembly.LoadFile method to load Kernel32.dll.

Copy
// Windows DLL (non-.NET assembly)
string filePath = Environment.ExpandEnvironmentVariables("%windir%");
if (! filePath.Trim().EndsWith(@"\"))
   filePath += @"\";
filePath += @"System32\Kernel32.dll";

try {
   Assembly assem = Assembly.LoadFile(filePath);
}
catch (BadImageFormatException e) {
   Console.WriteLine("Unable to load {0}.", filePath);
   Console.WriteLine(e.Message.Substring(0,
                     e.Message.IndexOf(".") + 1));
}
// The example displays an error message like the following:
//       Unable to load C:\WINDOWS\System32\Kernel32.dll.
//       The module was expected to contain an assembly manifest.
answered on Stack Overflow Jul 24, 2020 by Nadeem Taj
-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

answered on Stack Overflow Jan 10, 2018 by Elizaveta Golenok • edited Jan 10, 2018 by garfbradaz

User contributions licensed under CC BY-SA 3.0