sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Asp.Net project with Build platform x86

Open sadjadbp opened this issue 10 years ago • 21 comments

Here is a original thread at MVC repo, been told to open it here https://github.com/aspnet/Mvc/issues/4663

I have a asp.net project that reference a native dll via Nuget package. This native DLL is only build for x86. I've changed my project.json to set buildOptions "platform": "x86"

But when I build this asp.net project via visual studio, it creates "bin\Debug\net46\win7-x64" as oppose to "win7-x86". If I run the resulting ".exe" file it would crash.

I've tried to use "dotnet build -r win7-x86" and it would create the right folder "bin\Debug\net46\win7-x86" and then I can run the ".exe" file.

Still cannot test the application in x86 mode.

Then I've uninstalled Microsoft .NET Code 1.0.0 RC2 - SDK Preview 1 (x64) which was installed via "VS 2015 Tooling Preview" and installed the "x86" one, and everything seems to be working.

I think it is not right. I should be have the option to install x64 and x86 and then via that buildOption I should be able to tell dotnet how to compile. Just like MSBuild and ASP.Net projects where I set the build options from Project Properties.

sadjadbp avatar May 17 '16 21:05 sadjadbp

@sadjadbp Not sure if it helps, but you could probably publish it: dotnet publish -c Debug -r win7-x86 ... that should work ... if you don't mind publishing over building.

guardrex avatar May 21 '16 20:05 guardrex

