Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a .NET Core app with '<PlatformTarget>x86</PlatformTarget>'

I set up a basic C# console application in Visual Studio Code like so:

dotnet new console
dotnet restore

I then build like so:

dotnet build

Then run:

dotnet run

This all works as expected. If I change the platform target to x86 though, I get this error when I build and run the application again:

Unhandled Exception: System.BadImageFormatException: Could not load file or assembly 'C:\temp\code\testx86_2\bin\Debug\netcoreapp2.0\testx86_2.dll'. An attempt was made to load a program with an incorrect format.

My files:

Program.cs:

using System;

namespace testx86_2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}

testx86_2.csproj before setting the platform target:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

</Project>

testx86_2.csproj after setting the platform target:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>

</Project>

Versions:

  • Visual Studio Code: 1.13.1
  • Microsoft (R) Build Engine version 15.3.409.57025 for .NET Core
like image 556
rhughes Avatar asked Nov 08 '22 16:11

rhughes


1 Answers

Adding --runtime win-x86 to the run command should force the runtime to launch in 32-bit mode, even if using the the x64 version of dotnet cli.

The <PlatformTarget>x86</PlatformTarget> isn't needed, adding the switch to the run command is enough to force x86 mode. I would remove the PlatformTarget, to allow you to change between x86 / x64 more easily.

like image 59
Robert Avatar answered Nov 15 '22 11:11

Robert