Skip to content

WebSocket ability to force IPv6 or IPv4 #45462

Closed

Description

@xinbingyiran

UPDATE: Scoped to WebSocket only as there is no way to achieve that.
HttpClient has a way and HttpWebRequest is legacy API (so no need).


I find the sulution for httpclient from issues like this.

var handler = new SocketsHttpHandler{
   ConnectCallback = async (context,token)=>{
      // dns get ipv6 and connect
      return ipv6stream
   }
};
var client = new HttpClient(handler);// HttpClientHandle???

but it can useful for httpclient only,and it replaced httpclienthandle,will lost the diagnosticsHandler yet.

and I view the httpwebrequest and websocket source code, the SocketsHttpHandler is a private member of them only,and i can't get the ConnectCallback for using.

so how can i force ipv6 or ipv4 on httpwebrequest, websocket and httpclient(with diagnosticsHandler )

Activity

  1. scalablecory commented on Dec 3, 2020

    @scalablecory
    Contributor

    There's no way to do this with HttpWebRequest or WebSocket.
    The former is obsolete and not being invested in. We could consider an API for WebSocket, though.

    Not being able to use DiagnosticsHandler with SocketsHttpHandler is a known wart -- tracking that at #31261

  2. xinbingyiran commented on Dec 4, 2020

    @xinbingyiran
    Author

    Except DiagnosticsHandler,for replace the HttpWebRequest,I can use HttpClient via ConnectCallback to implement a ipv6 web request , I'm really looking forward to a similar API for 'WebSocket' as well.

  3. scalablecory commented on Dec 4, 2020

    @scalablecory
    Contributor

    Except DiagnosticsHandler,for replace the HttpWebRequest,I can use HttpClient via ConnectCallback to implement a ipv6 web request , I'm really looking forward to a similar API for 'WebSocket' as well.

    Actually, you can use WebSocket.CreateFromStream here, so I don't think any new API is needed.

  4. xinbingyiran commented on Dec 4, 2020

    @xinbingyiran
    Author

    Except DiagnosticsHandler,for replace the HttpWebRequest,I can use HttpClient via ConnectCallback to implement a ipv6 web request , I'm really looking forward to a similar API for 'WebSocket' as well.

    Actually, you can use WebSocket.CreateFromStream here, so I don't think any new API is needed.

    If I use WebSocket.CreateFromStream,it means i must copy all code of 'WebSocketHandle.ConnectAsync',and add 'ConnectCallback' to 'SocketsHttpHandler',to completed the websocket protocol upgrade manual,just for use ConnectCallback
    only.
    does it has a simple implement such as add the 'SocketsHttpHandler' or 'ConnectCallback' member to 'ClientWebSocketOptions'.

  5. changed the title [-]when i use tcp connection via httpwebrequest, websocket and httpclient,how can i force ipv6 or ipv4[/-] [+]When I use TCP connection via HttpWebRequest, WebSocket and HttpClient, how can I force IPv6 or IPv4[/+] on Dec 8, 2020
  6. added
    questionAnswer questions and provide assistance, not an issue with source code or documentation.
    and removed
    untriagedNew issue has not been triaged by the area owner
    on Dec 8, 2020
  7. karelz commented on Dec 8, 2020

    @karelz
    Member

    Triage: @xinbingyiran how much code is that? Is that inconveniently long code?

  8. added
    untriagedNew issue has not been triaged by the area owner
    on Dec 8, 2020
  9. xinbingyiran commented on Dec 9, 2020

    @xinbingyiran
    Author

    Triage: @xinbingyiran how much code is that? Is that inconveniently long code?

    Yes, It's not,but...
    At present,if a developer need to switch a option for using ipv4 or ipv6,he must view the source code of runtime, to find the logic of connection(it may be a long time),and then copy all of them to his own code and change it carefully.
    Only for using ipv6 or ConnectCallback of SocketsHttpHandler.do you think it's a better way than to offer an option?
    And there is a SocketsHttpHandler for using in ClientWebSocketOptions,why not try exposing it or its callback method to the developer?

  10. wfurt commented on Dec 9, 2020

    @wfurt
    Member

    In general case, this should not be needed, right @xinbingyiran? If the service is not IPv6 capable, it should not be in DNS in first place. If IPv6 is not available on the client it should fall-back to IPv4. The ConnectCallback was added primarily for use-cases other than this.
    While WebSocket may be interesting , HttpWebRequest is viewed as obsolete and it will be official marked as such soon.

  11. xinbingyiran commented on Dec 9, 2020

    @xinbingyiran
    Author

    What if I need to connect to a 'WebSocket' server that supports both IPv4 and IPv6(e.g. ws://localhost/) over the IPv4 protocol? Just like I need to make an IPv4 Web request, @wfurt .

    Since HTTP is supported, how about WebSocket?

  12. 6 remaining items

  13. added
    api-approvedAPI was approved in API review, it can be implemented
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    and removed
    api-approvedAPI was approved in API review, it can be implemented
    on Dec 17, 2020
  14. added this to the Future milestone on Dec 17, 2020
  15. antonfirsov commented on Feb 9, 2021

    @antonfirsov
    Contributor

    @xinbingyiran I'm wondering if you can elaborate a bit on your use-case.

    In #47583 we are proposing a switch to globally disable IPv6, is this something you would find useful?

  16. geoffkizer commented on Feb 9, 2021

    @geoffkizer
    Contributor

    At some point we had discussed allowing you to specify and configure the instance of SocketsHttpHandler that is used by the WebSocket. Seems like that would allow you to address this scenario.

  17. CarnaViire commented on Aug 12, 2025

    @CarnaViire
    Member

    At some point we had discussed allowing you to specify and configure the instance of SocketsHttpHandler that is used by the WebSocket. Seems like that would allow you to address this scenario.

    Triage: This is now possible, you can use a custom configured HttpMessageInvoker (or HttpClient) instance in the ClientWebSocket.ConnectAsync(Uri, HttpMessageInvoker, CancellationToken) overload (introduced in .NET 7).

    Closing as completed.

  18. modified the milestones: Future, 10.0.0 on Aug 13, 2025
  19. locked and limited conversation to collaborators on Sep 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Net

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions