HttpChannelFactory different behavior under .net Framework and .net Core
See original GitHub issueDescribe the bug i have a WCF client in a library (TargetFrameworks: netstandard2.0;netstandard2.1;net5.0). This library is used by different application. Now it turns out that the client does not work under .net 4.7.2. I could break it down to the fact that the DelegatingHandler is not called. To be more precise it looks like the func is not executed because it never arrives at the constructor of the DelegatingHandler.
But if I call the client from a .net Core 2.0, .net Core 3.1 or .net 5 application everything works as expected. So I guess that this code part is handled differently under .net Framework than under .net Core.
To Reproduce
And here is some sample code. It is very generic. But exactly on these lines you can break down the problem.
public class HttpMessageHandlerBehavior : IEndpointBehavior
{
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
bindingParameters.Add(new Func<HttpClientHandler, HttpMessageHandler>(x => new TestHandler(x)));
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { }
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { }
public void Validate(ServiceEndpoint endpoint) { }
}
public class TestHandler : DelegatingHandler
{
public TestHandler(HttpMessageHandler innerHandler) : base(innerHandler)
{ }
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
System.Diagnostics.Debug.WriteLine("Do Stuff here!");
}
}
Expected behavior I hoped that the code would work the same under all frameworks.
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (4 by maintainers)
The mechanism of providing a Func to modify the behavior of the HttpClientHandler was originally created because the HttpClientHandler is encapsulated by WCF and on Core there aren’t any public methods to modify it’s behavior. On .NET Framework, HttpClient isn’t used, instead we use HttpWebRequest. On .NET Framework you can modify quite a bit of behavior of HttpWebRequest using ServicePointManager, but as this isn’t available on Core, I created the Func binding parameter mechanism.
Can you give a high level overview of what it is you are doing in your handler? There might be an alternative way to do it on .NET Framework. I can’t port the Func mechanism to .NET Framework as it doesn’t use HttpClientHandler. Core was a significant rewrite to use HttpClient instead.
Sorry I forgot to link to it here when I made my method to hijack HttpWebRequests in .NET Framework available. You can find it here: https://github.com/mconnew/Playground/tree/main/HijackHttpWebRequest
I will stress this isn’t an official supported library and is just something I personally worked out and decided to make available as it’s interesting.
I’m going to close the issue as with my code sample that should help solve your problem in a similar way as you do on .NET [Core].