elsa-workflows/elsa-core · error · ArgumentException

Invalid target type.

Error message

Invalid target type.

What it means

ObjectConverter.ConvertAnyDateType throws ArgumentException('Invalid target type.') when called with a target type other than DateTime or DateTimeOffset. This is an internal contract violation rather than a bad input value.

Solutions

  1. Use ConvertTo<DateTime> or ConvertTo<DateTimeOffset> only
  2. Check library version for routing fixes; upgrade Elsa.Api.Client
  3. If custom types are needed, convert manually before calling

Example fix

// before
var d = ObjectConverter.ConvertTo<DateOnly>(value);
// after
var d = DateOnly.FromDateTime(ObjectConverter.ConvertTo<DateTime>(value));
Defensive patterns

Strategy: validation

Validate before calling

if (typeof(T) is not (DateTime or DateTimeOffset)) throw new NotSupportedException($"{typeof(T)} is not a date type");

Try / catch

try { d = ObjectConverter.ConvertTo<DateTime>(v); } catch (ArgumentException) { /* unsupported target */ }

Prevention

When it happens

Trigger: ConvertTo<T> where T's underlying type is neither DateTime nor DateTimeOffset but the code path routed into the date conversion helper (e.g. Nullable<DateTime>-adjacent types handled upstream failing to strip, or new date-like types added without updating the switch).

Common situations: Version upgrades where a caller relies on date-like conversion for a new type; library-internal routing bugs.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of elsa-workflows/elsa-core@fe9217bdfa (2026-09-13). Data as JSON: /api/errors/a1cd643d3f999555. Report an issue: GitHub.

Appendix: source

Thrown at src/clients/Elsa.Api.Client/Extensions/ObjectConverter.cs:226

    /// <returns>The converted value.</returns>
    /// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is not of type <see cref="DateTime"/> or <see cref="DateTimeOffset"/>.</exception>
    private static object ConvertAnyDateType(object value, Type targetType)
    {
        return targetType switch
        {
            { } t when t == typeof(DateTime) => value switch
            {
                DateTime dateTime => dateTime,
                DateTimeOffset dateTimeOffset => dateTimeOffset.DateTime,
                _ => throw new ArgumentException("Invalid value type.")
            },
            { } t when t == typeof(DateTimeOffset) => value switch
            {
                DateTime dateTime => new DateTimeOffset(dateTime),
                DateTimeOffset dateTimeOffset => dateTimeOffset,
                _ => throw new ArgumentException("Invalid value type.")
            },
            _ => throw new ArgumentException("Invalid target type.")
        };
    }
}

View on GitHub (pinned to fe9217bdfa)