问题
When I run I get: Could not load file or assembly 'CefSharp.Wpf, Version=41.0.0.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138' or one of its dependencies. An attempt was made to load a program with an incorrect format.
This means the problem is due to the dlls not being correct bitness.
In version 41.0.0, the nuget package adds dll references to the x86 version which point to the correct path (an x86 folder). There are also another copy of dlls that point to an empty path in the properties panel. The problem is that the x64 version crashes with a bad image error because it is referencing the x86 versions of the dlls.
I have back tracked to 39.0.2 and CefSharp works for me. I have both sets of dlls and where x86 and x64 versions of my application compile and run as expected.
When I upgrade again to 41.0.0 same problem as before. The x64 version will compile though the application uses x86 versions of the dll which crashes when trying to display the first browser. I tried to rebuild all as well as removing the dlls that are copied in automatically and during the compilation the x86 versions are copied in again. (At least I believe this is the case a
I then removed all of the dlls in the references and manually added the x64 versions. The compilation and execution worked correctly as it did with the 39.02 version of CefSharp.WPF.
How can I get both versions to work without having to manually change the dll paths?
Another note: I can use nuget while set to x64 and this makes the opposite of the above problem. Now x64 works and x86 doesn't. The paths of the dlls are now x64 folders.
回答1:
It turns out, after much tribulation and anguish, I found that the References were not setup correctly in my .csproj
. They were defined as being x64 somehow.
<Reference Include="CefSharp, Version=41.0.0.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<!--<HintPath>..\packages\CefSharp.Common.41.0.0\CefSharp\x64\CefSharp.dll</HintPath>-->
</Reference>
<Reference Include="CefSharp.Core, Version=41.0.0.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<!--<HintPath>..\packages\CefSharp.Common.41.0.0\CefSharp\x64\CefSharp.Core.dll</HintPath>-->
</Reference>
<Reference Include="CefSharp.Wpf, Version=41.0.0.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138, processorArchitecture=x86">
<SpecificVersion>False</SpecificVersion>
<!--<HintPath>..\packages\CefSharp.Wpf.41.0.0\CefSharp\x64\CefSharp.Wpf.dll</HintPath>-->
</Reference>
As soon as I commented out the HintPath
, it started working correctly. Note that the actual references are defined by the Import
at the top of the project.
<Import Project="..\packages\CefSharp.Wpf.41.0.0\build\CefSharp.Wpf.props" Condition="Exists('..\packages\CefSharp.Wpf.41.0.0\build\CefSharp.Wpf.props')" />
<Import Project="..\packages\CefSharp.Common.41.0.0\build\CefSharp.Common.props" Condition="Exists('..\packages\CefSharp.Common.41.0.0\build\CefSharp.Common.props')" />
These .props
files setup the actual HintPath
that is necessary for it to copy the correct .dlls
over to the build folder. I am uncertain as to how my project ended up with the path set incorrectly.
来源:https://stackoverflow.com/questions/31172045/could-not-load-file-or-assembly-cefsharp-wpf-for-both-x64-and-x86-only-one-wor