0

I have created a small .NET 6 console application (GPU Offers on GitHub), which is using a plugin system to dynamically (at runtime) load plugins (DLL files).

When I dotnet publish the console app (not the plugins) with the option to trim unused assemblies, the plugins don't get recognized as a plugin by the console app anymore...


private static IEnumerable<IPluginInterface> CreatePlugins(Assembly assembly)
{
    int count = 0;

    foreach (Type type in assembly.GetTypes())
    {
        if (typeof(IPluginInterface).IsAssignableFrom(type))
        {
            IPluginInterface result = Activator.CreateInstance(type) as IPluginInterface;
            if (result != null)
            {
                count++;
                yield return result;
            }
        }
    }

    if (count == 0)
    {
        string availableTypes = string.Join(", ", Enumerable.Select(assembly.GetTypes(), t => t.FullName));
        throw new ApplicationException(
            $"Can't find any type which implements IPluginInterface in {assembly} from {assembly.Location}.\nAvailable types: {availableTypes}");
    }
}

When I disable the trim unused assemblies option for the console application, the plugin is recognized again.

In my thought, the IPluginInterface cannot be treated as an "unused assembly", as it is actively used in the console application, as you can see above.

So, why the console application does not recognize the plugins anymore, when the trim unused assemblies publish option is enabled?

burnersk
  • 3,320
  • 4
  • 33
  • 56
  • I have the same problem, did you solve it ? It looks like a bug. Looking in Type.helpers.cs I see : [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2085:UnrecognizedReflectionPattern", Justification = "The GetInterfaces technically requires all interfaces to be preserved" + "But this method only compares the result against the passed in ifaceType." + "So if ifaceType exists, then trimming should have kept it implemented on any type.")] internal bool ImplementInterface(Type ifaceType) – Michel de Becdelièvre May 10 '22 at 12:51
  • I did not have solved it. For now, I do not trim the assemblies, which lead to unnecessary very large files. – burnersk May 11 '22 at 13:03

0 Answers0