Skip to content

Instantly share code, notes, and snippets.

View davidfowl's full-sized avatar

David Fowler davidfowl

View GitHub Profile
davidfowl /
Created March 30, 2024 22:51
Github copilot

Me using copilot

using System.Text.Json;
using System.Text.Json.Serialization;
var data = new[]
.Select(m => new Message(m)).ToArray();
davidfowl / TimeHttpEvents.cs
Last active April 6, 2024 20:17
Using Yarp.Telemetry.Consumption to track outbound network events (this package isn't tied to YARP)
using System.Diagnostics;
using System.Net.Sockets;
using System.Security.Authentication;
using Yarp.Telemetry.Consumption;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
public static class ConfigurationBinderExtensions
public static ValidateOptionsResult BindAndValidate<TOptions>(this IConfiguration configuration, TOptions instance) where TOptions : class, new()
var validateOptions = new DataAnnotationValidateOptions<TOptions>(Options.DefaultName);
return validateOptions.Validate(Options.DefaultName, instance);
davidfowl / FromSqlInterpolatedStringHandler.cs
Last active July 3, 2023 12:55
Implementation of parameterized sql queries using string interpolation handlers
using System.Data.Common;
using System.Runtime.CompilerServices;
using System.Text;
using Npgsql;
GetCatalogItemsSql(null, null, null, 10);
void GetCatalogItemsSql(int? catalogBrandId, int? before, int? after, int pageSize)
// This looks like it would be susceptible to SQL injection, but it's not.
davidfowl / SingleThreadedScheduler.cs
Created October 11, 2022 02:53
A sample showing how to schedule work on a single thread
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 / DefaultConfigProvider.cs
Last active July 16, 2024 08:12
Allows specifying default configuration values in code while still allowing other sources to override
using Microsoft.Extensions.Configuration.Memory;
var builder = WebApplication.CreateBuilder(args);
{ "request:timeout", "60" }
var app = builder.Build();
davidfowl / Minimal-protobuf.cs
Last active June 25, 2023 00:16
Minimal + protobuf
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))
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 / DTO.cs
Last active June 2, 2023 09:16
Associated types: A demo of what DTOs could look like as a language feature
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