reactiveui/refit · error · ArgumentException

Method must be defined and have an HTTP Method attribute

Error message

Method must be defined and have an HTTP Method attribute

What it means

FindMatchingRestMethodInfo looks up a method name key in the dictionary of HTTP-attributed methods. The dictionary is populated only from interface methods carrying an HTTP method attribute (Get/Post/etc.), so a missing key means the name does not exist on the interface or was never given an HTTP attribute. This is raised during method resolution, typically at the first call to that member.

Source

Thrown at src/Refit.Reflection/RequestBuilderImplementation.cs:374

    /// <summary>Finds the rest method matching the given name, parameter types and generic arguments.</summary>
    /// <param name="key">The method lookup key.</param>
    /// <param name="parameterTypes">The parameter types to match, or null to match a single overload.</param>
    /// <param name="genericArgumentTypes">The generic argument types to close over, or null.</param>
    /// <returns>The matching rest method info.</returns>
    /// <exception cref="ArgumentException">No method matching <paramref name="key"/> carries an HTTP method attribute,
    /// or the name is overloaded and <paramref name="parameterTypes"/> was not supplied to disambiguate it.</exception>
    /// <exception cref="InvalidOperationException">None of the overloads accept <paramref name="parameterTypes"/> once closed over <paramref name="genericArgumentTypes"/>.</exception>
    [RequiresUnreferencedCode("Resolving generic Refit methods from reflected metadata requires generic method metadata to be available at runtime.")]
    [RequiresDynamicCode("Resolving generic Refit methods from reflected metadata requires runtime generic method instantiation.")]
    internal RestMethodInfoInternal FindMatchingRestMethodInfo(
        string key,
        Type[]? parameterTypes,
        Type[]? genericArgumentTypes)
    {
        if (!_interfaceHttpMethods.TryGetValue(key, out var httpMethods))
        {
            throw new ArgumentException(
                "Method must be defined and have an HTTP Method attribute");
        }

        if (parameterTypes is null)
        {
            if (httpMethods.Count > 1)
            {
                throw new ArgumentException(
                    $"MethodName exists more than once, '{nameof(parameterTypes)}' mut be defined");
            }

            return CloseGenericMethodIfNeeded(httpMethods[0], genericArgumentTypes);
        }

        var possibleMethods = FilterPossibleMethods(httpMethods, parameterTypes, genericArgumentTypes);

        if (possibleMethods.Length == 1)
        {

View on GitHub (pinned to b455f65ecc)

Solutions

  1. Add an HTTP method attribute ([Get]/[Post]/[Put]/[Patch]/[Delete]/[Head]) to the target interface method.
  2. Verify the method name you are calling exactly matches the interface member name.
  3. Remove non-HTTP helper methods from the Refit interface, or move them to a separate service.

Example fix

// before
public interface IApi {
    Task<User> GetUserAsync(string id); // no attribute -> not registered
}

// after
public interface IApi {
    [Get("/users/{id}")] Task<User> GetUserAsync(string id);
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate at startup that every interface method has an HTTP attribute.
static void AssertAllMethodsAttributed<T>() {
    foreach (var m in typeof(T).GetMethods())
        if (m.GetCustomAttribute<Refit.HttpMethodAttribute>() is null)
            throw new InvalidOperationException($"{typeof(T).Name}.{m.Name} has no HTTP method attribute.");
}

Prevention

When it happens

Trigger: Invoking/reflecting a Refit method by a name not present in _interfaceHttpMethods, e.g. a method declared without [Get]/[Post]/... or a typo in a dynamic/keyed call, or calling a method that exists but is not a Refit HTTP method.

Common situations: Forgot the HTTP attribute on an interface method; renamed the method but the caller uses the old name; an inherited non-HTTP helper method is invoked through the proxy; mixed a Refit interface with plain helper methods.

Related errors


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