简体   繁体   English

如何在Linux中从.NET Core 2.0创建可执行控制台应用程序?

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

As far as I know in the https://www.microsoft.com/net/learn/get-started/macos , we only able to run it: 据我所知,在https://www.microsoft.com/net/learn/get-started/macos中 ,我们只能运行它:

$ 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? 如何创建可以在这样的终端中运行的可执行文件?

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: 要在Linux中创建独立控制台应用程序,您应该为您的dotnet核心应用程序使用自包含部署(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. 这将生成捆绑目标框架的单个二进制文件,并且可以独立执行,无需任何额外的shell脚本或预先安装的dotnet运行时。

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. 我建议使用官方dotnet cli console模板,然后通过指定目标运行时 (例如: linux-x64 )和框架将您的项目作为--self-contained开关发布。

So start with the basic console template: 所以从基本的console模板开始:

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: 编辑program.cs以根据您的业务逻辑处理您的输入文件或其他参数,例如: nano program.cs并添加如下内容:

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. 使用dotnet run file_inputs.txt测试您的应用程序,它应该正确打印到控制台。

In order to get the binaries and the bundled framework to be distributed, you should run dotnet publish : 为了分发二进制文件和捆绑框架,你应该运行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: 然后,您可以通过以下方式分发您的publish folder ,然后执行您的应用:

./myconsoleapp file_inputs.txt

Here's a bit more detailed article about creating standalone Linux console app in dotnet core: 这里有一篇关于在dotnet核心中创建独立Linux控制台应用程序的更详细的文章:

Also can read more here about other available Linux templates at the publish page: 此处还可以在publish页面上阅读有关其他可用Linux模板的更多信息:

Finally, I'm able to find the answer and solve this by my self. 最后,我能够找到答案并通过我自己来解决这个问题。 I created a shell script and made it executable. 我创建了一个shell脚本并使其可执行。

$ touch my_console_app
$ chmod 777 my_console_app

I put this command to that newly created my_console_app file and saved it. 我把这个命令放到新创建的my_console_app文件中并保存。

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. 现在我可以使用这个可执行的shell脚本运行并执行我的.net核心项目,并且能够接受参数参数。

$ ./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: 如果您只有.net核心项目的.dll文件,则可以将my_console_app的内容更改为:

dotnet ./path/to/your/project.dll "$1"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为 .NET Core 控制台应用程序创建 Linux 安装程序 - Create Linux installer for .NET Core console app 发布的控制台应用程序上没有.NET Core 2.0应用程序设置 - No .NET Core 2.0 appsettings on a published console app 由于ArgumentNullException,带有主管的Linux上的.NET Core 2.0应用程序无法运行 - .NET Core 2.0 app on Linux with supervisor cannot run due to ArgumentNullException 如何从 .NET 核心控制台应用程序连接到 RDP 服务器? - How to connect to a RDP Server from a .NET Core console app? .net linux 上的核心控制台应用程序 - 要执行的应用程序不存在 - .net core console app on linux - the application to execute does not exist 如何在.Net Core控制台应用程序中配置代理 - How to configure a proxy in a .Net Core Console App .Net core 2.0控制台应用程序的日志记录和配置? - Logging and configuration for .Net core 2.0 console application? 将.NET控制台应用编译为单个可执行文件 - Compile .NET console app as single executable 如何将.net框架数据层引用到.net核心控制台应用程序 - How reference .net framework data layer to .net core console app 如何在 .NET 框架的控制台应用程序中创建通知? - How to create notification in console app in .NET Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM