I'm using the old VS 2015 Pro (C# project) and it seems like there is a bug or I don't understand something. As I discovered by experimenting, adding copies of a dll file (that is already referenced by project) into Resources (bin\Debug\Resources) as Content (or None) makes Visual Studio overwrite (replace) the referenced dll (in bin\Debug folder), causing BadImageFormatException at startup. By including in Resources I mean adding instances of this:

<Content Include="Resources\NuGetPackageName\public\x64\DllName.dll">
  <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>

Removing these fixes the problem. Changing Content to None or Always to Never (or both) does not fix the problem. It does not make any sense to me. Why is it copying the file to bin\Debug as well??

I'm referencing the DLL from the packages folder (it's coming from NuGet package). Technically, I'm pulling 2 NuGet packages, they both contain x64 and x86 version of the same dll as content and (for each package) one of them (based on selected x86/x64 in VS) is getting copied by prebuild script into a special location inside each package (in packages\PackageName\SpecialLocation folder) and one of the two is used as reference in the project - don't ask why! I know it's crazy!). It was working fine until these additional copies in resources appeared - actually after adding another project to solution, because it was still working fine before it was added. The new project does not copy or reference this dll so it does not affect this). I've checked that the copy in packages\PackageName\SpecialLocation is not getting overwritten incorrectly, so it's only the one in bin\Debug that is getting swapped.

Can you think of any reason for this happening?

0

There are 0 best solutions below