Skip to content

Instantly share code, notes, and snippets.

View davidfowl's full-sized avatar

David Fowler davidfowl

View GitHub Profile
@davidfowl
davidfowl / dotnetlayout.md
Last active November 10, 2024 09:09
.NET project structure
$/
  artifacts/
  build/
  docs/
  lib/
  packages/
  samples/
  src/
 tests/
@davidfowl
davidfowl / MinimalAPIs.md
Last active October 24, 2024 13:39
Minimal APIs at a glance
@davidfowl
davidfowl / .NET6Migration.md
Last active October 23, 2024 18:30
.NET 6 ASP.NET Core Migration
@davidfowl
davidfowl / AUsage.cs
Last active October 18, 2024 14:16
Header propagation HttpClientFactory middleware
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient("myclient");
// Global header propagation for any HttpClient that comes from HttpClientFactory
services.AddHeaderPropagation(options =>
{
options.HeaderNames.Add("Correlation-Id");
});
}
@davidfowl
davidfowl / WaitToStart.cs
Last active October 16, 2024 20:56
Wait to start command
using Microsoft.Extensions.DependencyInjection;
var builder = DistributedApplication.CreateBuilder(args);
builder.AddContainer("redis", "redis").WithExplicitStart();
builder.Build().Run();
public static class ExplicitStartupExtensions
{
@davidfowl
davidfowl / Minimal-protobuf.cs
Last active October 8, 2024 09:15
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))
.Accepts<Person>("application/protobuf");
@davidfowl
davidfowl / gist:3173128
Created July 24, 2012 22:36 — forked from jmangelo/gist:3173109
MethodInfo.Invoke is slow... open instance delegate to the rescue
using System;
using System.Linq;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Reflection;
using System.Collections.Generic;
namespace ConsoleApplication3
{
class Program
@davidfowl
davidfowl / Example1.cs
Last active September 2, 2024 12:36
How .NET Standard relates to .NET Platforms
namespace Analogy
{
/// <summary>
/// This example shows that a library that needs access to target .NET Standard 1.3
/// can only access APIs available in that .NET Standard. Even though similar the APIs exist on .NET
/// Framework 4.5, it implements a version of .NET Standard that isn't compatible with the library.
/// </summary>INetCoreApp10
class Example1
{
public void Net45Application(INetFramework45 platform)
@davidfowl
davidfowl / TimeHttpEvents.cs
Last active August 7, 2024 01:57
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);
builder.Services.AddTelemetryConsumer<TelemetryConsumer>();
var app = builder.Build();
@davidfowl
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);
builder.Configuration.AddConfigurationDefaults(new()
{
{ "request:timeout", "60" }
});
var app = builder.Build();