Skip to content

Instantly share code, notes, and snippets.

Avatar
🌴

David Fowler davidfowl

🌴
View GitHub Profile
@davidfowl
davidfowl / SingleThreadedScheduler.cs
Created Oct 11, 2022
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 Aug 8, 2022
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 Aug 4, 2022
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");
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 Oct 17, 2022
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 / MinimalAPIs.md
Last active Nov 28, 2022
Minimal APIs at a glance
View MinimalAPIs.md
@davidfowl
davidfowl / Example.cs
Last active Jul 8, 2022
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 Sep 20, 2021 — 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>