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

HttpClient on .net core3.0 and 5.0 with different responses #54807

Closed
jchhh912 opened this issue Jun 28, 2021 · 12 comments
Closed

HttpClient on .net core3.0 and 5.0 with different responses #54807

jchhh912 opened this issue Jun 28, 2021 · 12 comments

Comments

@jchhh912
Copy link

Hi there,
I see a few cases of the same error as mine in Issues, and I thought I should do something to give you more information to make changes to this issue.

Next I used the same code on two computers, using the company network and 4G network to test The following two links

PC [001]

OS Name: Windows, OS Version: 10.0.19042, RID: win10-x64
Macafee Version:
image
Pulse Secure Version:
image

PC[002] =>Restore factory mode

OS Name: Windows, OS Version: 10.0.18362, RID: win10-x64

Link1:https://sfapi-sbox.sf-express.com/std/service

Link2:https://demo.identityserver.io/.well-known/openid-configuration

1.Using the company network

PC [001] link1 Link2
.netcore 3.1 OK OK
.netcore 5.0 Exception OK
Exception:A connection attempt failed because the connected party did not properly respond after a period of time,or established connection failed because connected host has failed to respond.
PC [002] link1 Link2
.netcore 3.1 OK OK
.netcore 5.0 OK OK

2.Using the 4G network

PC [001] link1 Link2
.netcore 3.1 OK OK
.netcore 5.0 Exception OK
PC [002] link1 Link2
.netcore 3.1 OK OK
.netcore 5.0 OK OK
@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Jun 28, 2021
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@jchhh912
Copy link
Author

jchhh912 commented Jun 28, 2021

Testing code:

using System;
using System.Net.Http;

namespace Demo.SDK5._0
{
    class Program
    { 
        static void Main(string[] args)
        {
            var client = new HttpClient();
            //var resp = client.GetAsync("https://sfapi-sbox.sf-express.com/std/service").Result;
            var resp = client.GetAsync("https://demo.identityserver.io/.well-known/openid-configuration").Result;
            Console.WriteLine(resp.Content.ReadAsStringAsync().Result);
            Console.ReadKey();
        }
    }
}

@jchhh912 jchhh912 changed the title .net core 3.1=>5.0 Httpclient Connection failed HttpClient on .net core3.0 and 5.0 with different responses Jun 28, 2021
@ghost
Copy link

ghost commented Jun 28, 2021

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

Hi there,
I see a few cases of the same error as mine in Issues, and I thought I should do something to give you more information to make changes to this issue.

Next I used the same code on two computers, using the company network and 4G network to test The following two links

PC [001]

OS Name: Windows, OS Version: 10.0.19042, RID: win10-x64
Macafee Version:
image
Pulse Secure Version:
image

PC[002] =>Restore factory mode

OS Name: Windows, OS Version: 10.0.18362, RID: win10-x64

Link1:https://sfapi-sbox.sf-express.com/std/service

Link2:https://demo.identityserver.io/.well-known/openid-configuration

1.Using the company network

PC [001] link1 Link2
.netcore 3.1 OK OK
.netcore 5.0 Exception OK
Exception:A connection attempt failed because the connected party did not properly respond after a period of time,or established connection failed because connected host has failed to respond.
PC [002] link1 Link2
.netcore 3.1 OK OK
.netcore 5.0 OK OK

2.Using the 4G network

PC [001] link1 Link2
.netcore 3.1 OK OK
.netcore 5.0 Exception OK
PC [002] link1 Link2
.netcore 3.1 OK OK
.netcore 5.0 OK OK
Author: jchhh912
Assignees: -
Labels:

area-System.Net.Http, untriaged

Milestone: -

@karelz
Copy link
Member

karelz commented Jun 28, 2021

@jchhh912 will you get the same exception if you use packet capture tool like Wireshark?

@karelz
Copy link
Member

karelz commented Jun 29, 2021

Triage: @antonfirsov thinks it may be IPv6 differences -- there is workaround he is going to post here.

@antonfirsov
Copy link
Member

antonfirsov commented Jun 29, 2021

@jchhh912 this might be caused by issues with IPV6 and/or dual-stack socket support in your corpnet or the McAffee proxy. Since .NET 5.0, HttpClient is using dual-stack sockets by default. Can you check if the following workaround helps:

