Skip to content

Option to have more control when resolving endpoint IP address in SocketsHttpHandler #25401

Closed
@AppBeat

Description

@AppBeat

I think it may be useful in some cases to have more control over endpoint address resolving.
This would be new optional feature in SocketsHttpHandler implementation. Here is my initial idea.

Proposed API

public sealed class SocketsHttpHandler : HttpMessageHandler
{
   //existing members
   //...

   //Proposed API
   public Func<HttpRequestMessage, string, int, CancellationToken, ValueTask<IPEndPoint>> ResolveEndpointAsync {get;set;}
}

where delegate inputs are:

  • HttpRequestMessage -> request details
  • string -> request.RequestUri.IdnHost
  • int -> request.RequestUri.Port
  • CancellationToken

and delegate output is:

  • ValueTask<IPEndPoint> -> representing resolved IP address based on given input

Similar Func<> pattern is used in WinHttpHandler.ServerCertificateValidationCallback

Example usage

socketsHttpHandlerInstance.ResolveEndpointAsync = async delegate (HttpRequestMessage request, string host, int port, CancellationToken cancellationToken)
{
	var addresses = await Dns.GetHostAddressesAsync(host).ConfigureAwait(false);
	//resolve hostname to IPv6
	var address = addresses.Where(addr => addr.AddressFamily == AddressFamily.InterNetworkV6).FirstOrDefault();
	return address != null ? new IPEndPoint(address, port) : null;
};

Main goals

  • feature is optional and should not change current behaviour in any way
  • if feature is not used it should not affect existing performance

Possible use cases

  • one could implement ResolveEndpointAsync to use Google DNS or other DNS server for resolving addresses, without changing system settings
  • one could implement ResolveEndpointAsync to use round-robin for resolving IP Addresses
  • better control over IPv4/IPv6 resolving
  • could be used in functional testing to resolve test IP addresses

Prototype implementation: https://github.com/AppBeat/corefx
Very early PR: dotnet/corefx#27937

Kind regards,
Vladimir

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-needs-workAPI needs work before it is approved, it is NOT ready for implementationarea-System.Net.Http

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions