OrchardCMS/OrchardCore · error · InvalidOperationException

Switch " " was not found

Error message

Switch "{0}" was not found

What it means

Command handlers in OrchardCore map command-line switches to public properties decorated with [OrchardSwitch]. SetSwitchValue uses reflection to find the property named by the switch; if no such public instance property exists on the handler, it throws. This keeps command invocations strict against typo'd or unsupported switches.

Solutions

  1. Correct the switch name on the command line to one defined on the target command handler.
  2. Check the handler class for public properties decorated with [OrchardSwitch] and use one of those names.
  3. Run the command's help to list supported switches.
  4. If you own the command, add the missing property plus [OrchardSwitch] attribute to the handler.

Example fix

// before
public class MyCommand { /* no property for switch 'verbose' */ }
// after
public class MyCommand {
  [OrchardSwitch]
  public bool Verbose { get; set; }
}
Defensive patterns

Strategy: validation

Validate before calling

var prop = handlerType.GetProperty(switchName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
if (prop is null) throw new InvalidOperationException($"Unknown switch '{switchName}' for {handlerType.Name}");

Try / catch

try { await handler.ExecuteAsync(context); }
catch (InvalidOperationException ex) { logger.LogError(ex, "Invalid switch '{Switch}' for command {Command}", switchName, commandName); }

Prevention

When it happens

Trigger: Invoking a command like `help -foo bar` (via SetSwitchValues) where -foo does not correspond to any public property on the command handler class, or with wrong casing despite IgnoreCase binding.

Common situations: Typing a switch that belongs to a different command, running an outdated command from docs after a rename, or expecting a switch that the handler never defined.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13). Data as JSON: /api/errors/e9378bfa0422fc1a. Report an issue: GitHub.

Appendix: source

Thrown at src/OrchardCore/OrchardCore.Infrastructure.Abstractions/Commands/DefaultCommandHandler.cs:40

    }

    private void SetSwitchValues(CommandContext context)
    {
        if (context.Switches != null && context.Switches.Count > 0)
        {
            foreach (var commandSwitch in context.Switches)
            {
                SetSwitchValue(commandSwitch);
            }
        }
    }

    private void SetSwitchValue(KeyValuePair<string, string> commandSwitch)
    {
        // Find the property.
        var propertyInfo = GetType()
            .GetProperty(commandSwitch.Key, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase)
            ?? throw new InvalidOperationException(S["Switch \"{0}\" was not found", commandSwitch.Key]);

        if (propertyInfo.GetCustomAttributes(typeof(OrchardSwitchAttribute), false).Length == 0)
        {
            throw new InvalidOperationException(S["A property \"{0}\" exists but is not decorated with \"{1}\"", commandSwitch.Key, nameof(OrchardSwitchAttribute)]);
        }

        // Set the value.
        try
        {
            var value = ConvertToType(propertyInfo.PropertyType, commandSwitch.Value);
            propertyInfo.SetValue(this, value, null /*index*/);
        }
        catch (Exception ex) when (!ex.IsFatal())
        {
            // TODO: (ngm) fix this message.
            var message = S["Error converting value \"{0}\" to \"{1}\" for switch \"{2}\"",
                commandSwitch.Value,
                propertyInfo.PropertyType.FullName,

View on GitHub (pinned to 4306c0717f)