Preparing NServiceBus for trimming and AOT

Trimming a tree blindfolded is probably not the best idea in the world. The best-case scenario is “at least the tree didn’t die.” The worst-case scenario is a trip to the emergency room. 1
The .NET ecosystem is getting into the tree-trimming business (in a manner of speaking), and we’re starting to make changes to NServiceBus to ensure you won’t be caught trimming your apps with a blindfold on.
Trimming and ahead-of-time compilation (AOT) reward code that the compiler can see. That changes some of the design assumptions for a framework like NServiceBus, which until now has relied on runtime discovery to maintain a productive developer experience.
Read moreThose scissors are sharp!
This is because source generators do not perform well when driven off implemented interfaces, so Microsoft recommends you Never Do That®. Scans using an attribute are the fastest and most optimized code path, which is important because a source generator can technically run every time you press a key in your IDE.
To check a package for yourself, navigate to the NuGet package page, click the Open in NuGet Package Explorer link such as this one for Npgsql, open the
libfolder, then (generally) the latestnet*.0directory, and then click on the assembly DLL. Now look below for any AssemblyMetadata values. A package is trimmable if the keyIsTrimmablehas valueTrue, and is AOT-compatible ifIsAotCompatiblehas valueTrue.That's right, if the source generator generates code that refers to your handler type, it will no longer be flagged as having no references!























