How to create executable console app from .NET Core 2.0 in Linux?

17,318

Solution 1

In order to create a standalone console app in Linux, you should use a self-contained deployment (SCD) publishing mode for your dotnet core app:

This will generate a single binary that bundles the target framework and can be executed independently without any extra shell-scripts or pre-installed dotnet runtime.

I recommend to use the official dotnet cli console template and then publish your project as a --self-contained switch by specifying your target runtime (eg: linux-x64) and framework what you use.

So start with the basic console template:

dotnet new console -o myconsoleapp
cd myconsoleapp

Edit program.cs to process your input-file or other arguments based on your business logic, eg: nano program.cs and add something like:

using System;
namespace myconsoleapp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Linux!");
            if (args.Length > 0) { Console.WriteLine("Input is: " + args[0]); }
        }
    }
}

Test your application with dotnet run file_inputs.txt and it should print to the console correctly.

In order to get the binaries and the bundled framework to be distributed, you should run dotnet publish:

dotnet publish -c release --self-contained --runtime linux-x64 --framework netcoreapp2.0

You can then distribute your publish folder and then execute your app just simply by:

./myconsoleapp file_inputs.txt

Here's a bit more detailed article about creating standalone Linux console app in dotnet core:

Also can read more here about other available Linux templates at the publish page:

Solution 2

Finally, I'm able to find the answer and solve this by my self. I created a shell script and made it executable.

$ touch my_console_app
$ chmod 777 my_console_app

I put this command to that newly created my_console_app file and saved it.

dotnet run --project ./path/to/your/project.csproj "$1"

Now I can run and execute my .net core project using this executable shell script and able to accept a parameter argument.

$ ./my_console_app file_inputs.txt

EDIT:

If you only have the .dll file from .net core project you can change the content of my_console_app into:

dotnet ./path/to/your/project.dll "$1"
Share:
17,318

Related videos on Youtube

Anang Satria
Author by

Anang Satria

Updated on June 13, 2022

Comments

  • Anang Satria
    Anang Satria almost 2 years

    As far as I know in the https://www.microsoft.com/net/learn/get-started/macos, we only able to run it:

    $ dotnet run
    

    I have a requirement to create a console app that we execute from a terminal as below:

    $ ./my_console_app file_inputs.txt
    

    How to create an executable that can run in a terminal like that?

    • Anang Satria
      Anang Satria about 6 years
      I wonder why people are giving the question downvote, while I believe this is a pretty important question :(
  • Anang Satria
    Anang Satria about 6 years
    Hi @muratiakos, so by doing that I can create an executable like what I want? it seems you're not answering my question.
  • Lex Li
    Lex Li about 6 years
    when you have time, read about self contained deployment, and dotnet publish --self-contained. That would show you an even simpler approach.
  • muratiakos
    muratiakos about 6 years
    Added now the self-contained description and article references to generate a single binary for execution without a shell script
  • muratiakos
    muratiakos about 6 years
    Shell wrapper is also good approach, if you have dotnet runtime pre-installed already, but you can also check my answer regarding SCD publishing for single runtime bundled deployments: stackoverflow.com/a/49692221/4358231
  • Anang Satria
    Anang Satria about 6 years
    Thanks, it could be a good alternative if we want to publish a standalone application. +1