使用 Windows 兼容包部署 .NET Core 应用程序

Deploying .NET Core Application with Windows Compatibility Pack

我正忙于将 .NET Core 2.1 应用程序部署到我们的测试环境中,但出现以下错误。

Error:
  An assembly specified in the application dependencies manifest (MyApp.deps.json) was not found:
    package: 'System.Diagnostics.EventLog', version: '4.5.0'
    path: 'runtimes/win/lib/netcoreapp2.1/System.Diagnostics.EventLog.dll'

我们正在使用 Windows Compatibility Pack 访问事件日志。

我在依赖项 Json 文件中有以下项目:

"System.Diagnostics.EventLog/4.5.0": {
  "dependencies": {
    "Microsoft.Win32.Registry": "4.5.0",
    "System.Security.Permissions": "4.5.0",
    "System.Security.Principal.Windows": "4.5.0",
    "System.Threading.AccessControl": "4.5.0"
  },
  "runtime": {
    "lib/netstandard2.0/System.Diagnostics.EventLog.dll": {
      "assemblyVersion": "4.0.0.0",
      "fileVersion": "4.6.26515.6"
    }
  },
  "runtimeTargets": {
    "runtimes/win/lib/netcoreapp2.0/System.Diagnostics.EventLog.dll": {
      "rid": "win",
      "assetType": "runtime",
      "assemblyVersion": "4.0.0.0",
      "fileVersion": "4.6.26515.6"
    }
  }
}

请告知应该如何部署这些依赖项。另外,这个相对路径的根文件夹是什么 runtimes/win/lib/netcoreapp2.0?

我们实际上为我们的场景找到了解决方案: - 我们的情况是我们试图 运行 在我们的测试代理上进行基于 netcoreapp 的测试项目 - dotnet test 在项目文件上工作 - dotnet vstest 有时在项目输出目录上工作(我们不确定为什么以及在哪个设置上) - 当 运行 进入另一个目录并从 CI 下载时,dotnet vstest 确实 运行 出现了上述错误 - dotnet vstest 在测试代理上 运行 变成了 AssemblyNotFoundException(这对我们没有任何意义)

解决方案是对我们的测试项目使用 dotnet publish,并在测试代理上使用 "self-contained" 输出到 运行。 dotnet publish 将所需的 runtimes/win/lib/netcoreappX.X/*.dll 文件复制到发布输出目录中。

经过大量测试,关键问题似乎是“RuntimeIdentifiers”。发布时有一个可见的选项,但为了在构建时使用它,您需要向 .csproj 文件添加几个标签。

第一个是:

<RuntimeIdentifier>win-x86</RuntimeIdentifier>

这将导致 NuGet 检索正确的 dll(根据您的需要更改值)。对我来说,我正在编译到 x86 平台。我不知道默认情况下 NuGet 得到了什么,但无论它是什么,相同文件的文件大小都不同。

您还应该添加此标签:

<SelfContained>false</SelfContained>

否则您的构建将默认复制整个框架。

另请注意,使用 RuntimeIdentifier 标记将使您的默认输出文件夹包含您指定的值。例如我的子文件夹变成了:

Project\bin\x86\Debug\netcoreapp3.1\win-86\

对于发布,您应该可以做类似的事情;问题是将您的 RuntimeIdentifier 与您的平台相匹配。除非您特别需要,否则您不需要指定 SelfContained。