I want to build a .NET Core project as a EXE and not a DLL so it can be executed.
The answer here did not work: How to run a .Net Core dll?
Here is the sample code:
using System;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Here is my project.json:
{
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
},
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {},
"frameworks": {
"netcoreapp1.1": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.1.0"
}
},
"imports": "dnxcore50"
}
}
}
I'm currently using VSCode, whenever I build the project with the build task, or run dotnet restore I just get a .dll in my bin/Debug folder.
How do you build a .NET Core application as an exe?
Bonus: I do, will that run on a mac or other device?
I think most people got to this page because they picked .net core and they can't get an executable .exe file from their VS 2017/VS 2019 build. vs 2015 always used to make a .exe file for a console application. With vs 2017/vs 2019 when you create the project you get 2 choices for a Console application. One is Console App (.NET Core) and the other choice is Console App (.NET Framework). If you pick the .NET Core option you are going to have to move heaven and earth to get a .exe file from your Build. The (.NET Core) option creates a .dll file from a Build. If you pick the (.NET Framework) option it will build a xxxx.exe executable for you by default.
To produce an EXE instead of a DLL, you need a self-contained deployment. What you are currently doing is a framework-dependent deployment.
To convert yours to self-contained, take the following steps in your project.json file.
Remove "type": "platform".
Add a "runtimes" section for the operating systems your app supports.
When you build, pass in the target operating system. E.g. dotnet build -r osx.10.10-x64.
This is the resultant project.json
{
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
},
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {},
"frameworks": {
"netcoreapp1.1": {
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.1.0"
}
},
"imports": "dnxcore50"
}
},
"runtimes": {
"win10-x64": {},
"osx.10.10-x64": {}
}
}
See also: https://learn.microsoft.com/en-us/dotnet/articles/core/deploying/#self-contained-deployments-scd
Related
I have an asp.net form application using .net 4.6.1 running on windows
and a linux application using .netcore 1.1
I want to make a nuget that can be shared between both application.
I created a .net core library and it works pretty fine with my linux application
here is its project.jso
{
"version": "0.1",
"dependencies": {
"NETStandard.Library": "1.6.0",
"Newtonsoft.Json": "9.0.1",
"StackExchange.Redis": "1.2.0"
},
"frameworks": {
"netstandard1.4": {
"imports": "netcoreapp1.0"
}
}
}
I can add my nuget package to the windows application and build it with no error, but when I want to run the application I'm getting all erros like:
Could not load file or assembly 'System.Diagnostics.DiagnosticSource,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
i'm using .net 4.6.1 so I believe it's .net core that is nagging
i can check in here and confirm that it's a .net core library.
I also setup the .net library in the production server. but still i'm getting the same error.
I tried all possible combination and version that i thought it would be logical. and have no clue how to solve it.
Im not sure why you say you are using net 4.6.1. Because you are using netstandard1.4 as framework, looking at your project.json file.
Try the following project.json file:
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {
"NETStandard.Library": "1.6.0",
"Newtonsoft.Json": "9.0.1",
"StackExchange.Redis": "1.2.0"
},
"frameworks": {
"netcoreapp1.1": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.1.0"
}
}
}
}
Make sure the production server has the runtime installed and Microsoft.NETCore.App-1.1.0
I am currently developing a desktop application in WPF, which uses an .NET Core library to make porting to different platforms easier. However, I cannot seem to reference the .NET Core library from the WPF app.
I tried the follwing solutions:
Reference the project:
Visual Studio complains about the project not being an .exe or .dll even though it is.
Reference the compiled .dll: This is really ugly, but it seems to work at first. Intellisense is OK with it and the WPF project compiles just fine. But as soon as I want to use any functionality from the .NET Core project a BadImageFormatException is thrown.
dotnet pack the project and reference the .nupkg: Installs a bunch of additional packages and throws a BadImageFormatException when any functionality is used.
From what I can gather there are two options here:
Do something really hacky like making a .NET Core Console project and passing all objects as strings between the two programs
Or:
Just give up on .NET Core and use EF6.
Here's my project.json:
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.EntityFrameworkCore.Sqlite": "1.0.0",
"Microsoft.EntityFrameworkCore.Design": {
"version": "1.0.0-preview2-final",
"type": "build"
}
},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
},
"imports": "dnxcore50"
},
"dnx451": {}
},
"tools": {
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
}
}
I tried both dnx451 and net451. The WPF project is also targeting .net 4.5.1. I am using "Visual Studio 2015 Update 3" with ".NET Core 1.0.1 VS 2015 Tooling Preview 2".
Your project.json isn't correct for a library. A library project should look like:
{
"dependencies": {
"Microsoft.EntityFrameworkCore.Sqlite": "1.0.0",
"Microsoft.EntityFrameworkCore.Design": {
"version": "1.0.0-preview2-final",
"type": "build"
}
},
"frameworks": {
"net451": { },
"netstandard1.3": {
"dependencies": {
"NETStandard.Library": "1.6.0"
}
}
},
"tools": {
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
}
}
When you run dotnet pack, two DLLs will be produced: one for .NET 4.5.1 and one for .NET Standard 1.3 (or whichever netstandard you want to target). The .NET 4.5.1 DLL should be compatible with your WPF project.
How can I tell Visual Studio to build against multiple runtimes?
I've created a simple .NET Core (1.0) console application (Hello World with HTTP download). I want to build it for multiple RIDs (win7-x64, win10-x64, etc) so that I can publish it and include the dependencies for a stand-alone application running on Windows 2008 R2 (win7-x64). But my developer machine where I'm building this is Windows 10, so VS picks win10-x64 so my publish task doesn't find any files for win7-x64. I also want to build specifically for win10-x64, so even if I can force it by removing the rest of the RIDs, I want the option to build against multiple.
I know I can do this with MSBuild and dotnet cli, but I'd like to know how to do it from within VS.
project.json:
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": "1.1.0",
"System.Net.Http": "4.1.0"
},
"runtimes": {
"win10-x64": {},
"win81-x64": {},
"win8-x64": {},
"win7-x64": {}
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}
Note that I'm using System.Net.Http which requires different dependencies in win7 vs win10 to test. If I run the win10-x64 on Win2k8 then it will fail because it's missing dependencies for the runtime on win7.
It shouldn't matter, but for reference here's the main code block, program.cs:
using System;
namespace TestOnWindows2008
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World");
using (var httpClient = new System.Net.Http.HttpClient())
{
string result = httpClient.GetStringAsync("https://www.microsoft.com/en-us/").Result;
Console.WriteLine(result.Substring(0, 200));
}
Console.WriteLine("Done");
}
}
}
Although I am not directly answering your question, maybe it will help you.
Since your target platform is win7-x64, which is older than your development platform, win10-x64, you can keep only win7-x64 and use it for both debugging and publishing.
I've just tested you code using Windows 10 x64 Enterprise and everything went ok.
project.json
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": "1.1.0",
"System.Net.Http": "4.1.0"
},
"runtimes": {
"win7-x64": {}
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}
And Program.cs is the one from your question.
Here is the output:
for run:
C:\temp\test2>dotnet restore && dotnet run
log : Restoring packages for C:\temp\test2\project.json...
log : Writing lock file to disk. Path: C:\temp\test2\project.lock.json
log : C:\temp\test2\project.json
log : Restore completed in 2603ms.
Project test2 (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing
Compiling test2 for .NETCoreApp,Version=v1.0
Compilation succeeded.
0 Warning(s)
0 Error(s)
Time elapsed 00:00:01.7257991
Hello World
Done
And for publish
C:\temp\test2>dotnet publish
Publishing test2 for .NETCoreApp,Version=v1.0/win7-x64
Project test2 (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
publish: Published to C:\temp\test2\bin\Debug\netcoreapp1.0\win7-x64\publish
Published 1/1 projects successfully
Every time I build a project using the new .NET Core RC2 templates I am not provided with a runnable .EXE file. If I hit F5 for debugging my console application it runs fine through the
C:\Program Files\dotnet\dotnet.exe
application. And if I use the
dotnet run
command in the folder, it runs fine as well. But I see no way to run the application without the .NET Core CLI tools.
The contents of my
bin\Debug\netcoreapp1.0\
folder looks like this:
As you can see there is no .EXE file available. Just the dll.
Am I overlooking something? Or is there something wrong with my project.json file?
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0-rc2-3002702"
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}
Thanks!
There are actually 2 app models in .NET Core:
Portable apps: heavily inspired by "DNX console apps", these apps don't produce .exe files and are instead executed by the .NET Core shared runtime (whose version is defined by the Microsoft.NETCore.App package, thanks to its special type: platform attribute). The corresponding .NET Core runtime must be installed on the machine to be able to use portable apps. If the exact version cannot be found, an exception is thrown when running dotnet run.
Standalone apps: standalone apps are really similar to good old .NET console apps as they produce .exe files. The .NET Core runtime doesn't have to be installed on the machine, because it is directly embedded with the application itself.
You're currently using the first model. To use the standalone model, you need to tweak your project.json:
Add a runtimes section to list the environments your app will target (e.g win7-x64 or ubuntu.14.04-x64). You can find the complete list here.
Remove the Microsoft.NETCore.App dependency. You can replace it by this package instead: "NETStandard.Library": "1.5.0-rc2-24027".
Here's an example of a standalone app:
{
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true,
"warningsAsErrors": true
},
"dependencies": {
"Microsoft.Extensions.Configuration.Binder": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.CommandLine": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
"Microsoft.Extensions.DependencyInjection": "1.0.0-rc2-final",
"Microsoft.Extensions.Logging": "1.0.0-rc2-final",
"Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
"NETStandard.Library": "1.5.0-rc2-24027"
},
"frameworks": {
"net451": { },
"netcoreapp1.0": {
"dependencies": {
"System.Net.Ping": "4.0.0-rc2-24027"
},
"imports": [
"dnxcore50",
"dotnet5.6",
"portable-net451+win8"
]
}
},
"runtimes": {
"win7-x64": { }
}
}
The answer is in the documentation with complete steps now.
You can create two types of deployments for .NET Core applications:
Framework-dependent deployment
Self-contained deployment
For a runnable .EXE file, the Publish self-contained should be used.
To create a runnable application from a .NET Core console application you can use the dotnet tool. Just run in your project directory:
dotnet publish --runtime win7-x64
This creates a standalone app (self-contained deployment; includes all necessary libraries consuming at least 60MB on your disk). Of course you can also choose other runtimes, like osx.10.11-x64 or ubuntu.16.04-x64.
If you used the default configuration (New Project -> Console App (.NET Core)), there is no modification of any configuration file necessary.
step 1: remove "type": "platform", from Project.json under frameworks section
step 2: add run time section to your project.json. Note each section is separeted by a comma. Add your runtime. below is just an example for win 10.
"runtimes": {
"win10-x64": {}
}
Step 3: dotnet restore command on your project. ( open cmd, go to your project folder wherever src folder is there, run dotnet restor)
step 4: dotnet pack
step 4: dotnet build -r win10-x64 - or just build.
Step 5: you can notice .exe created under debug/netcore/win10/
In ASP.NET Core try changing your app type to default, in project.json:
"Microsoft.NETCore.App": {
"type": "default",
"version": "1.0.0-*"
}
I've build my console application using dnu build command on my Mac. The output is MyApp.dll.
As it is not MyApp.exe, how can I execute it on windows, or even on Mac?
The code is:
using System;
class Program
{
public static void Main()
{
Console.WriteLine("Hello from Mac");
}
}
Add this to your project.json file:
"compilationOptions": {
"emitEntryPoint": true
},
It will generate the MyApp.exe on Windows (in bin/Debug) or the executable files on other platforms.
Edit: 30/01/2017
It is not enough anymore. You now have the possibility between Framework-dependent deployment and Self-contained deployment as described here.
Short form:
Framework-dependent deployment (.net core is present on the target system)
Run the dll with the dotnet command line utility dotnet MyApp.dll
Self-contained deployment (all components including .net core runtime are included in application)
Remove "type": "platform" from project.json
Add runtimes section to project.json
Build with target operating system dotnet build -r win7-x64
Run generated MyApp.exe
project.json file:
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.1"
}
}
}
},
"imports": "dnxcore50",
"runtimes": { "win7-x64": {} }
}
You can use dotnet publish to generate .exe output for your console app.
More details: Publish .NET Core apps with the CLI