{"record":{"id":"5d0c8f6750ac4502","repo":"RicoSuter/NSwag","slug":"cannot-create-an-instance-of-commandtype-because-it-does-not","errorCode":null,"errorMessage":"Cannot create an instance of {commandType} because it does not have any accessible constructors and no dependency resolver is available.","messagePattern":"Cannot create an instance of (.+?) because it does not have any accessible constructors and no dependency resolver is available\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/NSwag.Commands/NConsole/CommandLineProcessor.cs","lineNumber":283,"sourceCode":"\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\n            return command;\n        }\n    }\n}\n","sourceCodeStart":265,"sourceCodeEnd":294,"githubUrl":"https://github.com/RicoSuter/NSwag/blob/63daf8fcc3a25151b62eb4b326a1e8ea048a0d41/src/NSwag.Commands/NConsole/CommandLineProcessor.cs#L265-L294","documentation":"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.","triggerScenarios":"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.","commonSituations":"Registering an abstract base command class or a type with only private constructors; accidentally registering the wrong Type in a custom host.","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."],"exampleFix":"// before\npublic abstract class MyCommand : IConsoleCommand { private MyCommand() {} }\n// after\npublic class MyCommand : IConsoleCommand { public MyCommand() {} }","handlingStrategy":"validation","validationCode":"if (commandType.IsAbstract || commandType.GetConstructors(BindingFlags.Public | BindingFlags.Instance).Length == 0)\n    throw new ArgumentException($\"{commandType} must be concrete with a public constructor.\");","typeGuard":"bool IsInstantiable(Type t) => !t.IsAbstract && !t.IsInterface && t.GetConstructors(BindingFlags.Public | BindingFlags.Instance).Any(c => !c.IsStatic);","tryCatchPattern":"try { return host.GetCommand(args); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"does not have any accessible constructors\"))\n{ throw new StartupException($\"{commandType} is not constructible; add a public constructor or a resolver.\", ex); }","preventionTips":["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."],"tags":["reflection","constructor","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"}