Flowed cancellation token to TcpClient.ConnectAsync in .NET 5.0 target

This commit is contained in:
Günther Foidl
2021-12-26 19:26:04 +01:00
parent b475aee2e7
commit 9c3f95ce73
2 changed files with 8 additions and 3 deletions

View File

@@ -25,7 +25,7 @@ namespace S7.Net
/// <returns>A task that represents the asynchronous open operation.</returns> /// <returns>A task that represents the asynchronous open operation.</returns>
public async Task OpenAsync(CancellationToken cancellationToken = default) public async Task OpenAsync(CancellationToken cancellationToken = default)
{ {
var stream = await ConnectAsync().ConfigureAwait(false); var stream = await ConnectAsync(cancellationToken).ConfigureAwait(false);
try try
{ {
await queue.Enqueue(async () => await queue.Enqueue(async () =>
@@ -44,11 +44,16 @@ namespace S7.Net
} }
} }
private async Task<NetworkStream> ConnectAsync() private async Task<NetworkStream> ConnectAsync(CancellationToken cancellationToken)
{ {
tcpClient = new TcpClient(); tcpClient = new TcpClient();
ConfigureConnection(); ConfigureConnection();
#if NET5_0_OR_GREATER
await tcpClient.ConnectAsync(IP, Port, cancellationToken).ConfigureAwait(false);
#else
await tcpClient.ConnectAsync(IP, Port).ConfigureAwait(false); await tcpClient.ConnectAsync(IP, Port).ConfigureAwait(false);
#endif
return tcpClient.GetStream(); return tcpClient.GetStream();
} }

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net452;netstandard2.0;netstandard1.3</TargetFrameworks> <TargetFrameworks>net452;netstandard2.0;netstandard1.3;net5.0</TargetFrameworks>
<SignAssembly>true</SignAssembly> <SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>Properties\S7.Net.snk</AssemblyOriginatorKeyFile> <AssemblyOriginatorKeyFile>Properties\S7.Net.snk</AssemblyOriginatorKeyFile>
<InternalsVisibleTo>S7.Net.UnitTest</InternalsVisibleTo> <InternalsVisibleTo>S7.Net.UnitTest</InternalsVisibleTo>