{"record":{"id":"b48fb637d22c3a0e","repo":"reactiveui/refit","slug":"sequence-contains-more-than-one-matching-element","errorCode":null,"errorMessage":"Sequence contains more than one matching element","messagePattern":"Sequence contains more than one matching element","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/Refit.HttpClientFactory/HttpClientFactoryCore.cs","lineNumber":382,"sourceCode":"    /// <returns>The matching method definition.</returns>\n    /// <exception cref=\"InvalidOperationException\"><see cref=\"RequestBuilder\"/> declares no single-parameter generic <c>ForType</c> method, or more than one.</exception>\n    [RequiresUnreferencedCode(\"Resolving RequestBuilder.ForType by reflection requires method metadata to be available at runtime.\")]\n    [ExcludeFromCodeCoverage] // RequestBuilder declares exactly one single-parameter generic ForType, so the duplicate-match and no-match guards are unreachable.\n    private static MethodInfo FindRequestBuilderGenericForTypeMethod()\n    {\n        var methods = typeof(RequestBuilder).GetMethods(BindingFlags.Public | BindingFlags.Static);\n        MethodInfo? match = null;\n        for (var i = 0; i < methods.Length; i++)\n        {\n            var method = methods[i];\n            if (!method.IsGenericMethodDefinition || method.GetParameters().Length != 1)\n            {\n                continue;\n            }\n\n            if (match is not null)\n            {\n                throw new InvalidOperationException(\"Sequence contains more than one matching element\");\n            }\n\n            match = method;\n        }\n\n        return match ?? throw new InvalidOperationException(\"Sequence contains no matching element\");\n    }\n\n    /// <summary>Configures the primary and authorization handlers for a keyed Refit client from its settings.</summary>\n    /// <param name=\"builder\">The HTTP client builder to configure.</param>\n    /// <param name=\"settingsResolver\">Resolves the settings holder for the keyed client from a service provider.</param>\n    private static void ConfigureKeyedHandlers(\n        IHttpClientBuilder builder,\n        Func<IServiceProvider, ISettingsFor> settingsResolver)\n    {\n        _ = builder.ConfigurePrimaryHttpMessageHandler(serviceProvider =>\n            settingsResolver(serviceProvider).Settings?.HttpMessageHandlerFactory?.Invoke() ?? new HttpClientHandler());\n","sourceCodeStart":364,"sourceCodeEnd":400,"githubUrl":"https://github.com/reactiveui/refit/blob/b455f65ecc4c97d092317e349cb775f9cfc6bcdf/src/Refit.HttpClientFactory/HttpClientFactoryCore.cs#L364-L400","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Align all Refit.* package versions to the same release (Refit, Refit.HttpClientFactory, Refit.Newtonsoft.Json, Refit.SystemTextJson) in every project.","Run 'dotnet list package --include-transitive' (or the NuGet resolver) to find the conflict and pin/redirect the core Refit version.","Clear bin/obj and the NuGet cache, then do a clean restore + rebuild so no stale Refit.dll remains.","Add a central package version management (Directory.Packages.props) so Refit cannot drift across projects."],"exampleFix":"// before (csproj, drifting versions)\n//   <PackageReference Include=\"Refit\" Version=\"7.2.17\" />\n//   <PackageReference Include=\"Refit.HttpClientFactory\" Version=\"8.0.0\" />\n// after\n//   <PackageReference Include=\"Refit\" Version=\"8.0.0\" />\n//   <PackageReference Include=\"Refit.HttpClientFactory\" Version=\"8.0.0\" />","handlingStrategy":"validation","validationCode":"// Fail fast at startup if the two assemblies disagree.\nvar rbMethods = typeof(Refit.RequestBuilder)\n    .GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)\n    .Where(m => m.IsGenericMethodDefinition && m.GetParameters().Length == 1)\n    .ToList();\nif (rbMethods.Count != 1)\n    throw new InvalidOperationException($\"Refit/Refit.HttpClientFactory version mismatch: found {rbMethods.Count} ForType candidates.\");","typeGuard":null,"tryCatchPattern":"try { services.AddRefitClient<IMyApi>(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"more than one matching element\"))\n{\n    // Log and surface a clear 'align Refit package versions' error to the operator.\n    throw new InvalidOperationException(\"Refit version mismatch: align all Refit.* packages.\", ex);\n}","preventionTips":["Pin every Refit.* package to one version via Directory.Packages.props.","Run 'dotnet list package --include-transitive' in CI to detect Refit drift.","Do clean restores after Refit upgrades to avoid stale assemblies."],"tags":["refit","httpclientfactory","versioning","reflection","configuration"],"backgroundTag":null,"analyzedSha":"b455f65ecc4c97d092317e349cb775f9cfc6bcdf","analyzedAt":"2026-08-13T21:20:57.878Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}