reactiveui/refit · error · NotSupportedException

This Refit client was created with the generated-only API, b

Error message

This Refit client was created with the generated-only API, but the generated client needs the reflection request builder for '{methodName}'. Enable generated request building for this method or use RestService.For when reflection is acceptable.

What it means

Thrown by GeneratedOnlyRequestBuilder.BuildRestResultFuncForMethod — a request builder used when a Refit client is created in generated-only mode (no reflection). It throws unconditionally because that mode forbids building request delegates via reflection; the method it was asked to build ('{methodName}') was not covered by the generated client, so Refit cannot fulfill the call without falling back to reflection.

Source

Thrown at src/Refit/GeneratedOnlyRequestBuilder.cs:28

{
    /// <summary>Initializes a new instance of the <see cref="GeneratedOnlyRequestBuilder"/> class.</summary>
    /// <param name="settings">The settings used by the generated client.</param>
    internal GeneratedOnlyRequestBuilder(RefitSettings settings) => Settings = settings;

    /// <inheritdoc/>
    public RefitSettings Settings { get; }

    /// <inheritdoc/>
    [RequiresUnreferencedCode("Building request delegates from reflected method metadata requires generic method metadata to be available at runtime.")]
    [RequiresDynamicCode("Building request delegates from reflected method metadata requires runtime generic method instantiation.")]
    public Func<HttpClient, object[], object?> BuildRestResultFuncForMethod(
        string methodName,
        Type[]? parameterTypes = null,
        Type[]? genericArgumentTypes = null)
    {
        var methodContext =
            $"This Refit client was created with the generated-only API, but the generated client needs the reflection request builder for '{methodName}'.";
        throw new NotSupportedException(
            string.Concat(
                methodContext,
                " Enable generated request building for this method or use RestService.For when reflection is acceptable."));
    }
}

View on GitHub (pinned to b455f65ecc)

Solutions

  1. Ensure the source generator ran for the interface (rebuild; verify the generated partial client exists and includes the method).
  2. Use the reflection-capable entry point RestService.For<T> if runtime reflection is acceptable (not AOT).
  3. Confirm the method signature is one the generator supports (e.g. return type, attributes) and that the interface is marked/visible to the generator.

Example fix

// before — generated-only client, method not generated
var client = RestService.ForGenerated<IMyApi>(baseUrl);
await client.NewMethodAsync(); // throws NotSupportedException

// after — either let the generator produce it (rebuild/fix signature),
// or use the reflection entry point:
var client = RestService.For<IMyApi>(baseUrl);
await client.NewMethodAsync();
Defensive patterns

Strategy: validation

Validate before calling

// Verify the generated client covers the method before calling it in generated-only mode.
// At build time, ensure the source generator produced a partial client for IMyApi.
// At runtime, prefer the reflection entry point if the method isn't generated:
IMyApi client = RuntimeFeature.IsDynamicCodeSupported
    ? RestService.For<IMyApi>(baseUrl)        // reflection OK
    : RestService.ForGenerated<IMyApi>(baseUrl); // generated-only (AOT)

Try / catch

try { await client.MethodAsync(); }
catch (NotSupportedException ex) when (ex.Message.Contains("generated-only API"))
{
    // Method wasn't generated — fall back to RestService.For<T> (reflection) or regenerate the client.
    var reflectionClient = RestService.For<IMyApi>(baseUrl);
    await reflectionClient.MethodAsync();
}

Prevention

When it happens

Trigger: A Refit interface method that is not implemented by the source generator is invoked on a client built via the generated-only entry point (e.g. RestService.ForGenerated<T> / AddRefitClient with generated-only). The runtime tries to reflect-build the method and hits this NotSupportedException. Common when a method lacks partial/generated coverage or the generator didn't run for that interface.

Common situations: Adding a new method to a Refit interface but the source generator didn't regenerate (build/stale issue); using a generic/interface combination the generator doesn't handle; enabling generated-only mode while still relying on reflection-only features; trimming/AOT scenarios where the generated partial is absent.

Related errors


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