Having the exact same issue. I have a ASP.NET project that needs to be build for x86 (I know..this shouldn't be the case anymore...but my website is relying on an x86 C++ project). Whatever I tried it always build for x64. Thanks to the tip uninstalling the x64 version and installing the x86 version fixed it but now I'm stuck to x86 it seems.

@GuardRex I tried publishing but for me that also crashed with a BadImageFormatException on launch like build -r x86 did. It maybe packs the x64 librarys while building the project itself for x86.

WolfspiritM avatar May 23 '16 10:05 WolfspiritM

@WolfspiritM good that it helped. But yes, now I think you are stuck with x86. Interesting enough, installing the x64 version of Windows Server Tools on our servers, I can copy the published folder there and then Web Application works fine!

Either way, it seems to be VS Tooling bug or something. I'll wait for MS Person to answer to this thread.

sadjadbp avatar May 23 '16 20:05 sadjadbp

EDIT: Sorry everyone, this solution doesn't work. See comments further down.

ORIGINAL POST: I'm having the same issue as well, however I think I've got it working now (maybe). According to this https://github.com/dotnet/cli/blob/rel/1.0.0/Documentation/specs/runtime-configuration-file.md you also need to specify the runtime to force the platform properly. eg. outside next to buildOptions

"runtimes": { "win": [ "win7-x86", "win8-x86", "win10-x86" ] }

Try this to see if Visual Studio will pick this up (you might want to delete the win7-x64 folder first to ensure it rebuilds the project.

Quinn-L avatar May 24 '16 01:05 Quinn-L

@Quinn-L thanks, that seems to be the fix.

sadjadbp avatar May 24 '16 15:05 sadjadbp

@sadjadbp are you saying that you were able to get x86 outputs with "platform": "x86" and dotnet build merely by having that runtimes section present?

guardrex avatar May 24 '16 15:05 guardrex

@GuardRex seems like "platform": "x86" is irrelevant, but just in case I just left it there. I'm using Visual Studio not the command mode, and but adding what @Quinn-L suggested, I was able to get a win7-x86 build folder and the same goes for publishing via VS. I just installed SDK Preview 1 (x64) back again and removed the SDK Preview 1 (x86) from my PC. Now one project with these new settings would create win7-x86 folder and the other normal project creates a win7-x64 folder as it should.

sadjadbp avatar May 24 '16 15:05 sadjadbp

Sorry but I can't get it to work like that. I removed x86 SDK and installed x64 again, started Visual Studio, created a new "ASP.NET Core Web Application (.NET Framework)" added runtimes beside buildOptions and even added platform so it looks like this:

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true,
    "platform": "x86"
  },
  "runtimes": { "win": [ "win7-x86", "win8-x86", "win10-x86" ] },

Then did a build and ended up with a win7-x64 folder. Same goes for "dotnet build" and it also crashes (as before when using platform x86). My project.json: https://gist.github.com/WolfspiritM/df81be9ca3d1d6857ee5d534b032f230 Any ideas?

"dotnet --info" shows: Product Information: Version: 1.0.0-preview1-002702 Commit Sha: 6cde21225e

Runtime Environment: OS Name: Windows OS Version: 10.0.10586 OS Platform: Windows RID: win10-x64

WolfspiritM avatar May 24 '16 16:05 WolfspiritM

@WolfspiritM couple of VS restarts and now mine does not work, it seems to be caching the runtime version! So not fixed for now.

sadjadbp avatar May 24 '16 16:05 sadjadbp

Ok, I've restarted my machine like @sadjadbp and now it's no longer working on Visual Studio (ie. it's building against x64 again). Will need to play around more, unless there's a proper solution somewhere...

Quinn-L avatar May 24 '16 22:05 Quinn-L

Further research shows that x64 CLI can't build x86 apps https://github.com/dotnet/cli/issues/1485 We are required to install the x86 SDK and get this CLI to build our projects. So the reason it was originally working in VS on my machine was because I managed to get the x86 CLI as the primary CLI, but on restarting my VS is now with x64 CLI and breaking the build. Looks like those build settings are kind of pointless then...

Quinn-L avatar May 24 '16 23:05 Quinn-L

@Quinn-L Seems like whichever SDK you install the first (if you have both), it will be the default. I had x64 uninstalled and x86 installed. Both projects of mine (the one I wanted to be build in x86 and the other one that is fine under x64) was build in x86. Now I installed x64 on top of x86 SDK, and still both projects are compiled in x86 mode! As you said, seem like all those settings in project.json are useless.

They said, they'll move everything back to csproj to be MSBuild compatible in RC3 and forward. Maybe that'll fix our issues.

sadjadbp avatar May 25 '16 15:05 sadjadbp

@barrytang @abpiskunov can you take a look? Where should folks be opening VS Tooling issues?

TheRealPiotrP avatar May 26 '16 08:05 TheRealPiotrP

https://github.com/aspnet/Tooling/issues/522 @piotrpMSFT I've already added in Tooling

sadjadbp avatar May 26 '16 13:05 sadjadbp

Tooling issue was closed by @sayedihashimi , see his comment here: https://github.com/aspnet/Tooling/issues/522

abpiskunov avatar May 26 '16 21:05 abpiskunov

I reopened https://github.com/aspnet/Tooling/issues/522.

sayedihashimi avatar May 26 '16 23:05 sayedihashimi

I can reproduce the same problem with ASP.NET Core 1.0 and Visual Studio 2015 Update 3. The easiest way to reproduce this is to open Visual Studio and create a new ASP.NET Core Web Application (.NET Framework) project and select the "Empty" project template. Run the project without making any changes and verify that the "Hello World!" web page is shown. Note that the folder /bin/Debug/net452/win7-x64 is created.

Now stop the web app, delete the Debug folder and then open project.json and add "platfrom": "x86" to the buildOptions section:

"buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true,
    "platform": "x86"
},

Run the application again and this time a System.AggregateException is thrown from Microsoft.AspNetCore.Server.Kestrel.dll when host.Run() is called in Program.cs.

public class Program
{
    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run(); // <= this throws an exception when platform is set to x86
    }
}

Also note that the /bin/Debug/net452/win7-x64 is created instead of /bin/Debug/net452/win7-x86.

For completeness here is what my global.json file looks like:

{
  "projects": [ "src", "test" ],
  "sdk": {
    "version": "1.0.0-preview2-003121"
  }
}

MatLeger avatar Jul 07 '16 21:07 MatLeger

I tried almost all suggestions in this and related issues and they either don't generate any x86 binary or they result in BadFormatException.

As of now, is there any workaround to get a working x86 binary with version 1.0.0-preview2-003121 ?

My API is running well under x64 but I need to get a x86 binary one right now.

mafshin avatar Sep 03 '16 07:09 mafshin

I could get it working by running dotnet publish -r win7-x86 with the following configuration

  "runtimes": {
    "win7-x86": {},
    "win7-x64": {}
  },

  "buildOptions": {
    "platform": "x86",
    "emitEntryPoint": true,
    "preserveCompilationContext": true,
  },

The x86 build works just fine.

mafshin avatar Sep 13 '16 05:09 mafshin

For me, changing

"buildOptions": {
    "platform": "x86",
    "emitEntryPoint": true,
    "preserveCompilationContext": true,
  },

to

"buildOptions": {
    "emitEntryPoint": true
  },

Fixed it. I think it is because i'm running in a x64 environment.

jstensved avatar Apr 03 '17 20:04 jstensved