有没有一种方法可以使控制台应用程序仅使用.NET Core中的单个文件运行? [英] Is there a way to make a console application run using only a single file in .NET Core?

查看:312
本文介绍了有没有一种方法可以使控制台应用程序仅使用.NET Core中的单个文件运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET Framework中,您可以制作一个单独的 .EXE 文件,该文件将从命令行运行而无需任何其他配置文件(如果使用ILMerge,则可以将所有 .DLL 引用放入1个 .EXE 程序集)。

In .NET framework, you can make a single .EXE file that will run from the command line without having any extra config files (and if using ILMerge, you can put all .DLL references into the 1 .EXE assembly).

我正在尝试使用.NET Core来完成同一件事,但到目前为止没有成功。即使是最简单的 Hello World 应用程序,也没有依赖项,也需要有一个名为< MyApp> .runtimeconfig.json 的文件。以便使用 dotnet.exe 运行。

I am taking a stab at using .NET Core to accomplish the same thing, but so far without success. Even the simplest Hello World application with no dependencies requires there to be a file named <MyApp>.runtimeconfig.json in order to run using dotnet.exe.

dotnet F:\temp\MyApp.dll

< MyApp> .runtimeconfig.json 如下:

{
  "runtimeOptions": {
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "1.1.1"
    }
  }
}

没有此配置文件的文件与 .DLL ,出现以下错误:

Without this config file in the same folder as the .DLL, I get the following error:

A fatal error was encountered. The library 'hostpolicy.dll' required to
execute the application was not found in 'F:\temp'.

我的问题是:是否可以通过某种方式更改应用程序,因此不需要 存在此配置文件,以便将该信息的默认值编译在 .DLL 中,但是可以通过添加配置文件来覆盖它?

My question is: Is there some way to change the application so it doesn't require this config file to be present, so that the defaults of this information are compiled within the .DLL but can be overridden by adding the config file?


注意:我也想确保它正常工作,无论安装在哪个平台上,只要该平台具有正确的.NET版本即可。核心。

NOTE: I also want to ensure it "just works" regardless of the platform it is installed on it provided the platform has the right version of .NET Core.



背景



我正在尝试获得流畅的用户体验运行某些有时有用但很少需要的实用程序。由于它似乎无法使用从客户端应用程序引用的同一 .DLL 作为控制台应用程序,下一个最好的方法是拥有一个单个文件,该文件可以下载且运行时没有任何依赖关系。

Background

I am trying to get a smooth user experience for running some utilities that are useful sometimes, but are rarely ever needed. Since it doesn't appear to be possible to use the same .DLL that is referenced from a client application as a console application, the next best thing would be to have a single file that could be downloaded and run without any dependencies.

例如,在Java中,您只需下载一个 .jar 文件在任何受支持的平台上运行:

For example, in Java you can simply download a .jar file on any supported platform and run:

java <package>.jar <namespace>.SomeClass [args]

,它将正常工作而无需任何其他文件。

and it will "just work" without any extra files. How can I get a similar user experience using .NET Core?

总而言之,我想尝试避免多余的步骤首先解压缩到目录。 ..

In a nutshell, I want to try to avoid the extra step of "unzip to a directory first"...

推荐答案

更新2018年: .NET Core 3.0旨在实现一种新方案:打包.NET Core运行时和所有应用程序依赖项都合并为一个可执行文件。

Update 2018: .NET Core 3.0 aims to enable a new scenario: packing the .NET Core runtime and all application dependencies into a single executable.

目前,尚无故障保护方法来创建单个可执行文件。由于涉及大量类型转换dll文件,因此即使ILMerge和类似工具也可能无法产生正确的结果(尽管可能会得到改善,但问题是这些方案并未经过广泛的测试,尤其是在生产应用程序中)。

At the moment, there are no fail-safe methods to create a single executable file. Since there are a lot of type-forwarding dll files involved, even ILMerge and similar tools might not produce correct results (though this might improve, the problem is that those scenarios haven't undergone extensive testing, esp. in production applications)

当前有两种方法来部署.NET Core应用程序:

There are currently two ways to deploy a .NET Core application:


  • 便携式应用程序 /框架相关的应用程序 ,需要 dotnet 可执行文件并在目标计算机上安装了框架。在这里, XYZ.runtimeconfig.json 用于确定要使用的框架版本,并指定运行时参数。此部署模型允许在各种平台(Windows,Linux,Mac)上运行相同的代码

  • 作为自包含应用程序 :此处包含整个运行时发布的输出和可执行文件将生成(例如 yourapp.exe )。此输出特定于平台(通过运行时标识符设置),并且只能在目标操作系统上运行。但是,生成的可执行文件只是启动运行时并加载应用程序主dll文件的小填充程序。这还允许 XYZ.runtimeconfig.json 设置其他运行时属性,例如垃圾收集设置。(将其视为新的 app.config 文件)

  • As a "portable application" / "framework-dependent application", requiring a dotnet executable and installed framework on the target machine. Here, the XYZ.runtimeconfig.json is used to determine the framework version to use and also specifies runtime parameters. This deployment model allows running the same code on various platforms (windows, linux, mac)
  • As a "self-contained application": Here the entire runtime is included in the published output and an executable is generated (e.g. yourapp.exe). This output is specific to a platform (set via a runtime identifier) and can only be run on the targeted operating system. However, the produced executable is only a small shim that boots the runtime and loads the app's main dll file. This also allows an XYZ.runtimeconfig.json to set additional runtime properties like garbage collection settings.(think of it as a "new" app.config file)

将来, CoreRT运行时(在撰写本文时仍在开发中)旨在允许创建一个特定于运行时且不需要的预编译本机可执行文件任何其他文件。

In the future, the CoreRT runtime – which is still under development at the time of writing – aims to allow creating a single pre-compiled native executable that is specific to a runtime and does not require any other files.

这篇关于有没有一种方法可以使控制台应用程序仅使用.NET Core中的单个文件运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