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?
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.
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.
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.
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"
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With