Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 434
Anang Satria Avatar asked Apr 06 '18 11:04

Anang Satria


People also ask

How do I create an EXE for .NET Core console application?

First, right-click on the project and then hit Publish, then select folder and click create. Click the edit button to edit the configuration. In the publish configuration you can check the single EXE option. Once ready, save and click Publish.

Can we host .NET Core application in Linux?

Move the application files to the Linux server Publish the ASP.NET Core application you want to host to a local folder. Then, connect to the Linux Server using FileZilla to move the published files. Open the folder /var/www in the Linux server. Create a new folder named sample and then move the published files to it.

How do I run a .NET Core console app?

In . NET Core, it runs from the dll, so you have to just run the application by running the command prompt and using the command - dotnet run. Open your command prompt and go to that folder where your application persists.


2 Answers

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"
like image 31
Anang Satria Avatar answered Oct 05 '22 21:10

Anang Satria


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:

  • https://docs.microsoft.com/en-us/dotnet/core/deploying/index#self-contained-deployments-scd

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:

  • https://blogs.msdn.microsoft.com/luisdem/2016/10/11/net-core-how-to-publish-a-self-contained-application-exe/

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

  • https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-publish?tabs=netcore2x
like image 194
muratiakos Avatar answered Oct 05 '22 21:10

muratiakos