#endif
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(ReadAsync(buffer, offset, count), callback, state);
+ TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count), callback, state);
public override int EndRead(IAsyncResult asyncResult)
{
EnsureNotClosed();
- return TaskToApm.End<int>(asyncResult);
+ return TaskToAsyncResult.End<int>(asyncResult);
}
#if !NETFRAMEWORK && !NETSTANDARD2_0
+++ /dev/null
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-// Helper methods for using Tasks to implement the APM pattern.
-//
-// Example usage, wrapping a Task<int>-returning FooAsync method with Begin/EndFoo methods:
-//
-// public IAsyncResult BeginFoo(..., AsyncCallback? callback, object? state) =>
-// TaskToApm.Begin(FooAsync(...), callback, state);
-//
-// public int EndFoo(IAsyncResult asyncResult) =>
-// TaskToApm.End<int>(asyncResult);
-
-using System.Diagnostics;
-using System.Diagnostics.CodeAnalysis;
-
-namespace System.Threading.Tasks
-{
- /// <summary>
- /// Provides support for efficiently using Tasks to implement the APM (Begin/End) pattern.
- /// </summary>
- internal static class TaskToApm
- {
- /// <summary>
- /// Marshals the Task as an IAsyncResult, using the supplied callback and state
- /// to implement the APM pattern.
- /// </summary>
- /// <param name="task">The Task to be marshaled.</param>
- /// <param name="callback">The callback to be invoked upon completion.</param>
- /// <param name="state">The state to be stored in the IAsyncResult.</param>
- /// <returns>An IAsyncResult to represent the task's asynchronous operation.</returns>
- public static IAsyncResult Begin(Task task, AsyncCallback? callback, object? state) =>
- new TaskAsyncResult(task, state, callback);
-
- /// <summary>Processes an IAsyncResult returned by Begin.</summary>
- /// <param name="asyncResult">The IAsyncResult to unwrap.</param>
- public static void End(IAsyncResult asyncResult)
- {
- if (GetTask(asyncResult) is Task t)
- {
- t.GetAwaiter().GetResult();
- return;
- }
-
- ThrowArgumentException(asyncResult);
- }
-
- /// <summary>Processes an IAsyncResult returned by Begin.</summary>
- /// <param name="asyncResult">The IAsyncResult to unwrap.</param>
- public static TResult End<TResult>(IAsyncResult asyncResult)
- {
- if (GetTask(asyncResult) is Task<TResult> task)
- {
- return task.GetAwaiter().GetResult();
- }
-
- ThrowArgumentException(asyncResult);
- return default!; // unreachable
- }
-
- /// <summary>Gets the task represented by the IAsyncResult.</summary>
- public static Task? GetTask(IAsyncResult asyncResult) => (asyncResult as TaskAsyncResult)?._task;
-
- /// <summary>Throws an argument exception for the invalid <paramref name="asyncResult"/>.</summary>
- [DoesNotReturn]
- private static void ThrowArgumentException(IAsyncResult asyncResult)
- {
- throw asyncResult is null ?
- new ArgumentNullException(nameof(asyncResult)) :
- new ArgumentException(null, nameof(asyncResult));
- }
-
- /// <summary>Provides a simple IAsyncResult that wraps a Task.</summary>
- /// <remarks>
- /// We could use the Task as the IAsyncResult if the Task's AsyncState is the same as the object state,
- /// but that's very rare, in particular in a situation where someone cares about allocation, and always
- /// using TaskAsyncResult simplifies things and enables additional optimizations.
- /// </remarks>
- internal sealed class TaskAsyncResult : IAsyncResult
- {
- /// <summary>The wrapped Task.</summary>
- internal readonly Task _task;
- /// <summary>Callback to invoke when the wrapped task completes.</summary>
- private readonly AsyncCallback? _callback;
-
- /// <summary>Initializes the IAsyncResult with the Task to wrap and the associated object state.</summary>
- /// <param name="task">The Task to wrap.</param>
- /// <param name="state">The new AsyncState value.</param>
- /// <param name="callback">Callback to invoke when the wrapped task completes.</param>
- internal TaskAsyncResult(Task task, object? state, AsyncCallback? callback)
- {
- Debug.Assert(task != null);
- _task = task;
- AsyncState = state;
-
- if (task.IsCompleted)
- {
- // Synchronous completion. Invoke the callback. No need to store it.
- CompletedSynchronously = true;
- callback?.Invoke(this);
- }
- else if (callback != null)
- {
- // Asynchronous completion, and we have a callback; schedule it. We use OnCompleted rather than ContinueWith in
- // order to avoid running synchronously if the task has already completed by the time we get here but still run
- // synchronously as part of the task's completion if the task completes after (the more common case).
- _callback = callback;
- _task.ConfigureAwait(continueOnCapturedContext: false)
- .GetAwaiter()
- .OnCompleted(InvokeCallback); // allocates a delegate, but avoids a closure
- }
- }
-
- /// <summary>Invokes the callback.</summary>
- private void InvokeCallback()
- {
- Debug.Assert(!CompletedSynchronously);
- Debug.Assert(_callback != null);
- _callback.Invoke(this);
- }
-
- /// <summary>Gets a user-defined object that qualifies or contains information about an asynchronous operation.</summary>
- public object? AsyncState { get; }
- /// <summary>Gets a value that indicates whether the asynchronous operation completed synchronously.</summary>
- /// <remarks>This is set lazily based on whether the <see cref="_task"/> has completed by the time this object is created.</remarks>
- public bool CompletedSynchronously { get; }
- /// <summary>Gets a value that indicates whether the asynchronous operation has completed.</summary>
- public bool IsCompleted => _task.IsCompleted;
- /// <summary>Gets a <see cref="WaitHandle"/> that is used to wait for an asynchronous operation to complete.</summary>
- public WaitHandle AsyncWaitHandle => ((IAsyncResult)_task).AsyncWaitHandle;
- }
- }
-}
--- /dev/null
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System.Diagnostics;
+
+namespace System.Threading.Tasks
+{
+ /// <summary>
+ /// Provides methods for using <see cref="Task"/> to implement the Asynchronous Programming Model
+ /// pattern based on "Begin" and "End" methods.
+ /// </summary>
+#if SYSTEM_PRIVATE_CORELIB
+ public
+#else
+ internal
+#endif
+ static class TaskToAsyncResult
+ {
+ /// <summary>Creates a new <see cref="IAsyncResult"/> from the specified <see cref="Task"/>, optionally invoking <paramref name="callback"/> when the task has completed.</summary>
+ /// <param name="task">The <see cref="Task"/> to be wrapped in an <see cref="IAsyncResult"/>.</param>
+ /// <param name="callback">The callback to be invoked upon <paramref name="task"/>'s completion. If <see langword="null"/>, no callback will be invoked.</param>
+ /// <param name="state">The state to be stored in the <see cref="IAsyncResult"/>.</param>
+ /// <returns>An <see cref="IAsyncResult"/> to represent the task's asynchronous operation. This instance will also be passed to <paramref name="callback"/> when it's invoked.</returns>
+ /// <exception cref="ArgumentNullException"><paramref name="task"/> is null.</exception>
+ /// <remarks>
+ /// In conjunction with the <see cref="End(IAsyncResult)"/> or <see cref="End{TResult}(IAsyncResult)"/> methods, this method may be used
+ /// to implement the Begin/End pattern (also known as the Asynchronous Programming Model pattern, or APM). It is recommended to not expose this pattern
+ /// in new code; the methods on <see cref="TaskToAsyncResult"/> are intended only to help implement such Begin/End methods when they must be exposed, for example
+ /// because a base class provides virtual methods for the pattern, or when they've already been exposed and must remain for compatibility. These methods enable
+ /// implementing all of the core asynchronous logic via <see cref="Task"/>s and then easily implementing Begin/End methods around that functionality.
+ /// </remarks>
+ public static IAsyncResult Begin(Task task, AsyncCallback? callback, object? state)
+ {
+#if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(task);
+#else
+ if (task is null)
+ {
+ throw new ArgumentNullException(nameof(task));
+ }
+#endif
+
+ return new TaskAsyncResult(task, state, callback);
+ }
+
+ /// <summary>Waits for the <see cref="Task"/> wrapped by the <see cref="IAsyncResult"/> returned by <see cref="Begin"/> to complete.</summary>
+ /// <param name="asyncResult">The <see cref="IAsyncResult"/> for which to wait.</param>
+ /// <exception cref="ArgumentNullException"><paramref name="asyncResult"/> is null.</exception>
+ /// <exception cref="ArgumentException"><paramref name="asyncResult"/> was not produced by a call to <see cref="Begin"/>.</exception>
+ /// <remarks>This will propagate any exception stored in the wrapped <see cref="Task"/>.</remarks>
+ public static void End(IAsyncResult asyncResult) =>
+ Unwrap(asyncResult).GetAwaiter().GetResult();
+
+ /// <summary>Waits for the <see cref="Task{TResult}"/> wrapped by the <see cref="IAsyncResult"/> returned by <see cref="Begin"/> to complete.</summary>
+ /// <param name="asyncResult">The <see cref="IAsyncResult"/> for which to wait.</param>
+ /// <returns>The result of the <see cref="Task{TResult}"/> wrapped by the <see cref="IAsyncResult"/>.</returns>
+ /// <exception cref="ArgumentNullException"><paramref name="asyncResult"/> is null.</exception>
+ /// <exception cref="ArgumentException"><paramref name="asyncResult"/> was not produced by a call to <see cref="Begin"/>.</exception>
+ /// <remarks>This will propagate any exception stored in the wrapped <see cref="Task{TResult}"/>.</remarks>
+ public static TResult End<TResult>(IAsyncResult asyncResult) =>
+ Unwrap<TResult>(asyncResult).GetAwaiter().GetResult();
+
+ /// <summary>Extracts the underlying <see cref="Task"/> from an <see cref="IAsyncResult"/> created by <see cref="Begin"/>.</summary>
+ /// <param name="asyncResult">The <see cref="IAsyncResult"/> created by <see cref="Begin"/>.</param>
+ /// <returns>The <see cref="Task"/> wrapped by the <see cref="IAsyncResult"/>.</returns>
+ /// <exception cref="ArgumentNullException"><paramref name="asyncResult"/> is null.</exception>
+ /// <exception cref="ArgumentException"><paramref name="asyncResult"/> was not produced by a call to <see cref="Begin"/>.</exception>
+ public static Task Unwrap(IAsyncResult asyncResult)
+ {
+#if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(asyncResult);
+#else
+ if (asyncResult is null)
+ {
+ throw new ArgumentNullException(nameof(asyncResult));
+ }
+#endif
+
+ if ((asyncResult as TaskAsyncResult)?._task is not Task task)
+ {
+ throw new ArgumentException(null, nameof(asyncResult));
+ }
+
+ return task;
+ }
+
+ /// <summary>Extracts the underlying <see cref="Task{TResult}"/> from an <see cref="IAsyncResult"/> created by <see cref="Begin"/>.</summary>
+ /// <param name="asyncResult">The <see cref="IAsyncResult"/> created by <see cref="Begin"/>.</param>
+ /// <returns>The <see cref="Task{TResult}"/> wrapped by the <see cref="IAsyncResult"/>.</returns>
+ /// <exception cref="ArgumentNullException"><paramref name="asyncResult"/> is null.</exception>
+ /// <exception cref="ArgumentException">
+ /// <paramref name="asyncResult"/> was not produced by a call to <see cref="Begin"/>,
+ /// or the <see cref="Task{TResult}"/> provided to <see cref="Begin"/> was used a generic type parameter
+ /// that's different from the <typeparamref name="TResult"/> supplied to this call.
+ /// </exception>
+ public static Task<TResult> Unwrap<TResult>(IAsyncResult asyncResult)
+ {
+#if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(asyncResult);
+#else
+ if (asyncResult is null)
+ {
+ throw new ArgumentNullException(nameof(asyncResult));
+ }
+#endif
+
+ if ((asyncResult as TaskAsyncResult)?._task is not Task<TResult> task)
+ {
+ throw new ArgumentException(null, nameof(asyncResult));
+ }
+
+ return task;
+ }
+
+ /// <summary>Provides a simple <see cref="IAsyncResult"/> that wraps a <see cref="Task"/>.</summary>
+ /// <remarks>
+ /// We could use the Task as the IAsyncResult if the Task's AsyncState is the same as the object state,
+ /// but that's very rare, in particular in a situation where someone cares about allocation, and always
+ /// using TaskAsyncResult simplifies things and enables additional optimizations.
+ /// </remarks>
+ private sealed class TaskAsyncResult : IAsyncResult
+ {
+ /// <summary>The wrapped Task.</summary>
+ internal readonly Task _task;
+ /// <summary>Callback to invoke when the wrapped task completes.</summary>
+ private readonly AsyncCallback? _callback;
+
+ /// <summary>Initializes the IAsyncResult with the Task to wrap and the associated object state.</summary>
+ /// <param name="task">The Task to wrap.</param>
+ /// <param name="state">The new AsyncState value.</param>
+ /// <param name="callback">Callback to invoke when the wrapped task completes.</param>
+ internal TaskAsyncResult(Task task, object? state, AsyncCallback? callback)
+ {
+ Debug.Assert(task is not null);
+
+ _task = task;
+ AsyncState = state;
+
+ if (task.IsCompleted)
+ {
+ // The task has already completed. Treat this as synchronous completion.
+ // Invoke the callback; no need to store it.
+ CompletedSynchronously = true;
+ callback?.Invoke(this);
+ }
+ else if (callback is not null)
+ {
+ // Asynchronous completion, and we have a callback; schedule it. We use OnCompleted rather than ContinueWith in
+ // order to avoid running synchronously if the task has already completed by the time we get here but still run
+ // synchronously as part of the task's completion if the task completes after (the more common case).
+ _callback = callback;
+ _task.ConfigureAwait(continueOnCapturedContext: false)
+ .GetAwaiter()
+ .OnCompleted(() => _callback.Invoke(this));
+ }
+ }
+
+ /// <inheritdoc/>
+ public object? AsyncState { get; }
+
+ /// <inheritdoc/>
+ public bool CompletedSynchronously { get; }
+
+ /// <inheritdoc/>
+ public bool IsCompleted => _task.IsCompleted;
+
+ /// <inheritdoc/>
+ public WaitHandle AsyncWaitHandle => ((IAsyncResult)_task).AsyncWaitHandle;
+ }
+ }
+}
Link="System\Net\Sockets\Fletcher32.cs" />
<Compile Include="$(CommonPath)System\Net\Logging\NetEventSource.Common.cs"
Link="Common\System\Net\Logging\NetEventSource.Common.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="Common\System\Threading\Tasks\TaskToApm.cs" />
<Compile Include="$(CommonPath)System\IO\PathInternal.cs"
Link="System\IO\PathInternal.cs" />
<Compile Include="System\IO\ConnectedStreams.cs" Link="System\IO\ConnectedStreams.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="$(CommonTestPath)System\IO\ConnectedStreams.cs" Link="System\IO\ConnectedStreams.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs" Link="System\Threading\Tasks\TaskToApm.cs" />
<Compile Include="$(CommonPath)System\Net\ArrayBuffer.cs" Link="Common\System\Net\ArrayBuffer.cs" />
<Compile Include="$(CommonPath)System\Net\MultiArrayBuffer.cs" Link="Common\System\Net\MultiArrayBuffer.cs" />
<Compile Include="$(CommonPath)System\Net\StreamBuffer.cs" Link="Common\System\Net\StreamBuffer.cs" />
isSync = sync;
}
- public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state) => TaskToApm.Begin(ReadAsync(buffer, offset, count), callback, state);
- public override int EndRead(IAsyncResult asyncResult) => TaskToApm.End<int>(asyncResult);
- public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state) => TaskToApm.Begin(WriteAsync(buffer, offset, count), callback, state);
- public override void EndWrite(IAsyncResult asyncResult) => TaskToApm.End(asyncResult);
+ public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state) => TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count), callback, state);
+ public override int EndRead(IAsyncResult asyncResult) => TaskToAsyncResult.End<int>(asyncResult);
+ public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state) => TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count), callback, state);
+ public override void EndWrite(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult);
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
}
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(ReadAsync(buffer, offset, count), callback, state);
+ TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count), callback, state);
public override int EndRead(IAsyncResult asyncResult)
{
ThrowIfDisposed();
ThrowIfReadingNotSupported();
- return TaskToApm.End<int>(asyncResult);
+ return TaskToAsyncResult.End<int>(asyncResult);
}
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
}
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(WriteAsync(buffer, offset, count), callback, state);
+ TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count), callback, state);
public override void EndWrite(IAsyncResult asyncResult)
{
ThrowIfDisposed();
ThrowIfWritingNotSupported();
- TaskToApm.End(asyncResult);
+ TaskToAsyncResult.End(asyncResult);
}
public override long Length => throw new NotSupportedException();
}
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(ReadAsync(buffer, offset, count), callback, state);
+ TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count), callback, state);
public override int EndRead(IAsyncResult asyncResult)
{
ThrowIfDisposed();
- return TaskToApm.End<int>(asyncResult);
+ return TaskToAsyncResult.End<int>(asyncResult);
}
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
}
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(WriteAsync(buffer, offset, count), callback, state);
+ TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count), callback, state);
public override void EndWrite(IAsyncResult asyncResult)
{
ThrowIfDisposed();
- TaskToApm.End(asyncResult);
+ TaskToAsyncResult.End(asyncResult);
}
public override long Length => throw new NotSupportedException();
}
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(ReadAsync(buffer, offset, count), callback, state);
+ TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count), callback, state);
- public override int EndRead(IAsyncResult asyncResult) => TaskToApm.End<int>(asyncResult);
+ public override int EndRead(IAsyncResult asyncResult) => TaskToAsyncResult.End<int>(asyncResult);
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
}
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(WriteAsync(buffer, offset, count), callback, state);
+ TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count), callback, state);
- public override void EndWrite(IAsyncResult asyncResult) => TaskToApm.End(asyncResult);
+ public override void EndWrite(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult);
public override void Write(byte[] buffer, int offset, int count)
{
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-unix;$(NetCoreAppCurrent)</TargetFrameworks>
<Compile Include="System\IO\Compression\enc\BrotliEncoderOperation.cs" />
<Compile Include="System\IO\Compression\enc\BrotliEncoderParameter.cs" />
<Compile Include="System\IO\Compression\BrotliStream.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="Common\System\Threading\Tasks\TaskToApm.cs" />
<Compile Include="$(CommonPath)Microsoft\Win32\SafeHandles\SafeBrotliHandle.cs"
Link="Common\Microsoft\Win32\SafeHandles\SafeBrotliHandle.cs" />
</ItemGroup>
/// <exception cref="System.NotSupportedException">The current <see cref="System.IO.Compression.BrotliStream" /> implementation does not support the read operation.</exception>
/// <exception cref="System.InvalidOperationException">This call cannot be completed.</exception>
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) =>
- TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
+ TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
/// <summary>Waits for the pending asynchronous read to complete. (Consider using the <see cref="System.IO.Stream.ReadAsync(byte[],int,int)" /> method instead.)</summary>
/// <param name="asyncResult">The reference to the pending asynchronous request to finish.</param>
/// <exception cref="System.ArgumentException"><paramref name="asyncResult" /> did not originate from a <see cref="System.IO.Compression.BrotliStream.BeginRead(byte[],int,int,System.AsyncCallback,object)" /> method on the current stream.</exception>
/// <exception cref="System.InvalidOperationException">The end operation cannot be performed because the stream is closed.</exception>
public override int EndRead(IAsyncResult asyncResult) =>
- TaskToApm.End<int>(asyncResult);
+ TaskToAsyncResult.End<int>(asyncResult);
/// <summary>Asynchronously reads a sequence of bytes from the current Brotli stream, writes them to a byte array starting at a specified index, advances the position within the Brotli stream by the number of bytes read, and monitors cancellation requests.</summary>
/// <param name="buffer">The buffer to write the data into.</param>
/// <exception cref="System.NotSupportedException">The current <see cref="System.IO.Compression.BrotliStream" /> implementation does not support the write operation.</exception>
/// <exception cref="System.InvalidOperationException">The write operation cannot be performed because the stream is closed.</exception>
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) =>
- TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
+ TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
/// <summary>Handles the end of an asynchronous write operation. (Consider using the <see cref="System.IO.Stream.WriteAsync(byte[],int,int)" /> method instead.)</summary>
/// <param name="asyncResult">The object that represents the asynchronous call.</param>
/// <exception cref="System.InvalidOperationException">The underlying stream is closed or <see langword="null" />.</exception>
public override void EndWrite(IAsyncResult asyncResult) =>
- TaskToApm.End(asyncResult);
+ TaskToAsyncResult.End(asyncResult);
/// <summary>Asynchronously writes compressed bytes to the underlying Brotli stream from the specified byte array.</summary>
/// <param name="buffer">The buffer that contains the data to compress.</param>
Link="Common\System\IO\Compression\ZipTestHelper.cs" />
<Compile Include="$(CommonTestPath)System\IO\TempFile.cs"
Link="Common\System\IO\TempFile.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="Common\System\Threading\Tasks\TaskToApm.cs" />
<Compile Include="$(CommonTestPath)System\IO\ConnectedStreams.cs" Link="Common\System\IO\ConnectedStreams.cs" />
<Compile Include="$(CommonPath)System\Net\MultiArrayBuffer.cs" Link="ProductionCode\Common\System\Net\MultiArrayBuffer.cs" />
<Compile Include="$(CommonPath)System\Net\StreamBuffer.cs" Link="ProductionCode\Common\System\Net\StreamBuffer.cs" />
<Compile Include="System\IO\Compression\GZipStream.cs" />
<Compile Include="System\IO\Compression\PositionPreservingWriteOnlyStreamWrapper.cs" />
<Compile Include="System\IO\Compression\ZLibStream.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="Common\System\Threading\Tasks\TaskToApm.cs" />
</ItemGroup>
<!-- Windows specific files -->
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'windows'">
}
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) =>
- TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
+ TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
public override int EndRead(IAsyncResult asyncResult) =>
- TaskToApm.End<int>(asyncResult);
+ TaskToAsyncResult.End<int>(asyncResult);
private ValueTask<int> ReadAsyncInternal(Memory<byte> buffer, CancellationToken cancellationToken)
{
throw new InvalidDataException(SR.TruncatedData);
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) =>
- TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
+ TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
public override int EndRead(IAsyncResult asyncResult)
{
EnsureDecompressionMode();
EnsureNotDisposed();
- return TaskToApm.End<int>(asyncResult);
+ return TaskToAsyncResult.End<int>(asyncResult);
}
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
}
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) =>
- TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
+ TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
public override void EndWrite(IAsyncResult asyncResult)
{
EnsureCompressionMode();
EnsureNotDisposed();
- TaskToApm.End(asyncResult);
+ TaskToAsyncResult.End(asyncResult);
}
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
}
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) =>
- TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
+ TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
public override int EndRead(IAsyncResult asyncResult) =>
_deflateStream.EndRead(asyncResult);
}
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) =>
- TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
+ TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
public override void EndWrite(IAsyncResult asyncResult) =>
_deflateStream.EndWrite(asyncResult);
<Compile Include="$(CommonTestPath)System\IO\WrappedStream.cs" Link="Common\System\IO\WrappedStream.cs" />
<Compile Include="$(CommonTestPath)System\IO\Compression\ZipTestHelper.cs" Link="Common\System\IO\Compression\ZipTestHelper.cs" />
<Compile Include="$(CommonTestPath)TestUtilities\System\DisableParallelization.cs" Link="Common\TestUtilities\System\DisableParallelization.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs" Link="Common\System\Threading\Tasks\TaskToApm.cs" />
<Compile Include="$(CommonTestPath)System\IO\ConnectedStreams.cs" Link="Common\System\IO\ConnectedStreams.cs" />
<Compile Include="$(CommonPath)System\Net\MultiArrayBuffer.cs" Link="ProductionCode\Common\System\Net\MultiArrayBuffer.cs" />
<Compile Include="$(CommonPath)System\Net\StreamBuffer.cs" Link="ProductionCode\Common\System\Net\StreamBuffer.cs" />
</PropertyGroup>
<ItemGroup>
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="Common\System\Threading\Tasks\TaskToApm.cs" />
<Compile Include="Properties\InternalsVisibleTo.cs" />
<Compile Include="System\IO\Pipelines\BufferSegment.cs" />
<Compile Include="System\IO\Pipelines\CompletionData.cs" />
<Compile Include="System\IO\Pipelines\CancellationTokenExtensions.netstandard.cs" />
</ItemGroup>
+ <ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
+ <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToAsyncResult.cs" Link="Common\System\Threading\Tasks\TaskToAsyncResult.cs" />
+ </ItemGroup>
+
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<PackageReference Include="System.Buffers" Version="$(SystemBuffersVersion)" />
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
public sealed override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(ReadAsync(buffer, offset, count, default), callback, state);
+ TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, default), callback, state);
public sealed override int EndRead(IAsyncResult asyncResult) =>
- TaskToApm.End<int>(asyncResult);
+ TaskToAsyncResult.End<int>(asyncResult);
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
public override void SetLength(long value) => throw new NotSupportedException();
public sealed override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(WriteAsync(buffer, offset, count, default), callback, state);
+ TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, default), callback, state);
public sealed override void EndWrite(IAsyncResult asyncResult) =>
- TaskToApm.End(asyncResult);
+ TaskToAsyncResult.End(asyncResult);
public override void Write(byte[] buffer, int offset, int count) =>
WriteAsync(buffer, offset, count).GetAwaiter().GetResult();
<Compile Include="System\IO\Pipes\PipeTransmissionMode.cs" />
<Compile Include="$(CommonPath)DisableRuntimeMarshalling.cs"
Link="Common\DisableRuntimeMarshalling.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="Common\System\Threading\Tasks\TaskToApm.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'windows'">
}
public System.IAsyncResult BeginWaitForConnection(AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(WaitForConnectionAsync(), callback, state);
+ TaskToAsyncResult.Begin(WaitForConnectionAsync(), callback, state);
public void EndWaitForConnection(IAsyncResult asyncResult) =>
- TaskToApm.End(asyncResult);
+ TaskToAsyncResult.End(asyncResult);
// Server can only connect from Disconnected state
private void CheckConnectOperationsServer()
}
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
- => TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state);
+ => TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state);
public override int EndRead(IAsyncResult asyncResult)
- => TaskToApm.End<int>(asyncResult);
+ => TaskToAsyncResult.End<int>(asyncResult);
public override void Write(byte[] buffer, int offset, int count)
{
}
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
- => TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state);
+ => TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state);
public override void EndWrite(IAsyncResult asyncResult)
- => TaskToApm.End(asyncResult);
+ => TaskToAsyncResult.End(asyncResult);
internal static string GetPipePath(string serverName, string pipeName)
{
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
{
if (_isAsync)
- return TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state);
+ return TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state);
else
return base.BeginRead(buffer, offset, count, callback, state);
}
public override int EndRead(IAsyncResult asyncResult)
{
if (_isAsync)
- return TaskToApm.End<int>(asyncResult);
+ return TaskToAsyncResult.End<int>(asyncResult);
else
return base.EndRead(asyncResult);
}
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
{
if (_isAsync)
- return TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state);
+ return TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state);
else
return base.BeginWrite(buffer, offset, count, callback, state);
}
public override void EndWrite(IAsyncResult asyncResult)
{
if (_isAsync)
- TaskToApm.End(asyncResult);
+ TaskToAsyncResult.End(asyncResult);
else
base.EndWrite(asyncResult);
}
Link="Common\Interop\Unix\Interop.IOErrors.cs" />
<Compile Include="$(CommonPath)Interop\Unix\Interop.Poll.Structs.cs"
Link="Common\Interop\Unix\Interop.Poll.Structs.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="Common\System\Threading\Tasks\TaskToApm.cs" />
+ </ItemGroup>
+
+ <ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
+ <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToAsyncResult.cs" Link="Common\System\Threading\Tasks\TaskToAsyncResult.cs" />
</ItemGroup>
<ItemGroup Condition="'$(IsPartialFacadeAssembly)' != 'true'">
public override IAsyncResult BeginRead(byte[] array, int offset, int numBytes, AsyncCallback userCallback, object stateObject)
{
- return TaskToApm.Begin(ReadAsync(array, offset, numBytes), userCallback, stateObject);
+ return TaskToAsyncResult.Begin(ReadAsync(array, offset, numBytes), userCallback, stateObject);
}
// Will wait `timeout` miliseconds or until reading or writing is possible
public override IAsyncResult BeginWrite(byte[] array, int offset, int count, AsyncCallback userCallback, object stateObject)
{
- return TaskToApm.Begin(WriteAsync(array, offset, count), userCallback, stateObject);
+ return TaskToAsyncResult.Begin(WriteAsync(array, offset, count), userCallback, stateObject);
}
public override void EndWrite(IAsyncResult asyncResult)
{
try
{
- return TaskToApm.End<int>(asyncResult);
+ return TaskToAsyncResult.End<int>(asyncResult);
}
catch (OperationCanceledException)
{
<Compile Include="$(CommonTestPath)System\IO\ConnectedStreams.cs" Link="Common\System\IO\ConnectedStreams.cs" />
<Compile Include="$(CommonPath)System\Net\MultiArrayBuffer.cs" Link="ProductionCode\Common\System\Net\MultiArrayBuffer.cs" />
<Compile Include="$(CommonPath)System\Net\StreamBuffer.cs" Link="ProductionCode\Common\System\Net\StreamBuffer.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs" Link="Common\System\Threading\Tasks\TaskToApm.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(CommonTestPath)StreamConformanceTests\StreamConformanceTests.csproj" />
<ItemGroup>
<Compile Include="System\BinaryData.cs" />
<Compile Include="$(CommonPath)System\IO\ReadOnlyMemoryStream.cs" Link="Common\System\IO\ReadOnlyMemoryStream.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs" Link="Common\System\Threading\Tasks\TaskToApm.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\RequiresUnreferencedCodeAttribute.cs" Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'" />
<Compile Include="System\BinaryDataConverter.cs" />
</ItemGroup>
<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\RequiresDynamicCodeAttribute.cs" />
</ItemGroup>
+
+ <ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
+ <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToAsyncResult.cs" Link="Common\System\Threading\Tasks\TaskToAsyncResult.cs" />
+ </ItemGroup>
<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)System.Text.Json\src\System.Text.Json.csproj" />
Link="Common\System\Runtime\ExceptionServices\ExceptionStackTrace.cs" />
<Compile Include="$(CommonPath)\System\Threading\Tasks\RendezvousAwaitable.cs"
Link="Common\System\Threading\Tasks\RendezvousAwaitable.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="Common\System\Threading\Tasks\TaskToApm.cs" />
<Compile Include="System\Net\Http\NetEventSource.WinHttpHandler.cs" />
<Compile Include="System\Net\Http\NoWriteNoSeekStreamContent.cs" />
<Compile Include="System\Net\Http\WinHttpAuthHelper.cs" />
Link="Common\DisableRuntimeMarshalling.cs" />
</ItemGroup>
+ <ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
+ <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToAsyncResult.cs" Link="Common\System\Threading\Tasks\TaskToAsyncResult.cs" />
+ </ItemGroup>
+
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
<PackageReference Include="System.Buffers" Version="$(SystemBuffersVersion)" />
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
}
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) =>
- TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
+ TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
public override void EndWrite(IAsyncResult asyncResult) =>
- TaskToApm.End(asyncResult);
+ TaskToAsyncResult.End(asyncResult);
public override long Seek(long offset, SeekOrigin origin)
{
}
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state);
+ TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state);
public override int EndRead(IAsyncResult asyncResult) =>
- TaskToApm.End<int>(asyncResult);
+ TaskToAsyncResult.End<int>(asyncResult);
private async Task<int> ReadAsyncCore(byte[] buffer, int offset, int count, CancellationToken token)
{
Link="Common\System\Text\SimpleRegex.cs" />
<Compile Include="$(CommonPath)System\Threading\Tasks\RendezvousAwaitable.cs"
Link="Common\System\Threading\Tasks\RendezvousAwaitable.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="Common\System\Threading\Tasks\TaskToApm.cs" />
<Compile Include="..\..\src\System\Net\Http\NoWriteNoSeekStreamContent.cs"
Link="ProductionCode\NoWriteNoSeekStreamContent.cs" />
<Compile Include="..\..\src\System\Net\Http\WinHttpAuthHelper.cs"
Link="Common\System\Net\Security\CertificateHelper.Windows.cs" />
<Compile Include="$(CommonPath)\System\Runtime\ExceptionServices\ExceptionStackTrace.cs"
Link="Common\System\Runtime\ExceptionServices\ExceptionStackTrace.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="Common\System\Threading\Tasks\TaskToApm.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetPlatformIdentifier)' != '' and '$(TargetPlatformIdentifier)' != 'windows' and '$(TargetPlatformIdentifier)' != 'browser'">
<Compile Include="$(CommonPath)System\StrongToWeakReference.cs"
Link="Common\System\Net\Security\CertificateHelper.cs" />
<Compile Include="$(CommonPath)System\Net\Security\CertificateHelper.Unix.cs"
Link="Common\System\Net\Security\CertificateHelper.Unix.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="Common\System\Threading\Tasks\TaskToApm.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetPlatformIdentifier)' != '' and '$(TargetPlatformIdentifier)' != 'windows' and '$(TargetPlatformIdentifier)' != 'browser' and '$(TargetPlatformIdentifier)' != 'osx' and '$(TargetPlatformIdentifier)' != 'ios' and '$(TargetPlatformIdentifier)' != 'tvos'">
<Compile Include="$(CommonPath)Interop\Unix\System.Security.Cryptography.Native\Interop.Initialization.cs"
Link="Common\System\StringExtensions.cs" />
<Compile Include="$(CommonPath)\System\Net\HttpStatusDescription.cs"
Link="Common\System\Net\HttpStatusDescription.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="System\System\Threading\Tasks\TaskToApm.cs" />
<Compile Include="System\Net\Http\SocketsHttpHandler\HttpKeepAlivePingPolicy.cs" />
<Compile Include="System\Net\Http\SocketsHttpHandler\HttpNoProxy.cs" />
<Compile Include="System\Net\Http\SocketsHttpHandler\SocketsHttpConnectionContext.cs" />
public sealed override bool CanSeek => false;
public sealed override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(ReadAsync(buffer, offset, count, default), callback, state);
+ TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, default), callback, state);
public sealed override int EndRead(IAsyncResult asyncResult) =>
- TaskToApm.End<int>(asyncResult);
+ TaskToAsyncResult.End<int>(asyncResult);
public sealed override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(WriteAsync(buffer, offset, count, default), callback, state);
+ TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, default), callback, state);
public sealed override void EndWrite(IAsyncResult asyncResult) =>
- TaskToApm.End(asyncResult);
+ TaskToAsyncResult.End(asyncResult);
public sealed override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
}
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) =>
- TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
+ TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
public override void EndWrite(IAsyncResult asyncResult) =>
- TaskToApm.End(asyncResult);
+ TaskToAsyncResult.End(asyncResult);
public override void WriteByte(byte value)
{
ReadAsyncPrivate(buffer, cancellationToken);
public override IAsyncResult BeginRead(byte[] array, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) =>
- TaskToApm.Begin(ReadAsync(array, offset, count, CancellationToken.None), asyncCallback, asyncState);
+ TaskToAsyncResult.Begin(ReadAsync(array, offset, count, CancellationToken.None), asyncCallback, asyncState);
public override int EndRead(IAsyncResult asyncResult) =>
- TaskToApm.End<int>(asyncResult);
+ TaskToAsyncResult.End<int>(asyncResult);
public async ValueTask<int> ReadAsyncPrivate(Memory<byte> buffer, CancellationToken cancellationToken)
{
Link="Common\Interop\Unix\Interop.Libraries.cs" />
<Compile Include="$(CommonPath)System\Net\Logging\NetEventSource.Common.cs"
Link="Common\System\Net\Logging\NetEventSource.Common.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="System\System\Threading\Tasks\TaskToApm.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Net.Security.Native\Interop.NetSecurityNative.IsNtlmInstalled.cs" Condition="'$(TargetPlatformIdentifier)' != 'windows'"
Link="Common\Interop\Unix\System.Net.Security.Native\Interop.NetSecurityNative.IsNtlmInstalled.cs" />
<Compile Include="$(CommonTestPath)System\Buffers\NativeMemoryManager.cs"
Link="Common\System\Net\Http\HPackEncoder.cs" />
<Compile Include="$(CommonTestPath)System\Net\Http\HuffmanEncoder.cs"
Link="Common\System\Net\Http\HuffmanEncoder.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="ProductionCode\Common\System\Threading\Tasks\TaskToApm.cs" />
<Compile Include="..\..\src\System\Net\Http\SocketsHttpHandler\AuthenticationHelper.Digest.cs"
Link="ProductionCode\System\Net\Http\SocketsHttpHandler\AuthenticationHelper.Digest.cs" />
<Compile Include="..\..\src\System\Net\Http\SocketsHttpHandler\HttpKeepAlivePingPolicy.cs"
<Compile Include="System\Net\Managed\ChunkStream.cs" />
<Compile Include="System\Net\Managed\HttpResponseStream.Managed.cs" />
<Compile Include="System\Net\Managed\WebSockets\HttpWebSocket.Managed.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="Common\System\Threading\Tasks\TaskToApm.cs" />
<Reference Include="System.Net.Sockets" />
<Reference Include="System.Net.ServicePoint" />
<Reference Include="System.Threading.ThreadPool" />
Link="Common\System\Net\Logging\NetEventSource.Common.cs" />
<Compile Include="$(CommonPath)System\Net\InternalException.cs"
Link="Common\System\Net\InternalException.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="Common\System\Threading\Tasks\TaskToApm.cs" />
<!-- System.Net common -->
<Compile Include="$(CommonPath)System\Net\Sockets\ProtocolType.cs"
Link="Common\System\Net\Sockets\ProtocolType.cs" />
}
public static IAsyncResult BeginGetHostEntry(IPAddress address, AsyncCallback? requestCallback, object? stateObject) =>
- TaskToApm.Begin(GetHostEntryAsync(address), requestCallback, stateObject);
+ TaskToAsyncResult.Begin(GetHostEntryAsync(address), requestCallback, stateObject);
public static IAsyncResult BeginGetHostEntry(string hostNameOrAddress, AsyncCallback? requestCallback, object? stateObject) =>
- TaskToApm.Begin(GetHostEntryAsync(hostNameOrAddress), requestCallback, stateObject);
+ TaskToAsyncResult.Begin(GetHostEntryAsync(hostNameOrAddress), requestCallback, stateObject);
public static IPHostEntry EndGetHostEntry(IAsyncResult asyncResult)
{
ArgumentNullException.ThrowIfNull(asyncResult);
- return TaskToApm.End<IPHostEntry>(asyncResult);
+ return TaskToAsyncResult.End<IPHostEntry>(asyncResult);
}
public static IPAddress[] GetHostAddresses(string hostNameOrAddress)
(Task<IPAddress[]>)GetHostEntryOrAddressesCoreAsync(hostNameOrAddress, justReturnParsedIp: true, throwOnIIPAny: true, justAddresses: true, family, cancellationToken);
public static IAsyncResult BeginGetHostAddresses(string hostNameOrAddress, AsyncCallback? requestCallback, object? state) =>
- TaskToApm.Begin(GetHostAddressesAsync(hostNameOrAddress), requestCallback, state);
+ TaskToAsyncResult.Begin(GetHostAddressesAsync(hostNameOrAddress), requestCallback, state);
public static IPAddress[] EndGetHostAddresses(IAsyncResult asyncResult)
{
ArgumentNullException.ThrowIfNull(asyncResult);
- return TaskToApm.End<IPAddress[]>(asyncResult);
+ return TaskToAsyncResult.End<IPAddress[]>(asyncResult);
}
[Obsolete("GetHostByName has been deprecated. Use GetHostEntry instead.")]
[Obsolete("BeginGetHostByName has been deprecated. Use BeginGetHostEntry instead.")]
public static IAsyncResult BeginGetHostByName(string hostName, AsyncCallback? requestCallback, object? stateObject) =>
- TaskToApm.Begin(GetHostEntryCoreAsync(hostName, justReturnParsedIp: true, throwOnIIPAny: true, AddressFamily.Unspecified, CancellationToken.None), requestCallback, stateObject);
+ TaskToAsyncResult.Begin(GetHostEntryCoreAsync(hostName, justReturnParsedIp: true, throwOnIIPAny: true, AddressFamily.Unspecified, CancellationToken.None), requestCallback, stateObject);
[Obsolete("EndGetHostByName has been deprecated. Use EndGetHostEntry instead.")]
public static IPHostEntry EndGetHostByName(IAsyncResult asyncResult)
{
ArgumentNullException.ThrowIfNull(asyncResult);
- return TaskToApm.End<IPHostEntry>(asyncResult);
+ return TaskToAsyncResult.End<IPHostEntry>(asyncResult);
}
[Obsolete("GetHostByAddress has been deprecated. Use GetHostEntry instead.")]
[Obsolete("BeginResolve has been deprecated. Use BeginGetHostEntry instead.")]
public static IAsyncResult BeginResolve(string hostName, AsyncCallback? requestCallback, object? stateObject) =>
- TaskToApm.Begin(GetHostEntryCoreAsync(hostName, justReturnParsedIp: false, throwOnIIPAny: false, AddressFamily.Unspecified, CancellationToken.None), requestCallback, stateObject);
+ TaskToAsyncResult.Begin(GetHostEntryCoreAsync(hostName, justReturnParsedIp: false, throwOnIIPAny: false, AddressFamily.Unspecified, CancellationToken.None), requestCallback, stateObject);
[Obsolete("EndResolve has been deprecated. Use EndGetHostEntry instead.")]
public static IPHostEntry EndResolve(IAsyncResult asyncResult)
try
{
- ipHostEntry = TaskToApm.End<IPHostEntry>(asyncResult);
+ ipHostEntry = TaskToAsyncResult.End<IPHostEntry>(asyncResult);
}
catch (SocketException ex)
{
- object? asyncState = asyncResult switch
- {
- Task t => t.AsyncState,
- TaskToApm.TaskAsyncResult twar => twar._task.AsyncState,
- _ => null
- };
+ object? asyncState = TaskToAsyncResult.Unwrap(asyncResult).AsyncState;
IPAddress? address = asyncState switch
{
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-linux;$(NetCoreAppCurrent)-android;$(NetCoreAppCurrent)-osx;$(NetCoreAppCurrent)-ios;$(NetCoreAppCurrent)-tvos;$(NetCoreAppCurrent)-freebsd;$(NetCoreAppCurrent)-illumos;$(NetCoreAppCurrent)-solaris;$(NetCoreAppCurrent)</TargetFrameworks>
<Compile Include="$(CommonPath)System\Net\NetworkInformation\HostInformation.cs" Link="Common\System\Net\NetworkInformation\HostInformation.cs" />
<Compile Include="$(CommonPath)System\Net\NetworkInformation\NetworkInformationException.cs" Link="Common\System\Net\NetworkInformation\NetworkInformationException.cs" />
<Compile Include="$(CommonPath)System\HexConverter.cs" Link="Common\System\HexConverter.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs" Link="Common\System\Threading\Tasks\TaskToApm.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'windows'">
<!-- Interop -->
}
public override IAsyncResult BeginGetUnicastAddresses(AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(GetUnicastAddressesAsync(), callback, state);
+ TaskToAsyncResult.Begin(GetUnicastAddressesAsync(), callback, state);
public override UnicastIPAddressInformationCollection EndGetUnicastAddresses(IAsyncResult asyncResult) =>
- TaskToApm.End<UnicastIPAddressInformationCollection>(asyncResult);
+ TaskToAsyncResult.End<UnicastIPAddressInformationCollection>(asyncResult);
public override UnicastIPAddressInformationCollection GetUnicastAddresses() =>
GetUnicastAddressesAsync().GetAwaiter().GetResult();
public override IAsyncResult BeginGetUnicastAddresses(AsyncCallback? callback, object? state)
{
Task<UnicastIPAddressInformationCollection> t = GetUnicastAddressesAsync();
- return TaskToApm.Begin(t, callback, state);
+ return TaskToAsyncResult.Begin(t, callback, state);
}
public override UnicastIPAddressInformationCollection EndGetUnicastAddresses(IAsyncResult asyncResult)
{
- return TaskToApm.End<UnicastIPAddressInformationCollection>(asyncResult);
+ return TaskToAsyncResult.End<UnicastIPAddressInformationCollection>(asyncResult);
}
public sealed override Task<UnicastIPAddressInformationCollection> GetUnicastAddressesAsync()
<Compile Include="System\Net\Quic\**\*.cs" Exclude="System\Net\Quic\*.Unsupported.cs"/>
<!-- System.Net common -->
<Compile Include="$(CommonPath)DisableRuntimeMarshalling.cs" Link="Common\DisableRuntimeMarshalling.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs" Link="Common\System\Threading\Tasks\TaskToApm.cs" />
<Compile Include="$(CommonPath)System\Net\ArrayBuffer.cs" Link="Common\System\Net\ArrayBuffer.cs" />
<Compile Include="$(CommonPath)System\Net\MultiArrayBuffer.cs" Link="Common\System\Net\MultiArrayBuffer.cs" />
<Compile Include="$(CommonPath)System\Net\Logging\NetEventSource.Common.cs" Link="Common\System\Net\Logging\NetEventSource.Common.cs" />
/// <inheritdoc />
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
- => TaskToApm.Begin(ReadAsync(buffer, offset, count, default), callback, state);
+ => TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, default), callback, state);
/// <inheritdoc />
public override int EndRead(IAsyncResult asyncResult)
- => TaskToApm.End<int>(asyncResult);
+ => TaskToAsyncResult.End<int>(asyncResult);
/// <inheritdoc />
public override int Read(byte[] buffer, int offset, int count)
/// <inheritdoc />
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
- => TaskToApm.Begin(WriteAsync(buffer, offset, count, default), callback, state);
+ => TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, default), callback, state);
/// <inheritdoc />
public override void EndWrite(IAsyncResult asyncResult)
- => TaskToApm.End(asyncResult);
+ => TaskToAsyncResult.End(asyncResult);
/// <inheritdoc />
public override void Write(byte[] buffer, int offset, int count)
<Compile Include="$(CommonPath)System\Net\ArrayBuffer.cs" Link="ProductionCode\Common\System\Net\ArrayBuffer.cs" />
<Compile Include="$(CommonPath)System\Net\MultiArrayBuffer.cs" Link="ProductionCode\Common\System\Net\MultiArrayBuffer.cs" />
<Compile Include="$(CommonPath)System\Net\StreamBuffer.cs" Link="ProductionCode\Common\System\Net\StreamBuffer.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs" Link="Common\System\Threading\Tasks\TaskToApm.cs" />
<Compile Include="$(CommonTestPath)System\IO\ConnectedStreams.cs" Link="Common\System\IO\ConnectedStreams.cs" />
<Compile Include="$(CommonTestPath)System\Net\Capability.Security.cs" Link="Common\System\Net\Capability.Security.cs" />
<Compile Include="$(CommonTestPath)System\Net\Configuration.cs" Link="Common\System\Net\Configuration.cs" />
Link="Common\System\Net\SecurityProtocol.cs" />
<Compile Include="$(CommonPath)System\NotImplemented.cs"
Link="Common\System\NotImplemented.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="Common\System\Threading\Tasks\TaskToApm.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'windows'">
<Compile Include="$(CommonPath)Interop\Windows\WinInet\Interop.wininet_errors.cs"
CheckAndMarkAsyncGetRequestStreamPending();
Task<Stream> t = Task.Factory.StartNew<Stream>(s => ((FileWebRequest)s!).CreateWriteStream(),
this, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
- return TaskToApm.Begin(t, callback, state);
+ return TaskToAsyncResult.Begin(t, callback, state);
}
public override Task<Stream> GetRequestStreamAsync()
CheckAndMarkAsyncGetResponsePending();
Task<WebResponse> t = Task.Factory.StartNew(s => ((FileWebRequest)s!).CreateResponse(),
this, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
- return TaskToApm.Begin(t, callback, state);
+ return TaskToAsyncResult.Begin(t, callback, state);
}
public override Task<WebResponse> GetResponseAsync()
public override Stream EndGetRequestStream(IAsyncResult asyncResult)
{
- Stream stream = TaskToApm.End<Stream>(asyncResult);
+ Stream stream = TaskToAsyncResult.End<Stream>(asyncResult);
_writePending = false;
return stream;
}
public override WebResponse EndGetResponse(IAsyncResult asyncResult)
{
- WebResponse response = TaskToApm.End<WebResponse>(asyncResult);
+ WebResponse response = TaskToAsyncResult.End<WebResponse>(asyncResult);
_readPending = false;
return response;
}
<!-- Common -->
<Compile Include="$(CommonPath)System\NotImplemented.cs"
Link="Common\System\NotImplemented.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="Common\System\Threading\Tasks\TaskToApm.cs" />
<Compile Include="$(CommonPath)System\Net\Security\TlsAlertMessage.cs"
Link="Common\System\Net\Security\TlsAlertMessage.cs" />
<Compile Include="$(CommonPath)System\Net\Security\SafeCredentialReference.cs"
public virtual IAsyncResult BeginAuthenticateAsClient(
NetworkCredential credential, ChannelBinding? binding, string targetName, ProtectionLevel requiredProtectionLevel, TokenImpersonationLevel allowedImpersonationLevel,
AsyncCallback? asyncCallback, object? asyncState) =>
- TaskToApm.Begin(AuthenticateAsClientAsync(credential, binding, targetName, requiredProtectionLevel, allowedImpersonationLevel), asyncCallback, asyncState);
+ TaskToAsyncResult.Begin(AuthenticateAsClientAsync(credential, binding, targetName, requiredProtectionLevel, allowedImpersonationLevel), asyncCallback, asyncState);
- public virtual void EndAuthenticateAsClient(IAsyncResult asyncResult) => TaskToApm.End(asyncResult);
+ public virtual void EndAuthenticateAsClient(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult);
public virtual void AuthenticateAsServer() =>
AuthenticateAsServer((NetworkCredential)CredentialCache.DefaultCredentials, policy: null, ProtectionLevel.EncryptAndSign, TokenImpersonationLevel.Identification);
public virtual IAsyncResult BeginAuthenticateAsServer(
NetworkCredential credential, ExtendedProtectionPolicy? policy, ProtectionLevel requiredProtectionLevel, TokenImpersonationLevel requiredImpersonationLevel,
AsyncCallback? asyncCallback, object? asyncState) =>
- TaskToApm.Begin(AuthenticateAsServerAsync(credential, policy, requiredProtectionLevel, requiredImpersonationLevel), asyncCallback, asyncState);
+ TaskToAsyncResult.Begin(AuthenticateAsServerAsync(credential, policy, requiredProtectionLevel, requiredImpersonationLevel), asyncCallback, asyncState);
- public virtual void EndAuthenticateAsServer(IAsyncResult asyncResult) => TaskToApm.End(asyncResult);
+ public virtual void EndAuthenticateAsServer(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult);
public virtual void AuthenticateAsClient() =>
AuthenticateAsClient((NetworkCredential)CredentialCache.DefaultCredentials, binding: null, string.Empty, ProtectionLevel.EncryptAndSign, TokenImpersonationLevel.Identification);
}
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) =>
- TaskToApm.Begin(ReadAsync(buffer, offset, count), asyncCallback, asyncState);
+ TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count), asyncCallback, asyncState);
public override int EndRead(IAsyncResult asyncResult) =>
- TaskToApm.End<int>(asyncResult);
+ TaskToAsyncResult.End<int>(asyncResult);
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState) =>
- TaskToApm.Begin(WriteAsync(buffer, offset, count), asyncCallback, asyncState);
+ TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count), asyncCallback, asyncState);
public override void EndWrite(IAsyncResult asyncResult) =>
- TaskToApm.End(asyncResult);
+ TaskToAsyncResult.End(asyncResult);
private void ThrowIfExceptional()
{
}
internal IAsyncResult BeginAuthenticateAsClient(SslClientAuthenticationOptions sslClientAuthenticationOptions, CancellationToken cancellationToken, AsyncCallback? asyncCallback, object? asyncState) =>
- TaskToApm.Begin(AuthenticateAsClientAsync(sslClientAuthenticationOptions, cancellationToken)!, asyncCallback, asyncState);
+ TaskToAsyncResult.Begin(AuthenticateAsClientAsync(sslClientAuthenticationOptions, cancellationToken)!, asyncCallback, asyncState);
- public virtual void EndAuthenticateAsClient(IAsyncResult asyncResult) => TaskToApm.End(asyncResult);
+ public virtual void EndAuthenticateAsClient(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult);
//
// Server side auth.
}
private IAsyncResult BeginAuthenticateAsServer(SslServerAuthenticationOptions sslServerAuthenticationOptions, CancellationToken cancellationToken, AsyncCallback? asyncCallback, object? asyncState) =>
- TaskToApm.Begin(AuthenticateAsServerAsync(sslServerAuthenticationOptions, cancellationToken)!, asyncCallback, asyncState);
+ TaskToAsyncResult.Begin(AuthenticateAsServerAsync(sslServerAuthenticationOptions, cancellationToken)!, asyncCallback, asyncState);
- public virtual void EndAuthenticateAsServer(IAsyncResult asyncResult) => TaskToApm.End(asyncResult);
+ public virtual void EndAuthenticateAsServer(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult);
- internal IAsyncResult BeginShutdown(AsyncCallback? asyncCallback, object? asyncState) => TaskToApm.Begin(ShutdownAsync(), asyncCallback, asyncState);
+ internal IAsyncResult BeginShutdown(AsyncCallback? asyncCallback, object? asyncState) => TaskToAsyncResult.Begin(ShutdownAsync(), asyncCallback, asyncState);
- internal static void EndShutdown(IAsyncResult asyncResult) => TaskToApm.End(asyncResult);
+ internal static void EndShutdown(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult);
public TransportContext TransportContext => new SslStreamContext(this);
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState)
{
ThrowIfExceptionalOrNotAuthenticated();
- return TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
+ return TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
}
public override int EndRead(IAsyncResult asyncResult)
{
ThrowIfExceptionalOrNotAuthenticated();
- return TaskToApm.End<int>(asyncResult);
+ return TaskToAsyncResult.End<int>(asyncResult);
}
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState)
{
ThrowIfExceptionalOrNotAuthenticated();
- return TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
+ return TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
}
public override void EndWrite(IAsyncResult asyncResult)
{
ThrowIfExceptionalOrNotAuthenticated();
- TaskToApm.End(asyncResult);
+ TaskToAsyncResult.End(asyncResult);
}
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Link="Common\System\Net\EnterpriseTests\EnterpriseTestConfiguration.cs" />
<Compile Include="$(CommonTestPath)System\Threading\Tasks\TaskTimeoutExtensions.cs"
Link="Common\System\Threading\Tasks\TaskTimeoutExtensions.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="ProductionCode\Common\System\Threading\Tasks\TaskToApm.cs" />
</ItemGroup>
-</Project>
\ No newline at end of file
+</Project>
Link="CommonTest\System\Security\Cryptography\X509Certificates\RevocationResponder.cs" />
<Compile Include="$(CommonTestPath)System\Threading\Tasks\TaskTimeoutExtensions.cs"
Link="Common\System\Threading\Tasks\TaskTimeoutExtensions.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="ProductionCode\Common\System\Threading\Tasks\TaskToApm.cs" />
<Compile Include="$(CommonPath)System\IO\DelegatingStream.cs"
Link="ProductionCode\Common\System\IO\DelegatingStream.cs" />
<Compile Include="$(CommonPath)System\Net\ArrayBuffer.cs"
Link="ProductionCode\Common\System\Net\TlsFrameHelper.cs" />
<Compile Include="$(CommonPath)Interop\Windows\SChannel\Interop.Alerts.cs"
Link="Common\Interop\Windows\SChannel\Interop.Alerts.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="Common\System\Threading\Tasks\TaskToApm.cs" />
<Compile Include="$(CommonPath)System\Obsoletions.cs"
Link="Common\System\Obsoletions.cs" />
<!-- IP parser -->
Link="Common\System\Net\TcpValidationHelpers.cs" />
<Compile Include="$(CommonPath)System\Net\SocketProtocolSupportPal.cs"
Link="Common\System\Net\SocketProtocolSupportPal.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="Common\System\Threading\Tasks\TaskToApm.cs" />
<!-- System.Net.Internals -->
<Compile Include="$(CommonPath)System\Net\Internals\IPEndPointExtensions.cs"
Link="Common\System\Net\Internals\IPEndPointExtensions.cs" />
}
public IAsyncResult BeginConnect(EndPoint remoteEP, AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(ConnectAsync(remoteEP), callback, state);
+ TaskToAsyncResult.Begin(ConnectAsync(remoteEP), callback, state);
public IAsyncResult BeginConnect(string host, int port, AsyncCallback? requestCallback, object? state) =>
- TaskToApm.Begin(ConnectAsync(host, port), requestCallback, state);
+ TaskToAsyncResult.Begin(ConnectAsync(host, port), requestCallback, state);
public IAsyncResult BeginConnect(IPAddress address, int port, AsyncCallback? requestCallback, object? state) =>
- TaskToApm.Begin(ConnectAsync(address, port), requestCallback, state);
+ TaskToAsyncResult.Begin(ConnectAsync(address, port), requestCallback, state);
public IAsyncResult BeginConnect(IPAddress[] addresses, int port, AsyncCallback? requestCallback, object? state) =>
- TaskToApm.Begin(ConnectAsync(addresses, port), requestCallback, state);
+ TaskToAsyncResult.Begin(ConnectAsync(addresses, port), requestCallback, state);
- public void EndConnect(IAsyncResult asyncResult) => TaskToApm.End(asyncResult);
+ public void EndConnect(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult);
public IAsyncResult BeginDisconnect(bool reuseSocket, AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(DisconnectAsync(reuseSocket).AsTask(), callback, state);
+ TaskToAsyncResult.Begin(DisconnectAsync(reuseSocket).AsTask(), callback, state);
public void Disconnect(bool reuseSocket)
{
_localEndPoint = null;
}
- public void EndDisconnect(IAsyncResult asyncResult) => TaskToApm.End(asyncResult);
+ public void EndDisconnect(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult);
public IAsyncResult BeginSend(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback? callback, object? state)
{
ThrowIfDisposed();
ValidateBufferArguments(buffer, offset, size);
- return TaskToApm.Begin(SendAsync(new ReadOnlyMemory<byte>(buffer, offset, size), socketFlags, default).AsTask(), callback, state);
+ return TaskToAsyncResult.Begin(SendAsync(new ReadOnlyMemory<byte>(buffer, offset, size), socketFlags, default).AsTask(), callback, state);
}
public IAsyncResult? BeginSend(byte[] buffer, int offset, int size, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback? callback, object? state)
}
errorCode = SocketError.Success;
- return TaskToApm.Begin(t, callback, state);
+ return TaskToAsyncResult.Begin(t, callback, state);
}
public IAsyncResult BeginSend(IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, AsyncCallback? callback, object? state)
{
ThrowIfDisposed();
- return TaskToApm.Begin(SendAsync(buffers, socketFlags), callback, state);
+ return TaskToAsyncResult.Begin(SendAsync(buffers, socketFlags), callback, state);
}
public IAsyncResult? BeginSend(IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback? callback, object? state)
}
errorCode = SocketError.Success;
- return TaskToApm.Begin(t, callback, state);
+ return TaskToAsyncResult.Begin(t, callback, state);
}
- public int EndSend(IAsyncResult asyncResult) => TaskToApm.End<int>(asyncResult);
+ public int EndSend(IAsyncResult asyncResult) => TaskToAsyncResult.End<int>(asyncResult);
public int EndSend(IAsyncResult asyncResult, out SocketError errorCode) =>
EndSendReceive(asyncResult, out errorCode);
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"::DoBeginSendFile() SRC:{LocalEndPoint} DST:{RemoteEndPoint} fileName:{fileName}");
- return TaskToApm.Begin(SendFileAsync(fileName, preBuffer, postBuffer, flags).AsTask(), callback, state);
+ return TaskToAsyncResult.Begin(SendFileAsync(fileName, preBuffer, postBuffer, flags).AsTask(), callback, state);
}
- public void EndSendFile(IAsyncResult asyncResult) => TaskToApm.End(asyncResult);
+ public void EndSendFile(IAsyncResult asyncResult) => TaskToAsyncResult.End(asyncResult);
public IAsyncResult BeginSendTo(byte[] buffer, int offset, int size, SocketFlags socketFlags, EndPoint remoteEP, AsyncCallback? callback, object? state)
{
ArgumentNullException.ThrowIfNull(remoteEP);
Task<int> t = SendToAsync(buffer.AsMemory(offset, size), socketFlags, remoteEP).AsTask();
- return TaskToApm.Begin(t, callback, state);
+ return TaskToAsyncResult.Begin(t, callback, state);
}
- public int EndSendTo(IAsyncResult asyncResult) => TaskToApm.End<int>(asyncResult);
+ public int EndSendTo(IAsyncResult asyncResult) => TaskToAsyncResult.End<int>(asyncResult);
public IAsyncResult BeginReceive(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback? callback, object? state)
{
ThrowIfDisposed();
ValidateBufferArguments(buffer, offset, size);
- return TaskToApm.Begin(ReceiveAsync(new ArraySegment<byte>(buffer, offset, size), socketFlags, fromNetworkStream: false, default).AsTask(), callback, state);
+ return TaskToAsyncResult.Begin(ReceiveAsync(new ArraySegment<byte>(buffer, offset, size), socketFlags, fromNetworkStream: false, default).AsTask(), callback, state);
}
public IAsyncResult? BeginReceive(byte[] buffer, int offset, int size, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback? callback, object? state)
}
errorCode = SocketError.Success;
- return TaskToApm.Begin(t, callback, state);
+ return TaskToAsyncResult.Begin(t, callback, state);
}
public IAsyncResult BeginReceive(IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, AsyncCallback? callback, object? state)
{
ThrowIfDisposed();
- return TaskToApm.Begin(ReceiveAsync(buffers, socketFlags), callback, state);
+ return TaskToAsyncResult.Begin(ReceiveAsync(buffers, socketFlags), callback, state);
}
public IAsyncResult? BeginReceive(IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback? callback, object? state)
}
errorCode = SocketError.Success;
- return TaskToApm.Begin(t, callback, state);
+ return TaskToAsyncResult.Begin(t, callback, state);
}
- public int EndReceive(IAsyncResult asyncResult) => TaskToApm.End<int>(asyncResult);
+ public int EndReceive(IAsyncResult asyncResult) => TaskToAsyncResult.End<int>(asyncResult);
public int EndReceive(IAsyncResult asyncResult, out SocketError errorCode) =>
EndSendReceive(asyncResult, out errorCode);
private static int EndSendReceive(IAsyncResult asyncResult, out SocketError errorCode)
{
- if (TaskToApm.GetTask(asyncResult) is not Task<int> ti)
- {
- ArgumentNullException.ThrowIfNull(asyncResult);
- throw new ArgumentException(null, nameof(asyncResult));
- }
+ Task<int> ti = TaskToAsyncResult.Unwrap<int>(asyncResult);
if (!ti.IsCompleted)
{
EndPoint resultEp = t.Result.RemoteEndPoint;
if (!remoteEP.Equals(resultEp)) remoteEP = resultEp;
}
- IAsyncResult asyncResult = TaskToApm.Begin(t, callback, state);
+ IAsyncResult asyncResult = TaskToAsyncResult.Begin(t, callback, state);
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"size:{size} returning AsyncResult:{asyncResult}");
return asyncResult;
}
throw new ArgumentException(SR.Format(SR.net_InvalidEndPointAddressFamily, endPoint.AddressFamily, _addressFamily), nameof(endPoint));
}
- SocketReceiveMessageFromResult result = TaskToApm.End<SocketReceiveMessageFromResult>(asyncResult);
+ SocketReceiveMessageFromResult result = TaskToAsyncResult.End<SocketReceiveMessageFromResult>(asyncResult);
if (!endPoint.Equals(result.RemoteEndPoint))
{
endPoint = result.RemoteEndPoint;
if (!remoteEP.Equals(resultEp)) remoteEP = resultEp;
}
- return TaskToApm.Begin(t, callback, state);
+ return TaskToAsyncResult.Begin(t, callback, state);
}
public int EndReceiveFrom(IAsyncResult asyncResult, ref EndPoint endPoint)
throw new ArgumentException(SR.Format(SR.net_InvalidEndPointAddressFamily, endPoint.AddressFamily, _addressFamily), nameof(endPoint));
}
- SocketReceiveFromResult result = TaskToApm.End<SocketReceiveFromResult>(asyncResult);
+ SocketReceiveFromResult result = TaskToAsyncResult.End<SocketReceiveFromResult>(asyncResult);
if (!endPoint.Equals(result.RemoteEndPoint))
{
endPoint = result.RemoteEndPoint;
}
public IAsyncResult BeginAccept(AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(AcceptAsync(), callback, state);
+ TaskToAsyncResult.Begin(AcceptAsync(), callback, state);
- public Socket EndAccept(IAsyncResult asyncResult) => TaskToApm.End<Socket>(asyncResult);
+ public Socket EndAccept(IAsyncResult asyncResult) => TaskToAsyncResult.End<Socket>(asyncResult);
// This method provides support for legacy BeginAccept methods that take a "receiveSize" argument and
// allow data to be received as part of the accept operation.
BeginAccept(acceptSocket: null, receiveSize, callback, state);
public IAsyncResult BeginAccept(Socket? acceptSocket, int receiveSize, AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(AcceptAndReceiveHelperAsync(acceptSocket, receiveSize), callback, state);
+ TaskToAsyncResult.Begin(AcceptAndReceiveHelperAsync(acceptSocket, receiveSize), callback, state);
public Socket EndAccept(out byte[] buffer, IAsyncResult asyncResult)
{
public Socket EndAccept(out byte[] buffer, out int bytesTransferred, IAsyncResult asyncResult)
{
Socket s;
- (s, buffer, bytesTransferred) = TaskToApm.End<(Socket, byte[], int)>(asyncResult);
+ (s, buffer, bytesTransferred) = TaskToAsyncResult.End<(Socket, byte[], int)>(asyncResult);
return s;
}
}
public IAsyncResult BeginAcceptSocket(AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(AcceptSocketAsync(), callback, state);
+ TaskToAsyncResult.Begin(AcceptSocketAsync(), callback, state);
public Socket EndAcceptSocket(IAsyncResult asyncResult) =>
EndAcceptCore<Socket>(asyncResult);
public IAsyncResult BeginAcceptTcpClient(AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(AcceptTcpClientAsync(), callback, state);
+ TaskToAsyncResult.Begin(AcceptTcpClientAsync(), callback, state);
public TcpClient EndAcceptTcpClient(IAsyncResult asyncResult) =>
EndAcceptCore<TcpClient>(asyncResult);
{
try
{
- return TaskToApm.End<TResult>(asyncResult);
+ return TaskToAsyncResult.End<TResult>(asyncResult);
}
catch (SocketException) when (!_active)
{
Link="Common\System\Threading\Tasks\TaskTimeoutExtensions.cs" />
<Compile Include="$(CommonTestPath)System\Buffers\NativeMemoryManager.cs"
Link="Common\System\Buffers\NativeMemoryManager.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="Common\System\Threading\Tasks\TaskToApm.cs" />
<Compile Include="$(CommonTestPath)System\Diagnostics\Tracing\TestEventListener.cs"
Link="Common\System\Diagnostics\Tracing\TestEventListener.cs" />
<Compile Include="$(CommonPath)System\Net\Logging\NetEventSource.Common.cs"
<Compile Include="$(CommonPath)System\Threading\OpenExistingResult.cs">
<Link>Common\System\Threading\OpenExistingResult.cs</Link>
</Compile>
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs">
- <Link>Common\System\Threading\Tasks\TaskToApm.cs</Link>
+ <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToAsyncResult.cs">
+ <Link>Common\System\Threading\Tasks\TaskToAsyncResult.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup Condition="'$(IsMobileLike)' != 'true' and '$(FeatureNativeAot)' != 'true'">
}
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state);
+ TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state);
public override int EndRead(IAsyncResult asyncResult) =>
- TaskToApm.End<int>(asyncResult);
+ TaskToAsyncResult.End<int>(asyncResult);
public override int ReadByte()
{
}
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state);
+ TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state);
public override void EndWrite(IAsyncResult asyncResult) =>
- TaskToApm.End(asyncResult);
+ TaskToAsyncResult.End(asyncResult);
public override void WriteByte(byte value)
{
}
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
- => TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state);
+ => TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state);
public override int EndRead(IAsyncResult asyncResult)
- => TaskToApm.End<int>(asyncResult);
+ => TaskToAsyncResult.End<int>(asyncResult);
public override void Write(byte[] buffer, int offset, int count)
{
}
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
- => TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state);
+ => TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state);
public override void EndWrite(IAsyncResult asyncResult)
- => TaskToApm.End(asyncResult);
+ => TaskToAsyncResult.End(asyncResult);
public override void SetLength(long value)
{
}
public sealed override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(WriteAsync(buffer, offset, count), callback, state);
+ TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count), callback, state);
public sealed override void EndWrite(IAsyncResult asyncResult) =>
- TaskToApm.End(asyncResult);
+ TaskToAsyncResult.End(asyncResult);
public sealed override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) =>
WriteAsync(new ReadOnlyMemory<byte>(buffer, offset, count), cancellationToken).AsTask();
}
public sealed override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(ReadAsync(buffer, offset, count), callback, state);
+ TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count), callback, state);
public sealed override int EndRead(IAsyncResult asyncResult) =>
- TaskToApm.End<int>(asyncResult);
+ TaskToAsyncResult.End<int>(asyncResult);
public sealed override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) =>
ReadAsync(new Memory<byte>(buffer, offset, count), cancellationToken).AsTask();
Task.CompletedTask;
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(Task<int>.s_defaultResultTask, callback, state);
+ TaskToAsyncResult.Begin(Task<int>.s_defaultResultTask, callback, state);
public override int EndRead(IAsyncResult asyncResult) =>
- TaskToApm.End<int>(asyncResult);
+ TaskToAsyncResult.End<int>(asyncResult);
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(Task.CompletedTask, callback, state);
+ TaskToAsyncResult.Begin(Task.CompletedTask, callback, state);
public override void EndWrite(IAsyncResult asyncResult) =>
- TaskToApm.End(asyncResult);
+ TaskToAsyncResult.End(asyncResult);
public override int Read(byte[] buffer, int offset, int count) => 0;
}
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
- => TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state);
+ => TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state);
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
- => TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state);
+ => TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state);
protected override void Dispose(bool disposing)
{
}
public override int EndRead(IAsyncResult asyncResult)
- => TaskToApm.End<int>(asyncResult);
+ => TaskToAsyncResult.End<int>(asyncResult);
public override void EndWrite(IAsyncResult asyncResult)
- => TaskToApm.End(asyncResult);
+ => TaskToAsyncResult.End(asyncResult);
#pragma warning disable CS3016 // Arrays as attribute arguments is not CLS-compliant
#pragma warning disable CS8774 // Member must have a non-null value when exiting.
// Changes to this file must follow the https://aka.ms/api-review process.
// ------------------------------------------------------------------------------
-using System.Text;
-
namespace Microsoft.Win32.SafeHandles
{
public abstract partial class CriticalHandleMinusOneIsInvalid : System.Runtime.InteropServices.CriticalHandle
public new System.Threading.Tasks.Task<TResult> WaitAsync(System.TimeSpan timeout) { throw null; }
public new System.Threading.Tasks.Task<TResult> WaitAsync(System.TimeSpan timeout, System.Threading.CancellationToken cancellationToken) { throw null; }
}
+ public static partial class TaskToAsyncResult
+ {
+ public static System.IAsyncResult Begin(System.Threading.Tasks.Task task, System.AsyncCallback? callback, object? state) { throw null; }
+ public static void End(System.IAsyncResult asyncResult) { throw null; }
+ public static TResult End<TResult>(System.IAsyncResult asyncResult) { throw null; }
+ public static System.Threading.Tasks.Task Unwrap(System.IAsyncResult asyncResult) { throw null; }
+ public static System.Threading.Tasks.Task<TResult> Unwrap<TResult>(System.IAsyncResult asyncResult) { throw null; }
+ }
public partial class UnobservedTaskExceptionEventArgs : System.EventArgs
{
public UnobservedTaskExceptionEventArgs(System.AggregateException exception) { }
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DefineConstants>$(DefineConstants);INTERNAL_ASYMMETRIC_IMPLEMENTATIONS</DefineConstants>
Link="Common\System\Security\Cryptography\Utf8DataEncoding.cs" />
<Compile Include="$(CommonPath)System\Text\ValueStringBuilder.cs"
Link="Common\System\Text\ValueStringBuilder.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="Common\System\Threading\Tasks\TaskToApm.cs" />
<Compile Include="Microsoft\Win32\SafeHandles\SafePasswordHandle.cs" />
<Compile Include="System\Security\Cryptography\Aes.cs" />
<Compile Include="System\Security\Cryptography\AesAEAD.cs" />
}
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state);
+ TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state);
public override int EndRead(IAsyncResult asyncResult) =>
- TaskToApm.End<int>(asyncResult);
+ TaskToAsyncResult.End<int>(asyncResult);
public override int ReadByte()
{
}
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
- TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state);
+ TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), callback, state);
public override void EndWrite(IAsyncResult asyncResult) =>
- TaskToApm.End(asyncResult);
+ TaskToAsyncResult.End(asyncResult);
public override void Write(byte[] buffer, int offset, int count)
{
Link="CommonTest\System\Security\Cryptography\CryptoUtils.cs" />
<Compile Include="$(CommonTestPath)System\Security\Cryptography\PlatformSupport.cs"
Link="CommonTest\System\Security\Cryptography\PlatformSupport.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
- Link="Common\System\Threading\Tasks\TaskToApm.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="AesCcmTests.cs" />
<Compile Include="UnicodeEncoding\UnicodeEncoding.cs" />
<Compile Include="Decoder\Decoder.cs" />
<Compile Include="Encoder\Encoder.cs" />
- <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs" Link="Common\System\Threading\Tasks\TaskToApm.cs" />
<Compile Include="$(CommonTestPath)System\IO\ConnectedStreams.cs" Link="Common\System\IO\ConnectedStreams.cs" />
<Compile Include="$(CommonPath)System\Net\ArrayBuffer.cs" Link="ProductionCode\Common\System\Net\ArrayBuffer.cs" />
<Compile Include="$(CommonPath)System\Net\MultiArrayBuffer.cs" Link="ProductionCode\Common\System\Net\MultiArrayBuffer.cs" />
<ProjectReference Include="$(CommonTestPath)StreamConformanceTests\StreamConformanceTests.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.IO.Pipelines\src\System.IO.Pipelines.csproj" />
</ItemGroup>
-</Project>
\ No newline at end of file
+</Project>
<Compile Include="CancellationTokenTests.cs" />
<Compile Include="MethodCoverage.cs" />
<Compile Include="CESchedulerPairTests.cs" />
+ <Compile Include="TaskToAsyncResultTests.cs" />
<!-- Task -->
<Compile Include="Task\AsyncEnumerableToBlockingEnumerableTests.cs" />
<Compile Include="Task\ExecutionContextFlowTest.cs" />
--- /dev/null
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using Xunit;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace System.Threading.Tasks.Tests
+{
+ public class TaskToAsyncResultTests
+ {
+ [Fact]
+ public void InvalidArguments_ThrowExceptions()
+ {
+ AssertExtensions.Throws<ArgumentNullException>("task", () => TaskToAsyncResult.Begin(null, null, null));
+ AssertExtensions.Throws<ArgumentNullException>("task", () => TaskToAsyncResult.Begin(null, iar => { }, "test"));
+
+ AssertExtensions.Throws<ArgumentNullException>("asyncResult", () => TaskToAsyncResult.End(null));
+ AssertExtensions.Throws<ArgumentNullException>("asyncResult", () => TaskToAsyncResult.End<int>(null));
+
+ AssertExtensions.Throws<ArgumentException>("asyncResult", () => TaskToAsyncResult.End(new NonTaskIAsyncResult()));
+ AssertExtensions.Throws<ArgumentException>("asyncResult", () => TaskToAsyncResult.End<int>(new NonTaskIAsyncResult()));
+ AssertExtensions.Throws<ArgumentException>("asyncResult", () => TaskToAsyncResult.End<int>(Task.FromResult((long)42)));
+
+ AssertExtensions.Throws<ArgumentException>("asyncResult", () => TaskToAsyncResult.Unwrap(new NonTaskIAsyncResult()));
+ AssertExtensions.Throws<ArgumentException>("asyncResult", () => TaskToAsyncResult.Unwrap<int>(new NonTaskIAsyncResult()));
+ AssertExtensions.Throws<ArgumentException>("asyncResult", () => TaskToAsyncResult.Unwrap<int>(Task.FromResult((long)42)));
+ }
+
+ [Fact]
+ public async Task BeginFromTask_UnwrapTask_EndFromTask_Roundtrips()
+ {
+ var tcs = new TaskCompletionSource<int>();
+ object state = new object();
+
+ IAsyncResult ar = TaskToAsyncResult.Begin(tcs.Task, null, state);
+ Assert.NotNull(ar);
+ Assert.Same(state, ar.AsyncState);
+ Assert.NotNull(ar.AsyncWaitHandle);
+ Assert.False(ar.CompletedSynchronously);
+ Assert.False(ar.IsCompleted);
+
+ Assert.Same(tcs.Task, TaskToAsyncResult.Unwrap(ar));
+ Assert.Same(tcs.Task, TaskToAsyncResult.Unwrap<int>(ar));
+
+ tcs.SetResult(42);
+ await tcs.Task;
+
+ Assert.True(ar.IsCompleted);
+ Assert.False(ar.CompletedSynchronously);
+ Assert.True(ar.AsyncWaitHandle.WaitOne(0));
+
+ TaskToAsyncResult.End(ar);
+ Assert.Equal(42, TaskToAsyncResult.End<int>(ar));
+ }
+
+ [Fact]
+ public void BeginFromTask_CompletedSynchronously_CallbackInvokedSynchronously()
+ {
+ Task<int> t = Task.FromResult(42);
+ object state = new object();
+
+ int id = Environment.CurrentManagedThreadId;
+
+ IAsyncResult arCallback = null;
+ IAsyncResult ar = TaskToAsyncResult.Begin(t, iar =>
+ {
+ arCallback = iar;
+
+ Assert.True(iar.CompletedSynchronously);
+ Assert.True(iar.IsCompleted);
+ Assert.Same(state, iar.AsyncState);
+
+ Assert.Equal(id, Environment.CurrentManagedThreadId);
+
+ Assert.Equal(42, TaskToAsyncResult.End<int>(iar));
+ }, state);
+
+ Assert.Same(ar, arCallback);
+ Assert.True(ar.CompletedSynchronously);
+ Assert.True(ar.IsCompleted);
+ Assert.Same(state, ar.AsyncState);
+ }
+
+ [Fact]
+ public async void BeginFromTask_CompletedAsynchronously_CallbackInvokedAsynchronously()
+ {
+ var tcs = new TaskCompletionSource();
+ var invoked = new TaskCompletionSource();
+
+ var tl = new ThreadLocal<int>();
+ tl.Value = 42;
+ IAsyncResult ar = TaskToAsyncResult.Begin(tcs.Task, iar =>
+ {
+ Assert.NotEqual(42, tl.Value);
+ Assert.False(iar.CompletedSynchronously);
+ Assert.True(iar.IsCompleted);
+ Assert.Null(iar.AsyncState);
+ invoked.SetResult();
+ }, null);
+ tl.Value = 0;
+
+ Assert.False(invoked.Task.IsCompleted);
+ Assert.False(ar.CompletedSynchronously);
+ Assert.False(ar.IsCompleted);
+ Assert.Null(ar.AsyncState);
+ Assert.NotNull(ar.AsyncWaitHandle);
+ Assert.False(ar.AsyncWaitHandle.WaitOne(0));
+
+ tcs.SetResult();
+ await invoked.Task;
+
+ Assert.False(ar.CompletedSynchronously);
+ Assert.True(ar.IsCompleted);
+ Assert.Null(ar.AsyncState);
+ Assert.NotNull(ar.AsyncWaitHandle);
+ Assert.True(ar.AsyncWaitHandle.WaitOne(0));
+ }
+
+ [Fact]
+ public void EndFromTask_PropagatesExceptions()
+ {
+ IAsyncResult ar = TaskToAsyncResult.Begin(Task.FromException(new FormatException()), null, null);
+ Assert.Throws<FormatException>(() => TaskToAsyncResult.End(ar));
+
+ ar = TaskToAsyncResult.Begin(Task.FromException<int>(new FormatException()), null, null);
+ Assert.Throws<FormatException>(() => TaskToAsyncResult.End<int>(ar));
+ }
+
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
+ public async Task WithFromAsync_IAsyncResult_Roundtrips()
+ {
+ var tcs = new TaskCompletionSource();
+ var invoked = new TaskCompletionSource();
+ _ = Task.Factory.FromAsync(TaskToAsyncResult.Begin(tcs.Task, null, null), iar =>
+ {
+ invoked.SetResult();
+ });
+ tcs.SetResult();
+ await invoked.Task;
+ }
+
+ [Fact]
+ public async Task WithFromAsync_Delegate_Roundtrips()
+ {
+ var tcs = new TaskCompletionSource();
+ var invoked = new TaskCompletionSource();
+ _ = Task.Factory.FromAsync(
+ (callback, state) => TaskToAsyncResult.Begin(tcs.Task, callback, state),
+ iar => invoked.SetResult(),
+ new object());
+ tcs.SetResult();
+ await invoked.Task;
+ }
+ }
+
+ internal sealed class NonTaskIAsyncResult : IAsyncResult
+ {
+ public object? AsyncState { get; set; }
+ public WaitHandle AsyncWaitHandle { get; set; }
+ public bool CompletedSynchronously { get; set; }
+ public bool IsCompleted { get; set; }
+ }
+}
var d = new UnhandledExceptionEventHandler (helper.OnUnhandled);
AppDomain.CurrentDomain.UnhandledException += d;
- // this is TaskToApm.Begin (..., callback) where the callback is helper.WhenCompleted
+ // this is TaskToAsyncResult.Begin (..., callback) where the callback is helper.WhenCompleted
Task.Delay (100).GetAwaiter().OnCompleted (helper.WhenCompleted);
var wasSet = helper.WaitForExn (10000); // wait upto 10 seconds for the task to throw