Handle task cancellation in the .NET client

#2178