reactiveui/refit · critical · InvalidOperationException

Sequence contains more than one matching element

Error message

Sequence contains more than one matching element

What it means

Refit.HttpClientFactory reflects over the static methods of Refit's RequestBuilder type to locate the single open-generic 'ForType' method that takes exactly one parameter. This guard fires when two such methods are present, which the code comments mark as unreachable in a matched build. In practice it means the loaded Refit core assembly has an API surface that no longer matches the HttpClientFactory integration version you compiled against.

Source

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

    /// <returns>The matching method definition.</returns>
    /// <exception cref="InvalidOperationException"><see cref="RequestBuilder"/> declares no single-parameter generic <c>ForType</c> method, or more than one.</exception>
    [RequiresUnreferencedCode("Resolving RequestBuilder.ForType by reflection requires method metadata to be available at runtime.")]
    [ExcludeFromCodeCoverage] // RequestBuilder declares exactly one single-parameter generic ForType, so the duplicate-match and no-match guards are unreachable.
    private static MethodInfo FindRequestBuilderGenericForTypeMethod()
    {
        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());

View on GitHub (pinned to b455f65ecc)

Solutions

  1. Align all Refit.* package versions to the same release (Refit, Refit.HttpClientFactory, Refit.Newtonsoft.Json, Refit.SystemTextJson) in every project.
  2. Run 'dotnet list package --include-transitive' (or the NuGet resolver) to find the conflict and pin/redirect the core Refit version.
  3. Clear bin/obj and the NuGet cache, then do a clean restore + rebuild so no stale Refit.dll remains.
  4. Add a central package version management (Directory.Packages.props) so Refit cannot drift across projects.

Example fix

// before (csproj, drifting versions)
//   <PackageReference Include="Refit" Version="7.2.17" />
//   <PackageReference Include="Refit.HttpClientFactory" Version="8.0.0" />
// after
//   <PackageReference Include="Refit" Version="8.0.0" />
//   <PackageReference Include="Refit.HttpClientFactory" Version="8.0.0" />
Defensive patterns

Strategy: validation

Validate before calling

// Fail fast at startup if the two assemblies disagree.
var rbMethods = typeof(Refit.RequestBuilder)
    .GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)
    .Where(m => m.IsGenericMethodDefinition && m.GetParameters().Length == 1)
    .ToList();
if (rbMethods.Count != 1)
    throw new InvalidOperationException($"Refit/Refit.HttpClientFactory version mismatch: found {rbMethods.Count} ForType candidates.");

Try / catch

try { services.AddRefitClient<IMyApi>(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("more than one matching element"))
{
    // Log and surface a clear 'align Refit package versions' error to the operator.
    throw new InvalidOperationException("Refit version mismatch: align all Refit.* packages.", ex);
}

Prevention

When it happens

Trigger: AddRefitClient<T>() / GetRequestBuilderGenericForTypeMethod() runs and finds two public static generic methods on RequestBuilder with one parameter. Happens when two different Refit versions are loaded side by side (e.g. Refit 7.x core pulled in transitively while Refit.HttpClientFactory targets 8.x).

Common situations: Mismatched Refit.HttpClientFactory and Refit NuGet package versions; a transitive dependency forces an older/newer Refit core; upgrade that touched only some packages; assembly load context loading a stray Refit.dll.

Related errors


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