public HttpClient CreateWorkaroundClient()
{
    SocketsHttpHandler handler = new SocketsHttpHandler
    {
        ConnectCallback = IPv4ConnectAsync
    };
    return new HttpClient(handler);

    static async ValueTask<Stream> IPv4ConnectAsync(SocketsHttpConnectionContext context, CancellationToken cancellationToken)
    {
        // By default, we create dual-mode sockets:
        // Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp);

        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        socket.NoDelay = true;

        try
        {
            await socket.ConnectAsync(context.DnsEndPoint, cancellationToken).ConfigureAwait(false);
            return new NetworkStream(socket, ownsSocket: true);
        }
        catch
        {
            socket.Dispose();
            throw;
        }
    }
}

This overrides SocketsHttpHandler.ConnectCallback to enforce IPV4.

@jchhh912
Copy link
Author

@jchhh912 this might be caused by issues with IPV6 and/or dual-stack socket support in your corpnet or the McAffee proxy. Since .NET 5.0, is using dual-stack sockets by default. Can you check if the following workaround helps:HttpClient

public HttpClient CreateWorkaroundClient()
{
    SocketsHttpHandler handler = new SocketsHttpHandler
    {
        ConnectCallback = IPv4ConnectAsync
    };
    return new HttpClient(handler);

    static async ValueTask<Stream> IPv4ConnectAsync(SocketsHttpConnectionContext context, CancellationToken cancellationToken)
    {
        // By default, we create dual-mode sockets:
        // Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp);

        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        socket.NoDelay = true;

        try
        {
            await socket.ConnectAsync(context.DnsEndPoint, cancellationToken).ConfigureAwait(false);
            return new NetworkStream(socket, ownsSocket: true);
        }
        catch
        {
            socket.Dispose();
            throw;
        }
    }
}

This overrides to enforce IPV4.SocketsHttpHandler.ConnectCallback

Thanks, this is useful and I was wondering if this would be fixed in 6.0?

@jchhh912
Copy link
Author

@jchhh912 will you get the same exception if you use packet capture tool like Wireshark?

I'm sorry, but I can't use Wireshark due to my company's IT policy, but if there is another suitable method, I'd be happy to provide you with information.

@antonfirsov
Copy link
Member

@jchhh912 I'm not familiar with the McAfee software mentioned, I don't know what does the log in the screenshot mean.

For us the most important question here is if the workaround code in #54807 (comment) changes anything while keeping your company software turned on.

@jchhh912
Copy link
Author

@antonfirsov Yes, my application works fine after using it, but do I need to come back and change my code if my .net core version continues to be upgraded later?

@karelz
Copy link
Member

karelz commented Jun 30, 2021

We plan to implement this in .NET 6: #47583 ... as a workaround and escape route for environments where IPv6 does not work properly (like yours - likely caused by MacAfee SW).
If you need solution on .NET 5 or before .NET 6 ships, you should use the code workaround mentioned above.
Closing as resolved.

@karelz karelz closed this as completed Jun 30, 2021
@karelz karelz added this to the 6.0.0 milestone Jun 30, 2021
@karelz karelz removed needs more info untriaged New issue has not been triaged by the area owner labels Jun 30, 2021
@antonfirsov
Copy link
Member

@jchhh912 .NET uses IPv6 dual-stack sockets very extensively, switching to them in HttpClient was a natural step, we don't plan to revert it.

In our experience, IPv6 works 99.9% of the time, when not, it's typically a problem with IPv6 support within your IT infrastructure. We recommend to raise an issue against your IT department. If they can't resolve it for some reason, #47583 will provide an easy workaround (disabling IPv6 globally within your .NET app).

alnikola pushed a commit that referenced this issue Jul 12, 2021
Fixes #47583. Resolves #52287, #54807 and similar issues, without changing customer application code.

Introduces an AppContext switch `System.Net.DisableIPv6` and environment variable `DOTNET_SYSTEM_NET_DISABLEIPV6` to emulate the lack of OS-level IPv6 support. This is useful to workaround dual-stack socket issues when the OS reports support of IPv6, but some other underlying infrastructure element (typically VPN) doesn't function well with IPv6 request.

For consistency, this switch also impacts NameResolution and Ping, not only Sockets.
@ghost ghost locked as resolved and limited conversation to collaborators Jul 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants