elsa-workflows/elsa-core · error · InvalidOperationException
Type has no public constructors and cannot be instantiated.
Error message
Type {type} has no public constructors and cannot be instantiated. What it means
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.
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
Example fix
// before
class MyActivity {
private MyActivity() { }
}
// after
class MyActivity {
public MyActivity() { }
} Defensive patterns
Strategy: type-guard
Validate before calling
if (!activityType.IsAbstract && activityType.GetConstructors().Length == 0)
throw new InvalidOperationException($"{activityType.Name} lacks public constructors"); Type guard
static bool IsInstantiableActivity(Type t) =>
!t.IsAbstract && !t.IsInterface && t.GetConstructors().Length > 0; Try / catch
try { var activity = ActivityActivator.Create(type); }
catch (InvalidOperationException ex) when (ex.Message.Contains("no public constructors"))
{
logger.LogError(ex, "Activity type {Type} is not constructible", type.Name);
throw;
} Prevention
- 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
When it happens
Trigger: 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().
Common situations: 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.
Related errors
- No matching constructor found for activity type
- Multiple matching constructors found for activity type
- Unexpected non-optional, non-Input
- Can't find method name
- Value cannot be null. (Parameter 'type')
AI-assisted analysis of elsa-workflows/elsa-core@fe9217bdfa (2026-09-13).
Data as JSON: /api/errors/ab19984590b2d2a8.
Report an issue: GitHub.
Appendix: source
Thrown at src/modules/Elsa.Dsl.ElsaScript/Helpers/ActivityActivator.cs:19
using Elsa.Workflows;
namespace Elsa.Dsl.ElsaScript.Helpers;
internal static class ActivityActivator
{
public static IActivity Create(Type type)
{
// Try parameterless ctor first (fast path)
var parameterless = type.GetConstructor(Type.EmptyTypes);
if (parameterless != null)
return (IActivity)parameterless.Invoke([]);
// Fall back: pick a public ctor and provide default values for args
var ctor = type
.GetConstructors()
.OrderBy(c => c.GetParameters().Length) // shortest first
.FirstOrDefault()
?? throw new InvalidOperationException(
$"Type {type} has no public constructors and cannot be instantiated.");
var parameters = ctor.GetParameters();
var args = new object?[parameters.Length];
for (var i = 0; i < parameters.Length; i++)
{
var p = parameters[i];
if (p.HasDefaultValue)
{
args[i] = p.DefaultValue;
}
else
{
args[i] = p.ParameterType.IsValueType
? Activator.CreateInstance(p.ParameterType)
: null;View on GitHub (pinned to fe9217bdfa)