I'd like to know if I need to use EnumeratorCancellation when passing a cancellation token to my local function. I am thinking of using this code pattern often in the future:
public static IAsyncEnumerable<string> MyOuterFunctionAsync(
this Client client,
CancellationToken cancellationToken,
int? limit = null)
{
return MyLocalFunction().
TakeWhile(
(_, index) =>
limit is null ||
index < limit.Value);
async IAsyncEnumerable<string> MyLocalFunction()
{
var request = CreateRequest();
do
{
var page = await request.GetAsync(cancellationToken);
foreach (var item in page)
{
yield return item;
}
request = GetNextPageRequest();
}
while (request is not null)
}
}
Resharper doesn't mention the need for EnumeratorCancellation, and when I try to add it to the outer function it says it will have no effect, but if I try adding it to the local function Resharper stays happy, as without. Should I use it anywhere?
I checked the IL viewer but I don't see any difference between the versions.
Will MyOuterFunctionAsync work properly? Do I need to change MyLocalFunction signature to
async IAsyncEnumerable<string> MyLocalFunction(
[EnumeratorCancellation] CancellationToken cancellationToken)