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

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

问题描述

在 .NET 框架中,您可以制作一个从命令行运行的 .EXE 文件,而无需任何额外的配置文件(如果使用 ILMerge,您可以将所有 .DLL 引用到 1 .EXE 程序集).

我正在尝试使用 .NET Core 来完成同样的事情,但到目前为止还没有成功.即使最简单的 Hello World 应用程序没有依赖项,也需要有一个名为 .runtimeconfig.json 的文件才能使用 dotnet.exe.

dotnet F:	empMyApp.dll

.runtimeconfig.json的内容如下:

<代码>{运行时选项":{框架": {"name": "Microsoft.NETCore.App",版本":1.1.1"}}}

如果没有与 .DLL 位于同一文件夹中的此配置文件,我会收到以下错误:

遇到致命错误.库hostpolicy.dll"需要在F:	emp"中找不到执行应用程序.

我的问题是:是否有某种方法可以更改应用程序,使其要求此配置文件存在,以便在 中编译此信息的默认值.DLL 但可以通过添加配置文件来覆盖吗?

<块引用>

注意:我还想确保它正常工作",无论它安装在哪个平台上,只要该平台具有正确版本的 .NET Core.

背景

我试图获得流畅的用户体验,以运行一些有时有用但很少需要的实用程序.因为它 似乎不可能使用从客户端应用程序引用的相同 .DLL 作为控制台应用程序,下一个最好的方法是使用 单个文件,无需任何依赖即可下载和运行.

例如,在 Java 中,您可以简单地在任何支持的平台上下载 .jar 文件并运行:

java .jar .SomeClass [args]

它会正常工作"而无需任何额外文件.如何使用 .NET Core 获得类似的用户体验?

简而言之,我想尽量避免先解压缩到目录"的额外步骤...

解决方案

2018 年更新: .NET Core 3.0 旨在启用一个新方案:将 .NET Core 运行时和所有应用程序依赖项打包到一个单个可执行文件.

目前,没有故障安全方法来创建单个可执行文件.由于涉及到大量类型转发的 dll 文件,即使是 ILMerge 和类似工具也可能无法产生正确的结果(虽然这可能会有所改善,但问题是这些场景没有经过广泛的测试,尤其是在生产应用中)

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

  • 作为 便携式应用程序"/依赖框架的应用程序",需要 dotnet 可执行文件并在目标机器上安装框架.此处,XYZ.runtimeconfig.json 用于确定要使用的框架版本并指定运行时参数.这种部署模型允许在各种平台(windows、linux、mac)上运行相同的代码
  • 作为 "自包含应用程序":这里整个运行时都包含在发布的输出中,并生成一个可执行文件(例如 yourapp.exe).此输出特定于平台(通过运行时标识符设置)并且只能在目标操作系统上运行.但是,生成的可执行文件只是一个小垫片,用于启动运行时并加载应用程序的主 dll 文件.这也允许 XYZ.runtimeconfig.json 设置额外的运行时属性,如垃圾收集设置.(将其视为新"app.config 文件)

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

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).

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:	empMyApp.dll

The contents of the <MyApp>.runtimeconfig.json are as follows:

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

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:	emp'.

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?

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.

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.

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"...

解决方案

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.

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)

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

  • 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)

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天全站免登陆