Skip to content

Instantly share code, notes, and snippets.

Avatar

David Fowler davidfowl

View GitHub Profile
@davidfowl
davidfowl / SingleThreadedScheduler.cs
Created October 11, 2022 02:53
A sample showing how to schedule work on a single thread
View SingleThreadedScheduler.cs
using System.Collections.Concurrent;
using System.Threading.Channels;
var channel = Channel.CreateUnbounded<int>();
var syncContext = new SingleThreadedSyncContext();
syncContext.Post(async _ =>
{
await foreach (var item in channel.Reader.ReadAllAsync())
@davidfowl
davidfowl / DefaultConfigProvider.cs
Last active August 8, 2022 06:17
Allows specifying default configuration values in code while still allowing other sources to override
View DefaultConfigProvider.cs
using Microsoft.Extensions.Configuration.Memory;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddConfigurationDefaults(new()
{
{ "request:timeout", "60" }
});
var app = builder.Build();
@davidfowl
davidfowl / Minimal-protobuf.cs
Last active December 7, 2022 23:39
Minimal + protobuf
View Minimal-protobuf.cs
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.WebUtilities;
using ProtoBuf;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "POST a protobuf message to the /");
app.MapPost("/", (Proto<Person> p) => Results.Extensions.Protobuf(p.Item))
.Accepts<Person>("application/protobuf");
@davidfowl
davidfowl / Quic.cs
Last active December 19, 2022 02:46
View Quic.cs
using System.IO.Pipelines;
using System.Net;
using System.Net.Security;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Connections.Features;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core;
var builder = WebApplication.CreateBuilder(args);
@davidfowl
davidfowl / DTO.cs
Last active December 12, 2022 17:55
Associated types: A demo of what DTOs could look like as a language feature
View DTO.cs
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapPost("/todos", async (CreateTodo createTodo, TodoDb db) =>
{
// This is where the implicit conversion comes in
db.Todos.Add(createTodo);
@davidfowl
davidfowl / .NET6Migration.md
Last active March 20, 2023 13:04
.NET 6 ASP.NET Core Migration
@davidfowl
davidfowl / MinimalAPIs.md
Last active March 21, 2023 13:20
Minimal APIs at a glance
View MinimalAPIs.md
@davidfowl
davidfowl / Example.cs
Last active July 8, 2022 15:30
An implementation of MessagePipe. Something like a channel but with buffer management so you can peek and advance the message that was read.
View Example.cs
using System.Buffers;
using System.Net.WebSockets;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/ws", async (HttpContext context) =>
{
const int MaxMessageSize = 1024 * 1024;
@davidfowl
davidfowl / server.cs
Last active September 20, 2021 13:56 — forked from DamianEdwards/server.cs
Simplified ASP.NET Core app exploration
View server.cs
#!/usr/bin/env dotnet run
var builder = WebApplication.CreateBuilder(args);
var config = builder.Configuration;
var connString = config["connectionString"] ?? "Data Source=todos.db";
builder.AddDbContext<TodoDb>(options => options.UseSqlite(connString));
builder.AddSqlite<Todo>(connString) // Higher level API perhaps?
var app = builder.Build();
View ConcurrentDictionaryExtensions.cs
using System;
using System.Threading.Tasks;
namespace System.Collections.Concurrent
{
public static class ConcurrentDictionaryExtensions
{
/// <summary>
/// Provides an alternative to <see cref="ConcurrentDictionary{TKey, TValue}.GetOrAdd(TKey, Func{TKey, TValue})"/> that disposes values that implement <see cref="IDisposable"/>.
/// </summary>