Unable to start kestrel – An attempt was made to load a program with an incorrect format

Hopefully this saves someone some time in the future.

I was developing a throw away ASP.NET Core app on .NET Full framework in Visual Studio 2017. I noticed that the Platform target was set to x86 (the default), so I changed it to AnyCPU then redeployed.

I tried to run it from the console (I love how I can just run a webapp from the console now… so awesome), but got the following error:

crit: Microsoft.AspNetCore.Server.Kestrel[0]
      Unable to start Kestrel.
System.AggregateException: One or more errors occurred. ---> System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

Turns out that even though I had changed the Platform target via the Project Properties, there was a setting within the csproj that indicated an x86 runtime.

To solve the issue, open up the csproj file for your project and change the x86 part of the value of the RuntimeIdentifier element to x64.

So instead of (for example):

<RuntimeIdentifier>win7-x86</RuntimeIdentifier>

You want:

<RuntimeIdentifier>win7-x64</RuntimeIdentifier>

Save that and rebuild or redeploy your project.

For more information about the above, I first found the error message described at:

https://docs.microsoft.com/en-us/aspnet/core/publishing/iis#platform-conflicts-with-rid.

Information about the RuntimeIdentifer property, see https://docs.microsoft.com/en-us/dotnet/articles/core/tools/csproj#runtimeidentifier. For information about the RID values, see https://docs.microsoft.com/en-us/dotnet/articles/core/rid-catalog.