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

2.9k views Asked by At

.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.)

2

There are 2 answers

0
TheSpydog On BEST ANSWER

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>
5
Alsein On

Your assembly name libxxx.dll is confusing because of these reasons:

  • Libraries naming with lib are under the naming convention in unix/linux systems. But dll is used in Windows only. There is either foo.dll or libfoo.so/libfoo.dylib.

  • Libraries naming with libfoo.so or foo.dll means they are native assemblies, but native assemblies as dependencies are not handled automatically in MSBuild unless explicitly configured. A .NET managed assembly is named like Foo.Bar.dll.

Assuming that your libraries licenced under LGPL is a native assembly, the single file publish feature does not include it at all, so you may have to configure your project MSBuild file with a copy task to manually copy the assembly file from the project folder to the publish output folder, which must include this configuration to prevent it from being bundled:

<ExcludeFromSingleFile>true</ExcludeFromSingleFile>