Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AspNetCompiler - An attempt was made to load program with an incorrect format

I am building a 64bit only web application (AssemblyA) and referencing another application I have built which is also 64bit only (AssemblyB).

When I add AssemblyB as a reference in AssemblyA, I get the following compilation error in Visual Studio:

ASPNETCOMPILER Could not load file or assembly 'AssemblyB' or one of its dependencies. An attempt was made to load a program with an incorrect format.

Both of my application's platform target setting is set to x64, both Target Framework settings is .Net Framework 4.6 and Prefer 32bit is unchecked.

I have tried referencing every dependent reference of AssemblyB in AssemblyA, making sure the versions of all dependent references are the same.

I have used this question: How to determine if a .NET assembly was built for x86 or x64? to confirm all referenced assemblies are either MSIL or AMD64.

I have used the aspnet_compiler.exe command line tool with the errorstack option enabled and got the following stack trace:

[BadImageFormatException]: Could not load file or assembly 'AssemblyB' or one of its dependencies. An attempt was made to load a program with an incorrect format.

at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntro spection, Boolean suppressSecurityChecks)

at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String code Base, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& s tackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntros pection, Boolean suppressSecurityChecks)

at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark &stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)

at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark &stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection)

at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark &stackMark, Boolean forIntrospection)

at System.Reflection.Assembly.Load(String assemblyString)

at System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective)

[ConfigurationErrorsException]: Could not load file or assembly 'AssemblyB' or one of its dependencies. An attempt was made to load a program with an incorrect format.

at System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective)

at System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomain BinDirectory()

at System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai)

at System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig)

at System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies()

at System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStar tInitListPath, Boolean& isRefAssemblyLoaded)

at System.Web.Compilation.BuildManager.ExecutePreAppStart()

at System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, Host ingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException)

I have looked at the following related questions and none answer this question. Most relate to IIS configuration, which is not the case as this is a compilation error, or have the solution as setting the project to allow 32bit platform target, which is not suitable for my case:

  • "An attempt was made to load a program with an incorrect format" even when the platforms are the same (no good as uses IIS config)
  • Could not load file or assembly ... An attempt was made to load a program with an incorrect format (System.BadImageFormatException) (no good as is a runtime error)
  • System.BadImageFormatException An attempt was made to load a program with an incorrect format (no good as uses 32bit solution)
  • Could not load file or assembly 'xxx' or one of its dependencies. An attempt was made to load a program with an incorrect format (no good as uses 32bit solution)

I am at a loss as to where to go from here. To reiterate, I do not want to set either of my projects to 32bit and this isn't a problem with IIS config as this is a compilation error.

I have also tried it on several different machines and on brand new applications with the same result.

How do I fix this compilation error?

like image 235
Steve Avatar asked Feb 08 '16 02:02

Steve


1 Answers

The directive <AspNetCompiler /> takes an argument ToolPath that can be directed to the 64 bit version of aspnet_compiler.exe. This is described in this blogpost. The gist of it, edit your csproj file:

Add the ToolPath attribute to the AspNetCompiler node:

<AspNetCompiler VirtualPath="temp" PhysicalPath="$(ProjectDir)" ToolPath="$(AspNetToolPath)" />

And where MvcBuildViews is defined, add the properties FrameworkVersion and AspNetToolPath, like this:

<MvcBuildViews>true</MvcBuildViews>
<FrameworkVersion>v4.0.30319</FrameworkVersion>
<AspNetToolPath Condition=" '$(Platform)' == 'x64'">$(windir)\Microsoft.NET\Framework64\$(FrameworkVersion)</AspNetToolPath>
like image 172
Bouke Avatar answered Oct 20 '22 09:10

Bouke