Skip to content

Fix multiple constructors breaking DI registration for HttpClientRequestAdapter#656

Merged
gavinbarron merged 2 commits into
mainfrom
copilot/fix-httpclientrequestadapter-di
Feb 23, 2026
Merged

Fix multiple constructors breaking DI registration for HttpClientRequestAdapter#656
gavinbarron merged 2 commits into
mainfrom
copilot/fix-httpclientrequestadapter-di

Conversation

Copilot AI commented Feb 23, 2026

Copy link
Copy Markdown
Contributor

v1.21.0 introduced a second public constructor (5-parameter, for runtime binary backwards compatibility with the new httpVersion parameter), causing ActivatorUtilities-based DI to throw InvalidOperationException: Multiple constructors accepting all given argument types have been found.

Changes

  • HttpClientRequestAdapter.cs — Adds [ActivatorUtilitiesConstructor] to the preferred 6-parameter constructor to disambiguate for DI
  • Microsoft.Kiota.Http.HttpClientLibrary.csproj — Adds Microsoft.Extensions.DependencyInjection.Abstractions package reference ([6.0,)) to supply the attribute

Example

// Now works without InvalidOperationException
builder.Services.AddSingleton<IAuthenticationProvider, MyAuthProvider>();
builder.Services.AddHttpClient<IRequestAdapter, HttpClientRequestAdapter>();

var adapter = app.Services.GetRequiredService<IRequestAdapter>(); // no longer throws
Original prompt

This section details on the original issue you should resolve

<issue_title>Multiple constructors for HttpClientRequestAdapter breaks DI</issue_title>
<issue_description>### What are you generating using Kiota, clients or plugins?

API Client/SDK

In what context or format are you using Kiota?

Nuget tool

Client library/SDK language

Csharp

Describe the bug

Starting with 1.21.0 the call builder.Services.AddHttpClient<IRequestAdapter, HttpClientRequestAdapter>() throws following exception:

System.InvalidOperationException: Multiple constructors accepting all given argument types have been found in type 'Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter'. There should only be one applicable constructor.

Expected behavior

There should be no exception, DI should work out of the box like before.

How to reproduce

using Microsoft.Kiota.Abstractions;
using Microsoft.Kiota.Http.HttpClientLibrary;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpClient<IRequestAdapter, HttpClientRequestAdapter>();

var app = builder.Build();

_ = app.Services.GetRequiredService<IRequestAdapter>(); // crash

Open API description file

No response

Kiota Version

1.21.0

Latest Kiota version known to work for scenario above?(Not required)

1.20.1

Known Workarounds

downgrade

Configuration

No response

Debug output

Click to expand log ``` fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] An unhandled exception has occurred while executing the request. System.InvalidOperationException: Multiple constructors accepting all given argument types have been found in type 'Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter'. There should only be one applicable constructor. at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.TryFindMatchingConstructor(Type instanceType, Type[] argumentTypes, ConstructorInfo& matchingConstructor, Nullable`1[]& parameterMap) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.FindApplicableConstructor(Type instanceType, Type[] argumentTypes, ConstructorInfoEx[] constructors, ConstructorInfo& matchingConstructor, Nullable`1[]& matchingParameterMap) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateFactoryInternal(Type instanceType, Type[] argumentTypes, ParameterExpression& provider, ParameterExpression& argumentArray, Expression& factoryExpressionBody) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateFactory(Type instanceType, Type[] argumentTypes) at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory) at Microsoft.Extensions.Http.DefaultTypedHttpClientFactory`1.Cache.get_Activator() at Microsoft.Extensions.Http.DefaultTypedHttpClientFactory`1.CreateClient(HttpClient httpClient) at Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.AddTransientHelper[TClient,TImplementation](IServiceProvider s, IHttpClientBuilder builder) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context) ```

Other information

No response</issue_description>

Comments on the Issue (you are @copilot in this section)

@gavinbarron This should be fixable by adding the ActivatorUtilitiesConstructor attribute to designate the default constructor for use in DI scenarios

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

…ctors issue

Co-authored-by: gavinbarron <7122716+gavinbarron@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix multiple constructors issue in HttpClientRequestAdapter Fix multiple constructors breaking DI registration for HttpClientRequestAdapter Feb 23, 2026
Copilot AI requested a review from gavinbarron February 23, 2026 17:01
@gavinbarron gavinbarron marked this pull request as ready for review February 23, 2026 17:28
@gavinbarron gavinbarron requested a review from a team as a code owner February 23, 2026 17:28
@gavinbarron gavinbarron enabled auto-merge (squash) February 23, 2026 17:28
@sonarqubecloud

Copy link
Copy Markdown

@github-project-automation github-project-automation Bot moved this to In Progress 🚧 in Kiota Feb 23, 2026
@gavinbarron gavinbarron merged commit 21d58b0 into main Feb 23, 2026
14 of 16 checks passed
@gavinbarron gavinbarron deleted the copilot/fix-httpclientrequestadapter-di branch February 23, 2026 22:54
@github-project-automation github-project-automation Bot moved this from In Progress 🚧 to Done ✔️ in Kiota Feb 23, 2026
This was referenced May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Multiple constructors for HttpClientRequestAdapter breaks DI

3 participants