Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions async-csharp/IAsyncEnumerableWithYield/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# SQLite database the API creates on first run
*.db
*.db-shm
*.db-wal
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TaskOfIEnumerable", "TaskOfIEnumerable\TaskOfIEnumerable.csproj", "{CEF7D2E3-0B02-4E40-9084-C29DCCA519CD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SyncYield", "SyncYield\SyncYield.csproj", "{29FC1E6D-1BB0-4AE1-879B-B97A2BC0AD44}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IAsyncEnumerableWithYield", "IAsyncEnumerableWithYield\IAsyncEnumerableWithYield.csproj", "{296E143D-0CCD-48CA-9054-5E918F8B5462}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IAsyncEnumerableWithYieldApi", "IAsyncEnumerableWithYieldApi\IAsyncEnumerableWithYieldApi.csproj", "{01A66C19-2011-41A9-876A-E02A45431465}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{32DFB249-9EA5-4E7C-8BBA-E01853778BE7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CEF7D2E3-0B02-4E40-9084-C29DCCA519CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CEF7D2E3-0B02-4E40-9084-C29DCCA519CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CEF7D2E3-0B02-4E40-9084-C29DCCA519CD}.Debug|x64.ActiveCfg = Debug|Any CPU
{CEF7D2E3-0B02-4E40-9084-C29DCCA519CD}.Debug|x64.Build.0 = Debug|Any CPU
{CEF7D2E3-0B02-4E40-9084-C29DCCA519CD}.Debug|x86.ActiveCfg = Debug|Any CPU
{CEF7D2E3-0B02-4E40-9084-C29DCCA519CD}.Debug|x86.Build.0 = Debug|Any CPU
{CEF7D2E3-0B02-4E40-9084-C29DCCA519CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CEF7D2E3-0B02-4E40-9084-C29DCCA519CD}.Release|Any CPU.Build.0 = Release|Any CPU
{CEF7D2E3-0B02-4E40-9084-C29DCCA519CD}.Release|x64.ActiveCfg = Release|Any CPU
{CEF7D2E3-0B02-4E40-9084-C29DCCA519CD}.Release|x64.Build.0 = Release|Any CPU
{CEF7D2E3-0B02-4E40-9084-C29DCCA519CD}.Release|x86.ActiveCfg = Release|Any CPU
{CEF7D2E3-0B02-4E40-9084-C29DCCA519CD}.Release|x86.Build.0 = Release|Any CPU
{29FC1E6D-1BB0-4AE1-879B-B97A2BC0AD44}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{29FC1E6D-1BB0-4AE1-879B-B97A2BC0AD44}.Debug|Any CPU.Build.0 = Debug|Any CPU
{29FC1E6D-1BB0-4AE1-879B-B97A2BC0AD44}.Debug|x64.ActiveCfg = Debug|Any CPU
{29FC1E6D-1BB0-4AE1-879B-B97A2BC0AD44}.Debug|x64.Build.0 = Debug|Any CPU
{29FC1E6D-1BB0-4AE1-879B-B97A2BC0AD44}.Debug|x86.ActiveCfg = Debug|Any CPU
{29FC1E6D-1BB0-4AE1-879B-B97A2BC0AD44}.Debug|x86.Build.0 = Debug|Any CPU
{29FC1E6D-1BB0-4AE1-879B-B97A2BC0AD44}.Release|Any CPU.ActiveCfg = Release|Any CPU
{29FC1E6D-1BB0-4AE1-879B-B97A2BC0AD44}.Release|Any CPU.Build.0 = Release|Any CPU
{29FC1E6D-1BB0-4AE1-879B-B97A2BC0AD44}.Release|x64.ActiveCfg = Release|Any CPU
{29FC1E6D-1BB0-4AE1-879B-B97A2BC0AD44}.Release|x64.Build.0 = Release|Any CPU
{29FC1E6D-1BB0-4AE1-879B-B97A2BC0AD44}.Release|x86.ActiveCfg = Release|Any CPU
{29FC1E6D-1BB0-4AE1-879B-B97A2BC0AD44}.Release|x86.Build.0 = Release|Any CPU
{296E143D-0CCD-48CA-9054-5E918F8B5462}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{296E143D-0CCD-48CA-9054-5E918F8B5462}.Debug|Any CPU.Build.0 = Debug|Any CPU
{296E143D-0CCD-48CA-9054-5E918F8B5462}.Debug|x64.ActiveCfg = Debug|Any CPU
{296E143D-0CCD-48CA-9054-5E918F8B5462}.Debug|x64.Build.0 = Debug|Any CPU
{296E143D-0CCD-48CA-9054-5E918F8B5462}.Debug|x86.ActiveCfg = Debug|Any CPU
{296E143D-0CCD-48CA-9054-5E918F8B5462}.Debug|x86.Build.0 = Debug|Any CPU
{296E143D-0CCD-48CA-9054-5E918F8B5462}.Release|Any CPU.ActiveCfg = Release|Any CPU
{296E143D-0CCD-48CA-9054-5E918F8B5462}.Release|Any CPU.Build.0 = Release|Any CPU
{296E143D-0CCD-48CA-9054-5E918F8B5462}.Release|x64.ActiveCfg = Release|Any CPU
{296E143D-0CCD-48CA-9054-5E918F8B5462}.Release|x64.Build.0 = Release|Any CPU
{296E143D-0CCD-48CA-9054-5E918F8B5462}.Release|x86.ActiveCfg = Release|Any CPU
{296E143D-0CCD-48CA-9054-5E918F8B5462}.Release|x86.Build.0 = Release|Any CPU
{01A66C19-2011-41A9-876A-E02A45431465}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{01A66C19-2011-41A9-876A-E02A45431465}.Debug|Any CPU.Build.0 = Debug|Any CPU
{01A66C19-2011-41A9-876A-E02A45431465}.Debug|x64.ActiveCfg = Debug|Any CPU
{01A66C19-2011-41A9-876A-E02A45431465}.Debug|x64.Build.0 = Debug|Any CPU
{01A66C19-2011-41A9-876A-E02A45431465}.Debug|x86.ActiveCfg = Debug|Any CPU
{01A66C19-2011-41A9-876A-E02A45431465}.Debug|x86.Build.0 = Debug|Any CPU
{01A66C19-2011-41A9-876A-E02A45431465}.Release|Any CPU.ActiveCfg = Release|Any CPU
{01A66C19-2011-41A9-876A-E02A45431465}.Release|Any CPU.Build.0 = Release|Any CPU
{01A66C19-2011-41A9-876A-E02A45431465}.Release|x64.ActiveCfg = Release|Any CPU
{01A66C19-2011-41A9-876A-E02A45431465}.Release|x64.Build.0 = Release|Any CPU
{01A66C19-2011-41A9-876A-E02A45431465}.Release|x86.ActiveCfg = Release|Any CPU
{01A66C19-2011-41A9-876A-E02A45431465}.Release|x86.Build.0 = Release|Any CPU
{32DFB249-9EA5-4E7C-8BBA-E01853778BE7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{32DFB249-9EA5-4E7C-8BBA-E01853778BE7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{32DFB249-9EA5-4E7C-8BBA-E01853778BE7}.Debug|x64.ActiveCfg = Debug|Any CPU
{32DFB249-9EA5-4E7C-8BBA-E01853778BE7}.Debug|x64.Build.0 = Debug|Any CPU
{32DFB249-9EA5-4E7C-8BBA-E01853778BE7}.Debug|x86.ActiveCfg = Debug|Any CPU
{32DFB249-9EA5-4E7C-8BBA-E01853778BE7}.Debug|x86.Build.0 = Debug|Any CPU
{32DFB249-9EA5-4E7C-8BBA-E01853778BE7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{32DFB249-9EA5-4E7C-8BBA-E01853778BE7}.Release|Any CPU.Build.0 = Release|Any CPU
{32DFB249-9EA5-4E7C-8BBA-E01853778BE7}.Release|x64.ActiveCfg = Release|Any CPU
{32DFB249-9EA5-4E7C-8BBA-E01853778BE7}.Release|x64.Build.0 = Release|Any CPU
{32DFB249-9EA5-4E7C-8BBA-E01853778BE7}.Release|x86.ActiveCfg = Release|Any CPU
{32DFB249-9EA5-4E7C-8BBA-E01853778BE7}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Console.WriteLine($"{DateTime.Now.ToLongTimeString()}: Start");

await foreach (var item in FetchItems())
{
Console.WriteLine($"{DateTime.Now.ToLongTimeString()}: {item}");
}

Console.WriteLine($"{DateTime.Now.ToLongTimeString()}: End");

static async IAsyncEnumerable<int> FetchItems()
{
for (int i = 1; i <= 10; i++)
{
await Task.Delay(1000);
yield return i;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

[Route("api/[controller]")]
[ApiController]
public class ItemsController : ControllerBase
{
private readonly ItemContext _context;

public ItemsController(ItemContext context)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
}

[HttpGet]
public IAsyncEnumerable<Item> GetItems() =>
_context.Items.AsNoTracking().AsAsyncEnumerable();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.AspNetCore.Mvc;

[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpGet]
public IAsyncEnumerable<int> Get()
{
return FetchItems();
}

static async IAsyncEnumerable<int> FetchItems()
{
for (int i = 1; i <= 10; i++)
{
await Task.Delay(1000);
yield return i;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class Item
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Microsoft.EntityFrameworkCore;

public class ItemContext(DbContextOptions<ItemContext> options) : DbContext(options)
{
public DbSet<Item> Items => Set<Item>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.12" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddDbContext<ItemContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("Items")));

var app = builder.Build();

using (var scope = app.Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<ItemContext>();
context.Database.EnsureCreated();

if (!context.Items.Any())
{
context.Items.AddRange(
new Item { Name = "Keyboard" },
new Item { Name = "Mouse" },
new Item { Name = "Monitor" });
context.SaveChanges();
}
}

app.MapControllers();

app.MapGet("/items", () => FetchItems());

app.Run();

static async IAsyncEnumerable<int> FetchItems()
{
for (var i = 1; i <= 10; i++)
{
await Task.Delay(1000);
yield return i;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "api/values",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"ConnectionStrings": {
"Items": "Data Source=items.db"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
17 changes: 17 additions & 0 deletions async-csharp/IAsyncEnumerableWithYield/SyncYield/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Console.WriteLine($"{DateTime.Now.ToLongTimeString()}: Start");

foreach (var item in FetchItems())
{
Console.WriteLine($"{DateTime.Now.ToLongTimeString()}: {item}");
}

Console.WriteLine($"{DateTime.Now.ToLongTimeString()}: End");

static IEnumerable<int> FetchItems()
{
for (int i = 1; i <= 10; i++)
{
Thread.Sleep(1000);
yield return i;
}
}
10 changes: 10 additions & 0 deletions async-csharp/IAsyncEnumerableWithYield/SyncYield/SyncYield.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Console.WriteLine($"{DateTime.Now.ToLongTimeString()}: Start");

foreach (var item in await FetchItems())
{
Console.WriteLine($"{DateTime.Now.ToLongTimeString()}: {item}");
}

Console.WriteLine($"{DateTime.Now.ToLongTimeString()}: End");

static async Task<IEnumerable<int>> FetchItems()
{
List<int> Items = new List<int>();
for (int i = 1; i <= 10; i++)
{
await Task.Delay(1000);
Items.Add(i);
}

return Items;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Data.Common;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;

namespace Tests;

[TestClass]
public class AsAsyncEnumerableUnitTest
{
[TestMethod]
public async Task GivenItemsQuery_WhenAsAsyncEnumerableIsCalled_ThenNoCommandRunsUntilEnumeration()
{
await using var connection = new SqliteConnection("Data Source=:memory:");
await connection.OpenAsync();

var counter = new ReaderCounter();
var options = new DbContextOptionsBuilder<ItemContext>()
.UseSqlite(connection)
.AddInterceptors(counter)
.Options;

await using var context = new ItemContext(options);
await context.Database.EnsureCreatedAsync();
context.Items.AddRange(new Item { Name = "Keyboard" }, new Item { Name = "Mouse" });
await context.SaveChangesAsync();
counter.Readers = 0;

var items = context.Items.AsNoTracking().AsAsyncEnumerable();
await using var enumerator = items.GetAsyncEnumerator();

Assert.AreEqual(0, counter.Readers);

Assert.IsTrue(await enumerator.MoveNextAsync());
Assert.AreEqual(1, counter.Readers);
Assert.AreEqual("Keyboard", enumerator.Current.Name);
}

private sealed class ReaderCounter : DbCommandInterceptor
{
public int Readers;

public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(
DbCommand command,
CommandEventData eventData,
InterceptionResult<DbDataReader> result,
CancellationToken cancellationToken = default)
{
Readers++;
return base.ReaderExecutingAsync(command, eventData, result, cancellationToken);
}
}
}
Loading
Loading