0

I have a simple .NET Core .csproj for a project I want to deploy in two ways:

  1. As a self-contained, trimmed, single-file binary (a portable mytool.exe, with no other files)
  2. As an un-trimmed, multi-file DLL (dotnet mytool.dll, with some other supporting DLLs in the folder)

I want to do this because single-file .NET Core binaries are very slow to cold-start. I want this tool to be as portable as possible, so I need a single-file .NET Core binary, but I also want to let users call the much-faster dotnet mytool.dll if they don't need the portability.

I have configured my tool to build a self-contained, trimmed, single-file binary:

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RuntimeIdentifier>win10-x64</RuntimeIdentifier>
    <PublishReadyToRun>true</PublishReadyToRun>
    <PublishSingleFile>true</PublishSingleFile>
    <PublishTrimmed>true</PublishTrimmed>
  </PropertyGroup>

Is there an easy way to provide multiple "configurations" or "targets" that can be easily built from the command line (or in my case an ADO pipeline) so I can support my other configuration? E.g.:

  <!-- Other configuration (multi-file), somehow -->
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <PublishReadyToRun>true</PublishReadyToRun>
  </PropertyGroup>

Disclaimer: I work for Microsoft.

citelao
  • 4,898
  • 2
  • 22
  • 36

1 Answers1

0

Building on Pavel's comment, I found some relevant information.

Then, following the .NET application publishing overview, I set up a custom property (flavor) to control the build:

  <!--
    Default flavor is SINGLEBINARY
    https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2015/msbuild/how-to-build-the-same-source-files-with-different-options?view=vs-2015
  -->
  <PropertyGroup>
      <Flavor Condition="'$(Flavor)'==''">SINGLEBINARY</Flavor>
  </PropertyGroup>

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

  <!-- If flavor is SINGLEBINARY, output a standalone EXE -->
  <PropertyGroup Condition="'$(Flavor)'=='SINGLEBINARY'">
    <RuntimeIdentifier>win10-x64</RuntimeIdentifier>
    <PublishReadyToRun>true</PublishReadyToRun>
    <PublishSingleFile>true</PublishSingleFile>
    <PublishTrimmed>true</PublishTrimmed>
  </PropertyGroup>

  <!-- If flavor is MULTIBINARY, output a more-typical dotnet DLL -->
  <PropertyGroup Condition="'$(Flavor)'=='MULTIBINARY'">
    <!-- No specific options; the default publish works. -->
  </PropertyGroup>

I can now do separate builds and publishes based on this property:

dotnet build /p:flavor=singlebinary # single-binary; self-contained EXE for easy sharing
dotnet build /p:flavor=multibinary # multi-binary

dotnet publish --configuration Release /p:flavor=singlebinary
dotnet publish --configuration Release /p:flavor=multibinary
citelao
  • 4,898
  • 2
  • 22
  • 36