我如何在 dotnet 发布时更改 dll 名称?

How do I change the dll name when I dotnet publish?

除了最后一行之外,我所有的 .net 核心 docker 文件都是 99% 相同的: ENTRYPOINT ["dotnet", "<APP NAME HERE>.dll"]

我觉得很蠢,否则它们可能完全相同

有没有办法用 dotnet publish -c Release -o out 命令更改 dll 名称?我可以在不修改 csproj 文件或任何东西的情况下做到这一点吗?

正如the documentation建议的那样:

You can override the ENTRYPOINT instruction using the docker run --entrypoint flag.

命令看起来像这样:

docker run --entrypoint dotnet YOUR_IMAGE "<APP NAME HERE>.dll"

正如另一个 helpful bit of documentation 所建议的,您也可以在 docker run 命令末尾将参数传递给入口点,如下所示:

docker run YOUR_IMAGE "<APP NAME HERE>.dll"

这还需要您将 ENTRYPOINT 更改为:

ENTRYPOINT ["dotnet"]

您可以使用此命令执行构建并重命名程序集:

dotnet msbuild -r -p:Configuration=Release;AssemblyName=foo

在 Linux/macOS 上,您必须在命令周围添加引号,如下所示:

dotnet msbuild -r '-p:Configuration=Release;AssemblyName=foo'

但是,global property being set. You should read this open issue from Sep 2019 as it speaks directly to your question regarding Docker and renaming the output: https://github.com/dotnet/msbuild/issues/4696

可能会产生意想不到的副作用

此外,我知道您想避免编辑 .csproj 文件,但如果您不知道,您可以添加 AssemblyName 属性 并以这种方式设置输出名称.

如:

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AssemblyName>foo</AssemblyName>
  </PropertyGroup>

这将创建 foo.dll(以及其他文件,例如 .pdb.deps.json 等)