PQ0003 — Framework-created class has more than one public constructor¶
The framework creates this class itself, and it cannot tell which public constructor to use.
The framework creates device classes, function classes, command handler classes, status pollers and DeviceAdapter for you. It passes their constructor parameters from the device tree and the adapter's services. To do that, it needs exactly one public constructor to call.
public partial class Bridge
{
public Bridge(IProtocol protocol) { }
// added for a unit test
public Bridge(IProtocol protocol, FakeClock clock) { }
}
Why the build refuses this¶
The adapter compiles, then fails at start. Without this check, the error appears only when the device starts:
Multiple constructors accepting all given argument types have been found in type 'Bridge'.
There should only be one applicable constructor.
The framework does not pick the "longest" or the "best" constructor. Every public constructor counts as a candidate, so two public constructors fail unless exactly one of them is marked [ActivatorUtilitiesConstructor].
How to fix it¶
Keep one public constructor. If a unit test needs a different setup, pass the test doubles through that constructor. Make an extra constructor internal and expose internals to the test project with InternalsVisibleTo.
Or mark the constructor the framework uses with [ActivatorUtilitiesConstructor] from Microsoft.Extensions.DependencyInjection. Mark exactly one; two marked constructors fail the same way.
What is not reported¶
Non-public constructors. The framework never calls internal, protected or private constructors.
Static command handlers. The framework calls them without creating an instance.
If you disagree with a report¶
Do not suppress it. A wrong report is a bug in the check — report it with the class that triggered it.