reactiveui/refit · error · InvalidOperationException

Sequence contains no elements

Error message

Sequence contains no elements

What it means

When building the RestMethodInfo for a single interface method, Refit requires a HttpMethodAttribute (Get/Post/Put/Patch/Delete/Head). If the method has none, the GetCustomAttribute call returns null and this 'Sequence contains no elements' InvalidOperationException is thrown. It mirrors error 5 but fires during per-method construction rather than name lookup.

Source

Thrown at src/Refit.Reflection/RestMethodInfoInternal.cs:53

    /// <param name="clientPathPrefix">The shared route prefix declared by the client interface's <see cref="PathPrefixAttribute"/>, or null when none applies.</param>
    /// <exception cref="InvalidOperationException"><paramref name="methodInfo"/> carries no <see cref="HttpMethodAttribute"/>, so it declares no HTTP verb or path.</exception>
    [RequiresUnreferencedCode("Building request metadata from reflected interface methods requires request object property metadata to be available at runtime.")]
    internal RestMethodInfoInternal(
        [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
        Type targetInterface,
        MethodInfo methodInfo,
        RefitSettings? refitSettings = null,
        string? clientPathPrefix = null)
    {
        RefitSettings = refitSettings ?? new RefitSettings();
        ArgumentExceptionHelper.ThrowIfNull(targetInterface);
        ArgumentExceptionHelper.ThrowIfNull(methodInfo);
        Type = targetInterface;
        MethodInfo = methodInfo;
        ClientPathPrefix = clientPathPrefix ?? string.Empty;

        var hma = methodInfo.GetCustomAttribute<HttpMethodAttribute>(true)
                  ?? throw new InvalidOperationException("Sequence contains no elements");

        HttpMethod = hma.Method;
        RelativePath = CombineWithPathPrefix(ClientPathPrefix, hma.Path);

        IsMultipart = methodInfo.GetCustomAttribute<MultipartAttribute>(true) is not null;

        MultipartBoundary = GetMultipartBoundary(methodInfo, IsMultipart);

        VerifyUrlPathIsSane(RelativePath, RefitSettings.UrlResolution);

        HasReturnTypeAdapter = ReturnTypeAdapterResolver.TryResolveResultType(
            methodInfo.ReturnType,
            RefitSettings.ReturnTypeAdapters,
            out var adapterResultType);
        var (returnType, returnResultType, deserializedResultType) =
            DetermineReturnTypeInfo(methodInfo, adapterResultType);
        ReturnType = returnType;
        ReturnResultType = returnResultType;

View on GitHub (pinned to b455f65ecc)

Solutions

  1. Add an HTTP method attribute to every method on the Refit interface.
  2. Move non-HTTP helper members out of the Refit interface into a separate class/interface.

Example fix

// before
public interface IApi {
    Task<List<Item>> GetAllAsync(); // no attribute
}

// after
public interface IApi {
    [Get("/items")] Task<List<Item>> GetAllAsync();
}
Defensive patterns

Strategy: validation

Validate before calling

static void AssertHttpAttributed<T>() {
    foreach (var m in typeof(T).GetMethods())
        if (m.DeclaringType == typeof(T) && m.GetCustomAttribute<HttpMethodAttribute>() is null)
            throw new InvalidOperationException($"{m.Name} is missing an HTTP method attribute.");
}

Prevention

When it happens

Trigger: An interface method passed to RestMethodInfoInternal has no HTTP method attribute, e.g. a helper/property/getter that slipped into a Refit interface, or an inherited member without an attribute.

Common situations: Adding a non-HTTP helper method to a Refit interface; inheriting from a base interface that has plain members; forgetting the verb attribute on a new endpoint.

Related errors


AI-assisted analysis of reactiveui/refit@b455f65ecc (2026-08-13). Data as JSON: /api/errors/aec788faaae7b5b2. Report an issue: GitHub.