简体   繁体   中英

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

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM