{"record":{"id":"191eb9aa75af93fa","repo":"RicoSuter/NSwag","slug":"no-dependency-resolver-available-to-create-a-command-without","errorCode":null,"errorMessage":"No dependency resolver available to create a command without default constructor.","messagePattern":"No dependency resolver available to create a command without default constructor\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/NSwag.Commands/NConsole/CommandLineProcessor.cs","lineNumber":271,"sourceCode":"            }\n            else\n            {\n                throw new InvalidOperationException($\"Could not retrieve command from arguments {string.Join(\", \", args)}\");\n            }\n        }\n\n        /// <exception cref=\"InvalidOperationException\">No dependency resolver available to create a command without default constructor.</exception>\n        private IConsoleCommand CreateCommand(Type commandType)\n        {\n            var constructors = commandType.GetTypeInfo().DeclaredConstructors;\n            IConsoleCommand command;\n\n            if (constructors.Any())\n            {\n                var constructor = constructors.First(c => !c.IsStatic);\n\n                if (constructor.GetParameters().Length > 0 && _dependencyResolver == null)\n                    throw new InvalidOperationException(\"No dependency resolver available to create a command without default constructor.\");\n\n                var parameters = constructor.GetParameters()\n                    .Select(param => _dependencyResolver.GetService(param.ParameterType))\n                    .ToArray();\n\n                command = (IConsoleCommand)constructor.Invoke(parameters);\n            }\n            else\n            {\n                if (_dependencyResolver == null)\n                {\n                    throw new InvalidOperationException($\"Cannot create an instance of {commandType} because it does not \" +\n                                                        $\"have any accessible constructors and no dependency resolver is available.\");\n                }\n\n                command = (IConsoleCommand)_dependencyResolver.GetService(commandType);\n            }\n","sourceCodeStart":253,"sourceCodeEnd":289,"githubUrl":"https://github.com/RicoSuter/NSwag/blob/63daf8fcc3a25151b62eb4b326a1e8ea048a0d41/src/NSwag.Commands/NConsole/CommandLineProcessor.cs#L253-L289","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar host = new NSwagCommandLineHost(); // no dependency resolver\n// after\nhost.DependencyResolver = new SimpleInjectorDependencyResolver(container);","handlingStrategy":"validation","validationCode":"var ctor = commandType.GetConstructors();\nif (!ctor.Any(c => c.GetParameters().Length == 0) && hostDependencyResolver == null)\n    throw new InvalidOperationException($\"{commandType} needs a dependency resolver or a parameterless constructor.\");","typeGuard":null,"tryCatchPattern":"try { return host.GetCommand(args); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"No dependency resolver\"))\n{ throw new StartupException($\"Register a dependency resolver for {commandType.Name}.\", ex); }","preventionTips":["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."],"tags":["reflection","dependency-injection","cli"],"backgroundTag":"missing-dependency","analyzedSha":"63daf8fcc3a25151b62eb4b326a1e8ea048a0d41","analyzedAt":"2026-09-14T11:38:15.205Z","contentChangedAt":"2026-09-14T11:38:15.205Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}