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:
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?
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With