How to publish a console application with COM interop and trim unused code

134 Views Asked by At

I have a simple console application that references a small third-party COM interop .NET assembly.

I want to trim unused code when publishing.

Enter image description here

However, when I run the trimmed published application, I get an error:

Unhandled exception. System.IO.FileNotFoundException: File name: '3rdPartyInteropLibrary, Version=1.3.0.0, Culture=neutral, PublicKeyToken=null' at ClassLibrary1.Class1.Print() at Program.Main()

Even when I manually copy the 3rdPartyInteropLibrary.dll file to the output folder I get the error.

How can I configure trimming so that it only trims my code and leave the external library untouched?

--------------------------------- .csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <Reference Include="Interop.bpac">
      <HintPath>Interop.bpac.DLL</HintPath>
    </Reference>
  </ItemGroup>

</Project>

public class Program
{
    public static void Main()
    {
        var printer = new bpac.Printer();
        var printers = printer.GetInstalledPrinters() as Array;
    }
}
0

There are 0 best solutions below