.net-core.net-5.net-core-publishsinglefile

How to exclude an assembly from being packed into a PublishSingleFile app


.NET Core and .NET 5 have the single file executable feature.

How can I exclude a managed assembly from being packed into that single file?

Example: I might want that app.exe includes MyLib.A.dll and MyLib.B.dll, but not MyLib.Special.dll. Instead, I might want that MyLib.Special.dll resides on disk next to app.exe and is loaded from there.

(Background: It could be that MyLib.Special.dll is licensed under the L-GPL, but app.exe is proprietary.)


Solution

  • You can use ExcludeFromSingleFile inside of a ProjectReference element to prevent the assembly from being embedded, like so:

      <ItemGroup>
        <ProjectReference Include="MyLib.Special.csproj">
          <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
        </ProjectReference>
      </ItemGroup>