Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a handler causes System.InvalidOperationException: 'The inner handler has not been assigned.' to be thrown on request execution #275

Closed
IanKemp opened this issue Jun 25, 2024 · 4 comments
Labels
Status: No Recent Activity status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close type:question An issue that's a question

Comments

@IanKemp
Copy link

IanKemp commented Jun 25, 2024

The following code throws the aforementioned exception with Microsoft.Kiota.Http.HttpClientLibrary version 1.4.3 (latest as of this writing) when issuing a request against myClient. Am I doing something wrong, or is this a bug?

var kiotaClient = KiotaClientFactory.Create(
    new MyDelegatingHandler());

MyAuthenticationProvider authenticationProvider = new();
HttpClientRequestAdapter requestAdapter = new(
    authenticationProvider: authenticationProvider,
    httpClient: kiotaClient)
{
    BaseUrl = "https://example.com",
};

GeneratedKiotaClient myClient = new(requestAdapter);

Stack:

System.Net.Http.DelegatingHandler.SetOperationStarted()
System.Net.Http.DelegatingHandler.SendAsync(System.Net.Http.HttpRequestMessage, System.Threading.CancellationToken)
MyDelegatingHandler.SendAsync(System.Net.Http.HttpRequestMessage, System.Threading.CancellationToken)
Microsoft.Kiota.Http.HttpClientLibrary.Middleware.HeadersInspectionHandler.SendAsync(System.Net.Http.HttpRequestMessage, System.Threading.CancellationToken)
Microsoft.Kiota.Http.HttpClientLibrary.Middleware.RedirectHandler.SendAsync(System.Net.Http.HttpRequestMessage, System.Threading.CancellationToken)
Microsoft.Kiota.Http.HttpClientLibrary.Middleware.RetryHandler.SendAsync(System.Net.Http.HttpRequestMessage, System.Threading.CancellationToken)
Microsoft.Kiota.Http.HttpClientLibrary.Middleware.UriReplacementHandler<TUriReplacementHandlerOption>.SendAsync(System.Net.Http.HttpRequestMessage, System.Threading.CancellationToken)
System.Net.Http.HttpClient.SendAsync.__Core|83_0(System.Net.Http.HttpRequestMessage, System.Net.Http.HttpCompletionOption, System.Threading.CancellationTokenSource, bool, System.Threading.CancellationTokenSource, System.Threading.CancellationToken)
Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.GetHttpResponseMessage(Microsoft.Kiota.Abstractions.RequestInformation, System.Threading.CancellationToken, System.Diagnostics.Activity, string, bool)
Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.SendAsync<ModelType>(Microsoft.Kiota.Abstractions.RequestInformation, Microsoft.Kiota.Abstractions.Serialization.ParsableFactory<ModelType>, System.Collections.Generic.Dictionary<string, Microsoft.Kiota.Abstractions.Serialization.ParsableFactory<Microsoft.Kiota.Abstractions.Serialization.IParsable>>, System.Threading.CancellationToken)

I do not receive the same exception if I follow the example directly.

@IanKemp
Copy link
Author

IanKemp commented Jun 25, 2024

What works is using the other overload of Create:

var handlers = KiotaClientFactory.CreateDefaultHandlers();
handlers.Add(new MyDelegatingHandler());

var kiotaClient = KiotaClientFactory.Create(handlers);

// all other code same as first comment
MyAuthenticationProvider ...

The issue is that Create(HttpMessageHandler?, IRequestOption[]?) is IMO a bit of a footgun: it doesn't automagically add GetDefaultHttpMessageHandler() as the InnerHandler of the passed-in handler. Yes, the method does explicitly say that the parameter is the final handler, but I made the assumption (which I don't think is unwarranted) that if I wanted to do nothing more than add a single handler into the chain, I should probably be calling the method that takes a single handler. Perhaps a CreateWith(params HttpMessageHandler[] handlers) method that inserts the provided handlers between the default ones and the final GetDefaultHttpMessageHandler() one?

I also think that the linked example should be updated to the code given in this comment, as it's both more concise and IMO self-explanatory.

@svrooij
Copy link
Contributor

svrooij commented Jun 26, 2024

This seems related to microsoft/kiota-http-dotnet#268 ?

And since you're trying to do authentication, why not make a factory that creates the right IAuthenticationProvider. The authentication provider interface has one method to implement which is: Task AuthenticateRequestAsync(...), has access to all the request info and can attach additional headers.

@baywet baywet added status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close type:question An issue that's a question labels Jun 26, 2024
@andrueastman
Copy link
Member

Transferring issue as part of #238

@andrueastman andrueastman transferred this issue from microsoft/kiota-http-dotnet Jul 9, 2024

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: No Recent Activity status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close type:question An issue that's a question
Projects
Archived in project
Development

No branches or pull requests

4 participants