Skip to content

Commit 4c0104d

Browse files
authored
Remove AsyncEnumerable from System.Linq.Async public API (#2292)
1 parent 8da4d2a commit 4c0104d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+312
-51
lines changed

Ix.NET/Documentation/adr/0002-System-Linq-Async-In-Net10.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
## Status
66

7-
Proposed.
7+
Accepted.
88

99
## Authors
1010

@@ -15,7 +15,7 @@ Proposed.
1515

1616
As an accident of history, the Rx.NET repository ended up being the de facto implementation of LINQ for `IAsyncEnumerable<T>` from 2019 when .NET Core 3 shipped up until late 2025 when .NET 10 shipped.
1717

18-
This happened because Rx.NET had effectively been the incubator in which `IAsyncEnumerable<T>` was originally developed. Back before .NET Core 3.0, there was no such interface built into .NET, but Rx _did_ define this interface as part of its 'interactive extensions for .NET' feature. It also implemented common LINQ operators for that interface.
18+
This happened because Rx.NET had effectively been the incubator in which `IAsyncEnumerable<T>` was originally developed. Back before .NET Core 3.0, there was no such interface built into .NET, but Rx _did_ define this interface as part of its 'interactive extensions for .NET' feature. (It did this as early as 2010.) It also implemented common LINQ operators for that interface.
1919

2020
.NET Core 3.0 defined its own version of this `IAsyncEnumerable<T>`, but the .NET team did not implement LINQ for it at that time. Since the Rx.NET repository already had a fairly complete implementation of LINQ for its original version of `IAsyncEnumerable<T>`, it was fairly easy to adapt this to the new version of `IAsyncEnumerable<T>` built into .NET. Thus `System.Linq.Async` was born.
2121

@@ -34,20 +34,23 @@ There are also a couple of cases where functionality simply has not been reprodu
3434

3535
A further complication is that some methods in `System.Interactive.Async` clash with methods in `System.Linq.AsyncEnumerable`. For example, `MaxByAsync` and `MinByAsync`. Originally `MinBy` and `MaxBy` were unique to Rx.NET and Ix.NET. But .NET 6.0 added operators with these names to LINQ to Objects. Confusingly, they were slightly different: the Rx.NET and Ix.NET versions recognize that there might not be a single minimum or maximum value, and thus provide a collection of all the entries that are at the maximum value, but the .NET runtime class library versions just pick one arbitrary winner. So at this point, `System.Interactive` renamed its versions to `MinByWithTies` and `MaxByWithTies`. Unfortunately that same change wasn't made in `System.Interactive.Async`, so we now have the same situation with `System.Linq.AsyncEnumerable`: the .NET runtime class libraries now define `MinByAsync` and `MaxByAsync` extension methods for `IAsyncEnumerable<T>`, and these take the same arguments as the ones in `System.Interactive.Async`, but have a different return type, and have different behaviour!
3636

37+
One more important point to consider is that although LINQ to `IAsyncEnumerable<T>` _mostly_ consists of extension methods, there are a few static methods. (E.g., `AsyncEnumerable.Range`, which the .NET library implements, and `AsyncEnumerable.Create`, which it does not.) With extension methods, the compiler does not have a problem with multiple identically-named types in different assemblies all defining extension methods as long as the individual methods do not conflict. However, non-extension methods are a problem. If `System.Linq.Async` were to continue to define a public `AsyncEnumerable` type, then calls to `AsyncEnumerable.Range` would fail to compile: even though there would only be a single `Range` method (supplied by the new `System.Linq.AsyncEnumerable`) this would fail to compile because `AsyncEnumerable` itself is an ambiguous class name. So it will be necessary for the public API of `System.Linq.Async` v7 not to define an `AsyncEnumerable` type. This places some limits on how far we can go with source-level compatibility. (Binary compatibility is not a problem because the runtime assemblies can continue to define this type.)
38+
3739

3840
## Decision
3941

4042
The next Ix.NET release will:
4143

4244
1. add a reference to `System.Linq.AsyncEnumerable` and `System.Interactive.Async` in `System.Linq.Async`
4345
2. remove from `System.Linq.Async`'s and `System.Interactive.Async`'s publicly visible API (ref assemblies) all `IAsyncEnumerable<T>` extension methods for which direct replacements exist (adding `MinByWithTiesAsync` and `MaxByWithTiesAsync` for the case where the new .NET runtime library methods actually have slightly different functionality)
44-
3. add [Obsolete] attribute for members of `AsyncEnumerable` for which `System.Linq.AsyncEnumerable` offers replacements that require code changes to use (e.g., `WhereAwait`, which is replaced by an overload of `Where`)
45-
4. `AsyncEnumerable` methods that are a bad idea and that should probably have never existing (the ones that do sync over async, e.g. `ToEnumerable`) are marked as `Obsolete` and will not be replaced; note that although `ToObservable` has issues that meant the .NET team decided not to replicate it, the main issue is that it embeds opinions, and not that there's anything fundamentally broken about it, so we do not include `ToObservable` in this category
46-
5. remaining methods of `AsyncEnumerable` (where `System.Linq.AsyncEnumerable` offers no equivalent) are removed from the publicly visible API of `System.Linq.Async`, with identical replacements being defined by `AsyncEnumerableEx` in `System.Interactive
47-
6. mark `IAsyncGrouping` as obsolete
48-
7. mark the public `IAsyncIListProvider` as obsolete, and define a non-public version for continued internal use in `System.Interactive.Linq`
49-
8. continue to provide the full `System.Linq.Async` API in the `lib` assemblies to provide binary compatibility
50-
9. mark the `System.Linq.Async` NuGet package as obsolete, and recommend the use of `System.Linq.AsyncEnumerable` and/or `System.Interactive.Async` instead
46+
4. Rename `AsyncEnumerable` to `AsyncEnumerableDeprecated` in the public API (reference assemblies; the old name will be retained in runtime assemblies for binary compatibility) to avoid errors arising from there being two definitions of `AsyncEnumerable` in the same namespace
47+
5. add [Obsolete] attribute for members of `AsyncEnumerableDeprecated` for which `System.Linq.AsyncEnumerable` offers replacements that require code changes to use (e.g., `WhereAwait`, which is replaced by an overload of `Where`)
48+
6. the `AsyncEnumerable.ToEnumerable` method that was a bad idea and that should probably have never existed has been marked as `Obsolete` and will not be replaced; note that although `ToObservable` has issues that meant the .NET team decided not to replicate it, the main issue is that it embeds opinions, and not that there's anything fundamentally broken about it, so we do not include `ToObservable` in this category
49+
7. remaining methods of `AsyncEnumerable` (where `System.Linq.AsyncEnumerable` offers no equivalent) are removed from the publicly visible API of `System.Linq.Async`, with identical replacements being defined by `AsyncEnumerableEx` in `System.Interactive.Async`
50+
8. mark `IAsyncGrouping` as obsolete
51+
9. mark the public `IAsyncIListProvider` as obsolete, and define a non-public version for continued internal use in `System.Interactive.Linq`
52+
10. continue to provide the full `System.Linq.Async` API in the `lib` assemblies to provide binary compatibility
53+
11. mark the `System.Linq.Async` NuGet package as obsolete, and recommend the use of `System.Linq.AsyncEnumerable` and/or `System.Interactive.Async` instead
5154

5255
The main effect of this is that code that had been using the `System.Linq.Async` implementation of LINQ for `IAsyncEnumerable<T>` will, in most cases, now be using the .NET runtime library implementation if it is rebuilt against this new version of `System.Linq.Async`.
5356

@@ -79,4 +82,4 @@ Code that had been written to use `System.Linq.Async` v6 that upgrades to .NET 1
7982

8083
The situation is very similar for code written to use `System.Linq.Async` v6 that does _not_ upgrade to .NET 10 (e.g. either it stays on .NET 8 or 9, or it targets .NET Framework or .NET Standard) but which newly acquires a dependency on `System.Linq.AsyncEnumerable` either because the developer adds it, or because they update to a new version of some component which adds it as a new transitive dependency.
8184

82-
Code written to use `System.Linq.Async` v6 that changes nothing at all but, which is rebuilt after `System.Linq.Async` v7 is released, will see a warning that the package is now deprecated. They can fix this warning by removing the package and adding a reference to `System.Linq.AsyncEnumerable` or `System.Interactive.Async` or both as required.
85+
Code written to use `System.Linq.Async` v6 that changes nothing at all but, which is rebuilt after `System.Linq.Async` v7 is released, will see a warning that the package is now deprecated. Developers can fix this warning by removing the package and adding a reference to `System.Linq.AsyncEnumerable` or `System.Interactive.Async` or both as required.

Ix.NET/Source/System.Interactive.Async/System/Linq/AsyncEnumerableEx.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// The .NET Foundation licenses this file to you under the MIT License.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Collections.Generic;
6+
using System.Threading;
7+
58
namespace System.Linq
69
{
710
/// <summary>
@@ -10,5 +13,44 @@ namespace System.Linq
1013
/// <seealso cref="AsyncEnumerable"/>
1114
public static partial class AsyncEnumerableEx
1215
{
16+
// NOTE: This was originally in System.Linq.Async, but was moved here because we are
17+
// deprecating that package. It's not clear if this is still useful: there was
18+
// a REVIEW comment against this code saying "Async iterators can be
19+
// used to implement these interfaces." We retain this mainly so that anyone
20+
// who was using it can still have it. Unfortunately there's no way to do that
21+
// without introducing a source-level breaking change: we've had to move it out
22+
// of AsyncEnumerable because we can't have System.Linq.Async's public API defining
23+
// a class of that name. (It causes ambiguous type errors if you try to invoke
24+
// a static method such as AsyncEnumerable.Range: even if System.Linq.Async doesn't
25+
// define Range on its AsyncEnumerable, the compiler chokes on the fact that there
26+
// are two AsyncEnumerable types.)
27+
28+
/// <summary>
29+
/// Creates a new enumerable using the specified delegates implementing the members of <see cref="IAsyncEnumerable{T}"/>.
30+
/// </summary>
31+
/// <typeparam name="T">The type of the elements returned by the enumerable sequence.</typeparam>
32+
/// <param name="getAsyncEnumerator">The delegate implementing the <see cref="IAsyncEnumerable{T}.GetAsyncEnumerator"/> method.</param>
33+
/// <returns>A new enumerable instance.</returns>
34+
public static IAsyncEnumerable<T> Create<T>(Func<CancellationToken, IAsyncEnumerator<T>> getAsyncEnumerator)
35+
{
36+
if (getAsyncEnumerator == null)
37+
throw Error.ArgumentNull(nameof(getAsyncEnumerator));
38+
39+
return new AnonymousAsyncEnumerable<T>(getAsyncEnumerator);
40+
}
41+
42+
private sealed class AnonymousAsyncEnumerable<T> : IAsyncEnumerable<T>
43+
{
44+
private readonly Func<CancellationToken, IAsyncEnumerator<T>> _getEnumerator;
45+
46+
public AnonymousAsyncEnumerable(Func<CancellationToken, IAsyncEnumerator<T>> getEnumerator) => _getEnumerator = getEnumerator;
47+
48+
public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken)
49+
{
50+
cancellationToken.ThrowIfCancellationRequested(); // NB: [LDM-2018-11-28] Equivalent to async iterator behavior.
51+
52+
return _getEnumerator(cancellationToken);
53+
}
54+
}
1355
}
1456
}

Ix.NET/Source/System.Linq.Async/System/Linq/AsyncEnumerable.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@
77

88
namespace System.Linq
99
{
10+
#if INCLUDE_RELOCATED_TO_INTERACTIVE_ASYNC
1011
/// <summary>
1112
/// Provides a set of extension methods for <see cref="IAsyncEnumerable{T}"/>.
1213
/// </summary>
1314
public static partial class AsyncEnumerable
1415
{
1516
//
16-
// REVIEW: Create methods may not belong in System.Linq.Async. Async iterators can be
17-
// used to implement these interfaces. Move to System.Interactive.Async?
17+
// NOTE: This has been replaced in v7 by a method of the same name on
18+
// System.Interactive.Async's AsyncEnumerableEx class. This is a breaking
19+
// change but it's necessary because we can't allow System.Linq.Async's
20+
// ref assembly to define a public AsyncEnumerable type. If we do that,
21+
// it will conflict with System.Linq.AsyncEnumerable, preventing direct
22+
// invocation of static methods. (E.g., AsyncEnumerable.Range.)
1823
//
1924

2025
/// <summary>
@@ -45,4 +50,5 @@ public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToke
4550
}
4651
}
4752
}
53+
#endif // INCLUDE_RELOCATED_TO_INTERACTIVE_ASYNC
4854
}

Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Aggregate.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88

99
namespace System.Linq
1010
{
11+
#if REFERENCE_ASSEMBLY
12+
public static partial class AsyncEnumerableDeprecated
13+
#else
1114
public static partial class AsyncEnumerable
15+
#endif
1216
{
1317
#if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
1418
// https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.aggregateasync?view=net-9.0-pp#system-linq-asyncenumerable-aggregateasync-1(system-collections-generic-iasyncenumerable((-0))-system-func((-0-0-0))-system-threading-cancellationtoken)

Ix.NET/Source/System.Linq.Async/System/Linq/Operators/All.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88

99
namespace System.Linq
1010
{
11+
#if REFERENCE_ASSEMBLY
12+
public static partial class AsyncEnumerableDeprecated
13+
#else
1114
public static partial class AsyncEnumerable
15+
#endif
1216
{
1317
#if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
1418
// https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.allasync?view=net-9.0-pp#system-linq-asyncenumerable-allasync-1(system-collections-generic-iasyncenumerable((-0))-system-func((-0-system-boolean))-system-threading-cancellationtoken)

Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Any.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88

99
namespace System.Linq
1010
{
11+
#if REFERENCE_ASSEMBLY
12+
public static partial class AsyncEnumerableDeprecated
13+
#else
1114
public static partial class AsyncEnumerable
15+
#endif
1216
{
1317
#if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
1418
// https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.anyasync?view=net-9.0-pp#system-linq-asyncenumerable-anyasync-1(system-collections-generic-iasyncenumerable((-0))-system-threading-cancellationtoken)

Ix.NET/Source/System.Linq.Async/System/Linq/Operators/AppendPrepend.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010

1111
namespace System.Linq
1212
{
13+
#if REFERENCE_ASSEMBLY
14+
public static partial class AsyncEnumerableDeprecated
15+
#else
1316
public static partial class AsyncEnumerable
17+
#endif
1418
{
1519
#if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
1620
// https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.append?view=net-9.0-pp

Ix.NET/Source/System.Linq.Async/System/Linq/Operators/AsAsyncEnumerable.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66

77
namespace System.Linq
88
{
9+
#if REFERENCE_ASSEMBLY
10+
public static partial class AsyncEnumerableDeprecated
11+
#else
912
public static partial class AsyncEnumerable
10-
// NB: Synchronous LINQ to Objects doesn't hide the implementation of the source either.
13+
#endif
14+
// NB: Synchronous LINQ to Objects doesn't hide the implementation of the source either.
1115
{
1216
#if INCLUDE_RELOCATED_TO_INTERACTIVE_ASYNC
1317
// Note: this one isn't actually in the System.Linq.AsyncEnumerable package, so we've moved it

Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Average.Generated.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88

99
namespace System.Linq
1010
{
11+
#if REFERENCE_ASSEMBLY
12+
public static partial class AsyncEnumerableDeprecated
13+
#else
1114
public static partial class AsyncEnumerable
15+
#endif
1216
{
1317
#if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
1418
/// <summary>

Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Average.Generated.tt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ using System.Threading.Tasks;
1414

1515
namespace System.Linq
1616
{
17+
#if REFERENCE_ASSEMBLY
18+
public static partial class AsyncEnumerableDeprecated
19+
#else
1720
public static partial class AsyncEnumerable
21+
#endif
1822
{
1923
<#
2024
var os = new[]

0 commit comments

Comments
 (0)