question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HttpChannelFactory different behavior under .net Framework and .net Core

See original GitHub issue

Describe 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:closed
  • Created 3 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mconnewcommented, Mar 3, 2021

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.

0reactions
mconnewcommented, Aug 9, 2023

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].

Read more comments on GitHub >

github_iconTop Results From Across the Web

Breaking changes - .NET Framework to .NET Core
NET Framework to .NET Core versions 1.0 through 3.1, the breaking changes listed in this article may affect you. Breaking changes are ...
Read more >
How to know if I running under .NET Framework or .NET Core
So, I see you not fully understand the differece of .net standart, .net core and .net framework. So, there are only 2 types...
Read more >
Behavior changes when comparing strings on .NET 5+
Learn about string-comparison behavior changes in .NET 5 and later versions on Windows.
Read more >
NET Core vs .NET Framework - What's The Difference?
NET Core focuses to develop apps in a variety of domains like gaming, mobile, IoT, AI, etc. .NET Framework is limited to Windows...
Read more >
Differences Between .NET Framework, .NET Core, and . ...
The .NET Framework and .NET Core are implementions of .NET whereas .NET Standard is not an implmention rather it's a specification.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found