private static IAsyncResult HostResolutionBeginHelper(string hostName, bool justReturnParsedIp, bool includeIPv6, bool throwOnIIPAny, AsyncCallback requestCallback, object state)
{
if (hostName == null)
{
throw new ArgumentNullException(nameof(hostName));
}
if (NetEventSource.IsEnabled) NetEventSource.Info(null, hostName);
// See if it's an IP Address.
IPAddress address;
ResolveAsyncResult asyncResult;
if (IPAddress.TryParse(hostName, out address))
{
if (throwOnIIPAny && (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any)))
{
throw new ArgumentException(SR.net_invalid_ip_addr, nameof(hostName));
}
asyncResult = new ResolveAsyncResult(address, null, includeIPv6, state, requestCallback);
if (justReturnParsedIp)
{
IPHostEntry hostEntry = NameResolutionUtilities.GetUnresolvedAnswer(address);
asyncResult.StartPostingAsyncOp(false);
asyncResult.InvokeCallback(hostEntry);
asyncResult.FinishPostingAsyncOp();
return asyncResult;
}
}
else
{
asyncResult = new ResolveAsyncResult(hostName, null, includeIPv6, state, requestCallback);
}
// Set up the context, possibly flow.
asyncResult.StartPostingAsyncOp(false);
// Start the resolve.
Task.Factory.StartNew(
s => ResolveCallback(s),
asyncResult,
CancellationToken.None,
TaskCreationOptions.DenyChildAttach,
TaskScheduler.Default);
// Finish the flowing, maybe it completed? This does nothing if we didn't initiate the flowing above.
asyncResult.FinishPostingAsyncOp();
return asyncResult;
}