Here’s a quick fix for an error I came across when I’ve been experimenting with various packaging options for WPF (Win32) applications.
To set the scene I have a new application, PackagingWPF, that I created based on the WPF Application project template. At this stage I’m not even going to both modifying the application.
Next I’m going to add another project, this time based on the Windows Application Packaging Project template. Then, right-click on the Applications node and select Add Reference.
Check the box alongside the WPF application, PackagingWPF and click OK.
After adding the WPF application, we then need to set the packaging project to be the Startup Project. Do this by right-clicking the project in Solution Explorer and clicking Set as Startup Project.
At this point make sure you can build and run the WPF application – yes, running the packaging project should launch your WPF application in the context it would be once deployed via the package.
We’ll go ahead now and attempt to publish the application – right-click on the packaging project and select Publish, followed by Create App Packages. We’ll select the Sideloading option (disabling automatic updates) and select Yes to signing the application (we’ll need to select or create a certificate for signing – don’t forget to Trust the certificate to make sure you can install the generated package). On the final screen of the publishing wizard I’m going to leave the defaults and click Create.
After a few seconds you’ll see an error similar to the following:
There was a mismatch between the processor architecture of the project being built "MSIL" and the processor architecture of the reference "c:\temp\PackagingWPF\PackagingWPF\bin\x86\Debug\net5.0-windows\win-x86\PackagingWPF.dll", "x86"
As I haven’t modified the WPF application it’s currently set to target Any CPU which clearly doesn’t align with the packaging project, which in this case seems to want an x86 version of the dll. To fix this we can add x64 and x86 platforms to the project via the csproj.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<Platforms>x64;x86</Platforms>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>
Note that I’ve actually removed the AnyCPU option (simply by not including it in the Platforms list) since I’ll never be using that option.
When we step through the publishing wizard, this time I’m going to change the last screen. Since we know that the Any CPU option won’t exist any more, we need to pick one or more of the configurations that does exist – here I’m picking the x64 Architecture, which correlates to the Debug (x64) solution configuration.
After clicking Create a similar error is generated
There was a mismatch between the processor architecture of the project being built "AMD64" and the processor architecture of the reference "c:\temp\PackagingWPF\PackagingWPF\bin\x86\Debug\net5.0-windows\win-x86\PackagingWPF.dll", "x86"
At this point you might be scratching your head but it’s worth checking that the WPF application is set to build for the correct platform for the chosen solution configuration. Right-click on the solution node in Solution Explorer and select Configuration Manager. Make sure the Active solution configuration is set to Debug and that the Active solution platform is x64. Then, make sure that the Platform dropdown for both projects is set to x64. The following image shows what my Configuration Manager was set to before I changed the Platform to x64.
Whilst you’re in Configuration Manager, you might also want to double-check the x86 solution platform. I also typically clean up both solution and project configurations to remove those that aren’t going to be used, in this case leaving on x86 and x64.
At this point the publish process should appear to work and successfully generate a msixbundle that you can install.
Warning – Visual Studio 16.9.0 Preview 2.0 is broken: In this version of Visual Studio when you do a clean (delete bin and obj folders) and then run the publish process, you’ll get an error similar to:
Unknown build error, 'Could not find file 'c:\temp\PackagingWPF\PackagingWPF\obj\x64\Debug\net5.0-windows10.0.17763.0\win-x64\PackagingWPF.dll'. Line 11 Position 17.' PackagingWPFPackaged c:\temp\PackagingWPF\PackagingWPF\MainWindow.xaml 12
The solution is to use the stable release of Visual Studio (16.8.3 is what I have at the moment) which seems to generate the package correctly.