{"record":{"id":"ab19984590b2d2a8","repo":"elsa-workflows/elsa-core","slug":"type-type-has-no-public-constructors-and-cannot-be","errorCode":null,"errorMessage":"Type {type} has no public constructors and cannot be instantiated.","messagePattern":"Type (.+?) has no public constructors and cannot be instantiated\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/modules/Elsa.Dsl.ElsaScript/Helpers/ActivityActivator.cs","lineNumber":19,"sourceCode":"using Elsa.Workflows;\n\nnamespace Elsa.Dsl.ElsaScript.Helpers;\n\ninternal static class ActivityActivator\n{\n    public static IActivity Create(Type type)\n    {\n        // Try parameterless ctor first (fast path)\n        var parameterless = type.GetConstructor(Type.EmptyTypes);\n        if (parameterless != null)\n            return (IActivity)parameterless.Invoke([]);\n\n        // Fall back: pick a public ctor and provide default values for args\n        var ctor = type\n                       .GetConstructors()\n                       .OrderBy(c => c.GetParameters().Length) // shortest first\n                       .FirstOrDefault()\n                   ?? throw new InvalidOperationException(\n                       $\"Type {type} has no public constructors and cannot be instantiated.\");\n\n        var parameters = ctor.GetParameters();\n        var args = new object?[parameters.Length];\n\n        for (var i = 0; i < parameters.Length; i++)\n        {\n            var p = parameters[i];\n\n            if (p.HasDefaultValue)\n            {\n                args[i] = p.DefaultValue;\n            }\n            else\n            {\n                args[i] = p.ParameterType.IsValueType\n                    ? Activator.CreateInstance(p.ParameterType)\n                    : null;","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/elsa-workflows/elsa-core/blob/fe9217bdfa0e27f0e09e45006eb6898f616e513d/src/modules/Elsa.Dsl.ElsaScript/Helpers/ActivityActivator.cs#L1-L37","documentation":"ActivityActivator.Create falls back to reflection: after other activation strategies fail, it picks the shortest public constructor and supplies default values for its parameters. If the type exposes no public constructors at all, this InvalidOperationException is thrown. It means Elsa cannot construct the activity type at runtime.","triggerScenarios":"Registering or invoking an activity type whose class has only internal/private constructors, a static class, or an abstract type with no public ctor reachable via GetConstructors().","commonSituations":"Custom activities accidentally given private constructors; activities defined in non-public classes; using types meant for DI-only construction; abstract base activities passed where concrete ones are expected.","solutions":["Add a public parameterless constructor (or make existing constructors public) to the activity type","Ensure the activity class is concrete and not abstract or static","Verify the type registered in the activity registry is the concrete implementation, not a base class","Use a factory/activator registration if the type requires DI-provided dependencies"],"exampleFix":"// before\nclass MyActivity {\n  private MyActivity() { }\n}\n// after\nclass MyActivity {\n  public MyActivity() { }\n}","handlingStrategy":"type-guard","validationCode":"if (!activityType.IsAbstract && activityType.GetConstructors().Length == 0)\n    throw new InvalidOperationException($\"{activityType.Name} lacks public constructors\");","typeGuard":"static bool IsInstantiableActivity(Type t) =>\n    !t.IsAbstract && !t.IsInterface && t.GetConstructors().Length > 0;","tryCatchPattern":"try { var activity = ActivityActivator.Create(type); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"no public constructors\"))\n{\n    logger.LogError(ex, \"Activity type {Type} is not constructible\", type.Name);\n    throw;\n}","preventionTips":["Always give activity classes a public constructor","Never register abstract or static types in the activity registry","Add a startup check that every registered activity type is instantiable"],"tags":["reflection","activity","instantiation","constructor"],"backgroundTag":"class-not-found","analyzedSha":"fe9217bdfa0e27f0e09e45006eb6898f616e513d","analyzedAt":"2026-09-13T20:32:34.702Z","contentChangedAt":"2026-09-13T20:32:34.702Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}