reactiveui/refit · critical · InvalidOperationException

Sequence contains no matching element

Error message

Sequence contains no matching element

What it means

The companion no-match guard of the ForType reflection lookup: zero single-parameter generic methods were found on RequestBuilder. As with error 0, the comments flag this as unreachable in a matched build, so seeing it means the loaded Refit core assembly is a different version whose public RequestBuilder.ForType signature the HttpClientFactory code does not recognize.

Source

Thrown at src/Refit.HttpClientFactory/HttpClientFactoryCore.cs:388

        var methods = typeof(RequestBuilder).GetMethods(BindingFlags.Public | BindingFlags.Static);
        MethodInfo? match = null;
        for (var i = 0; i < methods.Length; i++)
        {
            var method = methods[i];
            if (!method.IsGenericMethodDefinition || method.GetParameters().Length != 1)
            {
                continue;
            }

            if (match is not null)
            {
                throw new InvalidOperationException("Sequence contains more than one matching element");
            }

            match = method;
        }

        return match ?? throw new InvalidOperationException("Sequence contains no matching element");
    }

    /// <summary>Configures the primary and authorization handlers for a keyed Refit client from its settings.</summary>
    /// <param name="builder">The HTTP client builder to configure.</param>
    /// <param name="settingsResolver">Resolves the settings holder for the keyed client from a service provider.</param>
    private static void ConfigureKeyedHandlers(
        IHttpClientBuilder builder,
        Func<IServiceProvider, ISettingsFor> settingsResolver)
    {
        _ = builder.ConfigurePrimaryHttpMessageHandler(serviceProvider =>
            settingsResolver(serviceProvider).Settings?.HttpMessageHandlerFactory?.Invoke() ?? new HttpClientHandler());

        _ = builder.ConfigureAdditionalHttpMessageHandlers((handlers, serviceProvider) =>
        {
            if (settingsResolver(serviceProvider).Settings?.AuthorizationHeaderValueGetter is not { } getToken)
            {
                return;
            }

View on GitHub (pinned to b455f65ecc)

Solutions

  1. Set every Refit.* package to one consistent version and restore.
  2. Inspect the loaded assembly with 'dotnet list package --include-transitive' or Assembly.Load events to confirm which Refit.dll is actually resolved.
  3. Do a clean build (rm -rf bin obj + dotnet restore) to eliminate a stale cached assembly.
  4. Use Directory.Packages.props to enforce a single Refit version across the solution.

Example fix

// before
//   <PackageReference Include="Refit.HttpClientFactory" Version="8.0.0" />
//   <PackageReference Include="Refit" Version="6.3.0" />  // transitive, too old
// after
//   <PackageReference Include="Refit.HttpClientFactory" Version="8.0.0" />
//   <PackageReference Include="Refit" Version="8.0.0" />
Defensive patterns

Strategy: validation

Validate before calling

var found = typeof(Refit.RequestBuilder)
    .GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)
    .Any(m => m.IsGenericMethodDefinition && m.GetParameters().Length == 1);
if (!found)
    throw new InvalidOperationException("Loaded Refit core is missing the expected ForType method; align Refit.* package versions.");

Try / catch

try { services.AddRefitClient<IMyApi>(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("no matching element"))
{
    throw new InvalidOperationException("Refit version mismatch: upgrade/pin the Refit core package to match Refit.HttpClientFactory.", ex);
}

Prevention

When it happens

Trigger: FindRequestBuilderGenericForTypeMethod() iterates RequestBuilder.GetMethods() and finds no public static open-generic method with exactly one parameter. Occurs when the core Refit version is older (or newer) than the HttpClientFactory integration expects, e.g. core 6.x loaded while the factory integration is 8.x.

Common situations: Downgrading only the Refit core package; a transitive dependency pins an old Refit; Refit.HttpClientFactory upgraded without upgrading Refit; mismatched Refit source-link/debug build loaded.

Related errors


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