RicoSuter/NSwag · error · InvalidOperationException
No dependency resolver available to create a command…
Error message
No dependency resolver available to create a command without default constructor.
What it means
CreateCommand instantiates the resolved command type by reflection, preferring a parameterless constructor. If the only accessible constructor has parameters and no IDependencyResolver was configured on the processor, it cannot supply dependencies and throws this InvalidOperationException.
Solutions
- Add a parameterless constructor to the command class, or
- Configure a dependency resolver on the command host before processing (e.g. a class implementing IDependencyResolver returning GetService instances).
- Register the command through the overload that accepts a factory/instance instead of relying on reflection.
Example fix
// before var host = new NSwagCommandLineHost(); // no dependency resolver // after host.DependencyResolver = new SimpleInjectorDependencyResolver(container);
Defensive patterns
Strategy: validation
Validate before calling
var ctor = commandType.GetConstructors();
if (!ctor.Any(c => c.GetParameters().Length == 0) && hostDependencyResolver == null)
throw new InvalidOperationException($"{commandType} needs a dependency resolver or a parameterless constructor."); Try / catch
try { return host.GetCommand(args); }
catch (InvalidOperationException ex) when (ex.Message.Contains("No dependency resolver"))
{ throw new StartupException($"Register a dependency resolver for {commandType.Name}.", ex); } Prevention
- Give custom commands a parameterless constructor when possible.
- Always set DependencyResolver when registering DI-based commands.
- Test command instantiation in a unit test at host startup.
When it happens
Trigger: A registered command type exposes only constructors with parameters while CommandLineProcessorHost/processor was built without a dependency resolver; CreateCommand then fails at the _dependencyResolver == null check.
Common situations: Custom NConsole commands with injected dependencies registered in a host created via the default constructor; library upgrades changing a built-in command's constructor signature.
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
- Cannot create an instance of
- API Explorer not registered in DI.
- The NSwag DI services are not registered: Call…
- The OpenAPI/Swagger document
- The specified runtime in the document
AI-assisted analysis of RicoSuter/NSwag@63daf8fcc3 (2026-09-14).
Data as JSON: /api/errors/191eb9aa75af93fa.
Report an issue: GitHub.
Appendix: source
Thrown at src/NSwag.Commands/NConsole/CommandLineProcessor.cs:271
}
else
{
throw new InvalidOperationException($"Could not retrieve command from arguments {string.Join(", ", args)}");
}
}
/// <exception cref="InvalidOperationException">No dependency resolver available to create a command without default constructor.</exception>
private IConsoleCommand CreateCommand(Type commandType)
{
var constructors = commandType.GetTypeInfo().DeclaredConstructors;
IConsoleCommand command;
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);
}
View on GitHub (pinned to 63daf8fcc3)