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

[BUG] The 'InnerHandler' property must be null. #1403

Closed
jchannon opened this issue Aug 17, 2022 · 7 comments · Fixed by #1712
Closed

[BUG] The 'InnerHandler' property must be null. #1403

jchannon opened this issue Aug 17, 2022 · 7 comments · Fixed by #1712
Labels

Comments

@jchannon
Copy link

jchannon commented Aug 17, 2022

Using the code defined in the README https://github.com/reactiveui/refit#reducing-header-boilerplate-with-delegatinghandlers-authorization-headers-worked-example, running the app results in an exception:

Unexpected application termination! "The 'InnerHandler' property must be null. 'DelegatingHandler' instances provided to 'HttpMessageHandlerBuilder' must not be reused or cached.\nHandler: 'Easee.Connector.AuthHeaderHandler'"
System.InvalidOperationException: The 'InnerHandler' property must be null. 'DelegatingHandler' instances provided to 'HttpMessageHandlerBuilder' must not be reused or cached.
Handler: 'Easee.Connector.AuthHeaderHandler'
   at HttpMessageHandler Microsoft.Extensions.Http.HttpMessageHandlerBuilder.CreateHandlerPipeline(HttpMessageHandler primaryHandler, IEnumerable<DelegatingHandler> additionalHandlers)
   at HttpMessageHandler Microsoft.Extensions.Http.DefaultHttpMessageHandlerBuilder.Build()
   at ActiveHandlerTrackingEntry Microsoft.Extensions.Http.DefaultHttpClientFactory.CreateHandlerEntry(string name)
   at ActiveHandlerTrackingEntry Microsoft.Extensions.Http.DefaultHttpClientFactory+<>c__DisplayClass14_0.<.ctor>b__1(?)+() => { }
   at void System.Lazy<T>.ViaFactory(LazyThreadSafetyMode mode)
   at void System.Lazy<T>.ExecutionAndPublication(LazyHelper executionAndPublication, bool useDefaultConstructor)
   at T System.Lazy<T>.CreateValue()
   at T System.Lazy<T>.get_Value()
   at HttpMessageHandler Microsoft.Extensions.Http.DefaultHttpClientFactory.CreateHandler(string name)
   at HttpClient Microsoft.Extensions.Http.DefaultHttpClientFactory.CreateClient(string name)

Steps To Reproduce

Expected behavior
My app to run.

I believe you shouldn't set the InnerHandler

Environment

  • OS: macOS 12.5
  • .NET Core: 6
@jchannon jchannon added the bug label Aug 17, 2022
@MarkEnverus
Copy link

MarkEnverus commented Oct 10, 2022

This still seems to be an issue - when using RestService.For - the code works, but when using Dependency Injection (HttpClientFactory) the code fails.
I was able to work around it using => AuthorizationHeaderValueGetter.

@Ninja2
Copy link

Ninja2 commented Oct 31, 2022

As @jchannon said, the inner handler should not be set when using from DI or it will fail when building the handlers pipeline

@MarkEnverus
Copy link

@Ninja2 should we update the docs to reflect this? It's confusing and then you need separate setups for either RestService.For and DI?

@noontz
Copy link

noontz commented Apr 6, 2023

I can confirm this issue when implementing on Azure Functions.

  • Assigning the InnerHander throws an exception with DI
  • NOT assigning the InnerHandler throws an exception with RestService.For

In case others end here as me ( and the issue isn't closed ;) )

My current workaround is to add a flag in the DelegatingHandler constructor e.g.

      public RequestInterceptor(ILogger log, bool useDependencyInjection = true)
      {
        Logger = log;

        if(useDependencyInjection)
            return;

        var handler = new HttpClientHandler
        {
            // what ever
        };

        InnerHandler = handler;
     }

Hence for DI (Azure Functions):

public class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
        var logger = builder.Services
            .AddRefitClient<Iapi>()
            .ConfigureHttpClient(httpClient => httpClient.BaseAddress = new Uri(<url>))
            .AddHttpMessageHandler(serviceProvider => new RequestInterceptor(serviceProvider.GetService<ILogger<RequestInterceptor>>()));
    }
}

For RestService.For (Testing):

RestService.For<Iapi>(new HttpClient(new RequestInterceptor(new Mock<ILogger>().Object, false)) { BaseAddress = new Uri(<url>) });

bbrandt added a commit to bbrandt/refit that referenced this issue Sep 27, 2023
glennawatson added a commit that referenced this issue Oct 1, 2023
* Addressing #1403

Co-authored-by: Glenn <5834289+glennawatson@users.noreply.github.com>
@salghazaly
Copy link

salghazaly commented Nov 23, 2023

I m facing the same issue in a MAUI Blazor App .Net 7.0
in my app there are two messages handlers one for SSL Pinning as follow


public class HttpsValidation : HttpClientHandler
{
public HttpsValidation()
{
    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate | DecompressionMethods.Brotli;
    UseProxy = true;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
   
    ServerCertificateCustomValidationCallback = OnValidateCertificate;
}
}

and a DelegatingHandler message handler for httpclient request as follow

public class CustomHttpRequestHandler : DelegatingHandler
{

}

I m injecting httpsclient as follow

builder.Services.AddHttpClient<HttpClient>(
    "MyhttpClient",
    client => client.BaseAddress = new Uri(BaseUrl)
    ).ConfigureHttpMessageHandlerBuilder(builder => builder.PrimaryHandler = new HttpClientHandler()
    {
        UseDefaultCredentials = true
    }
    ).ConfigurePrimaryHttpMessageHandler(() => new HttpsValidation())
    .AddHttpMessageHandler<CustomHttpRequestHandler>();

Really the app is working fine but sometimes this issue appears suddenly even sometimes when the app is idle for a while I get this error as follow

blazor.webview.js:1 The 'InnerHandler' property must be null. 'DelegatingHandler' instances provided to 'HttpMessageHandlerBuilder' must not be reused or cached.
Handler: 'CustomHttpRequestHandler '

@rsr-maersk
Copy link

any status update here?

@ChrisPulman ChrisPulman mentioned this issue Jun 9, 2024
2 tasks
Copy link

github-actions bot commented Jul 5, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants