It's in the documentation you linked to.
As a general rule, you should first look or ask around for updated APIs that support TAP directly. Almost all BCL classes have already been updated to support TAP, and a handful (such as HttpWebRequest
) have been replaced with TAP alternatives (e.g., HttpClient
). In this case, there isn't a TAP TcpClient
equivalent, so wrapping them is your best bet.
If you do write TAP over APM wrappers, I recommend using simple extension methods:
public static Task ConnectTaskAsync(this TcpClient client, IPAddress address, int port)
{
return Task.Factory.FromAsync(client.BeginConnect, client.EndConnect, address, port, null);
}
This gives you a natural way to consume them, and separates your "interop" code from any code containing actual logic:
async Task SomeMethodAsync()
{
this.tcpClient = new TcpClient();
await this.tcpClient.ConnectTaskAsync(ip, port);
// ... do stuff ...
}