如何在没有 App.runtimeconfig.json 的情况下构建 App?

How to build App without App.runtimeconfig.json?

当我在 Visual Studio 中以 Release 模式构建 App 时,它会在 Release 文件夹中生成这些文件:

App.exe 
App.dll 
App.runtimeconfig.json 
App.pdb
App.deps.json
App.runtimeconfig.dev.json

如果我移动前 3 个文件的位置并双击 App.exe 它就会启动。如果我删除 App.runtimeconfig.json 它不会启动!

我只想在我的文件夹中保留 App.exeApp.dll,我该怎么做才能摆脱 App.runtimeconfig.json


编辑 #1

这里是App.runtimeconfig.json的内容:

{
  "runtimeOptions": {
    "tfm": "netcoreapp3.0",
    "framework": {
      "name": "Microsoft.WindowsDesktop.App",
      "version": "3.0.0"
    }
  }
}

编辑 #2

这是我 App.csproj 文件 PropertyGroup 中的内容:

<PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <PublishTrimmed>true</PublishTrimmed>
    <PublishReadyToRun>true</PublishReadyToRun>
    <PublishSingleFile>true</PublishSingleFile>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <UseWPF>true</UseWPF>
</PropertyGroup>

现在我在 Visual studio 的 Error List 中得到了这个:

Error NETSDK1047 Assets file 'C:\Users\Emon\source\repos\SocketTest\App\obj\project.assets.json' doesn't have a target for '.NETCoreApp,Version=v3.0/win-x64'. Ensure that restore has run and that you have included 'netcoreapp3.0' in the TargetFrameworks for your project. You may also need to include 'win-x64' in your project's RuntimeIdentifiers. App C:\Program Files\dotnet\sdk.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets 234

当我右键单击项目时,我看到:构建、清理、重建等,没有恢复!


编辑 #3

我先删除了 binobj 文件夹,然后重新排序 <PropertyGroup><RuntimeIdentifier> 放在 <TargetFramework> 下面,现在可以使用了!

如您所见,App.runtimeconfig.json包含运行时信息和使用的.NET Core 版本,您不能简单地删除它。但是您在发布应用程序时通过选择 Self-contained 部署模式切换到自包含部署

.NET Core 引入了 trim 结果可执行文件以减小大小的能力。基本上,您的项目文件中需要以下属性

<PublishTrimmed>true</PublishTrimmed>
<PublishReadyToRun>true</PublishReadyToRun>
<PublishSingleFile>true</PublishSingleFile>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>

有一个非常好的article关于单个可执行文件

我想让我的 csproj 文件尽可能通用,并且不想在该文件中指定任何 RuntimeIdentifiers。我宁愿将 RuntimeIdentifier 指定为命令行构建选项。这是我想出的:

csproj:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <GenerateRuntimeConfigurationFiles>false</GenerateRuntimeConfigurationFiles>
        <OutputType>Exe</OutputType>
        <Platforms>AnyCPU;x64</Platforms>
        <PublishReadyToRun>true</PublishReadyToRun>
        <PublishSingleFile>true</PublishSingleFile>
        <PublishTrimmed>true</PublishTrimmed>
        <TargetFramework>net5.0</TargetFramework>
    </PropertyGroup>
</Project>

命令行:

dotnet publish --runtime win-x64 -c Release