RicoSuter/NSwag · error · InvalidOperationException
Cannot create an instance of
Error message
Cannot create an instance of {commandType} because it does not have any accessible constructors and no dependency resolver is available. What it means
CreateCommand throws this InvalidOperationException when the command type has no accessible constructors at all (e.g. private, abstract, or static-only) AND no dependency resolver is available to have the DI container construct it. It is the terminal fallback of command instantiation.
Solutions
- Make the command class concrete with at least one public constructor (ideally parameterless).
- Provide a dependency resolver so the container can create the instance.
- Verify the registered Type is the concrete command class, not an abstract base or interface.
Example fix
// before
public abstract class MyCommand : IConsoleCommand { private MyCommand() {} }
// after
public class MyCommand : IConsoleCommand { public MyCommand() {} } Defensive patterns
Strategy: validation
Validate before calling
if (commandType.IsAbstract || commandType.GetConstructors(BindingFlags.Public | BindingFlags.Instance).Length == 0)
throw new ArgumentException($"{commandType} must be concrete with a public constructor."); Type guard
bool IsInstantiable(Type t) => !t.IsAbstract && !t.IsInterface && t.GetConstructors(BindingFlags.Public | BindingFlags.Instance).Any(c => !c.IsStatic);
Try / catch
try { return host.GetCommand(args); }
catch (InvalidOperationException ex) when (ex.Message.Contains("does not have any accessible constructors"))
{ throw new StartupException($"{commandType} is not constructible; add a public constructor or a resolver.", ex); } Prevention
- Register concrete classes only, never abstract bases or interfaces.
- Include a public (parameterless) constructor on every custom command.
- Exercise command creation in CI smoke tests.
When it happens
Trigger: The command type resolved from the registry exposes no public constructors and _dependencyResolver is null; reflection cannot Invoke any constructor and DI cannot be used.
Common situations: Registering an abstract base command class or a type with only private constructors; accidentally registering the wrong Type in a custom host.
Understand the failure class
Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.
Related errors
- No dependency resolver available to create a command…
- The specified runtime in the document
- Project outputs could not be located in
- The ouput of is a 32-bit application and requires…
- The ouput of is a 64-bit application and requires…
AI-assisted analysis of RicoSuter/NSwag@63daf8fcc3 (2026-09-14).
Data as JSON: /api/errors/5d0c8f6750ac4502.
Report an issue: GitHub.
Appendix: source
Thrown at src/NSwag.Commands/NConsole/CommandLineProcessor.cs:283
if (constructors.Any())
{
var constructor = constructors.First(c => !c.IsStatic);
if (constructor.GetParameters().Length > 0 && _dependencyResolver == null)
throw new InvalidOperationException("No dependency resolver available to create a command without default constructor.");
var parameters = constructor.GetParameters()
.Select(param => _dependencyResolver.GetService(param.ParameterType))
.ToArray();
command = (IConsoleCommand)constructor.Invoke(parameters);
}
else
{
if (_dependencyResolver == null)
{
throw new InvalidOperationException($"Cannot create an instance of {commandType} because it does not " +
$"have any accessible constructors and no dependency resolver is available.");
}
command = (IConsoleCommand)_dependencyResolver.GetService(commandType);
}
return command;
}
}
}
View on GitHub (pinned to 63daf8fcc3)