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 December 5, 2023 06:31
.NET project structure
View dotnetlayout.md
$/
  artifacts/
  build/
  docs/
  lib/
  packages/
  samples/
  src/
 tests/
@davidfowl
davidfowl / .NET6Migration.md
Last active December 4, 2023 23:30
.NET 6 ASP.NET Core Migration
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>
@davidfowl
davidfowl / MinimalAPIs.md
Last active November 14, 2023 19:12
Minimal APIs at a glance
View MinimalAPIs.md
@davidfowl
davidfowl / DynamicController.cs
Last active October 9, 2023 12:30
Dynamically loading a controller
View DynamicController.cs
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Infrastructure;
View LazyControllers.cs
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Threading;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
@davidfowl
davidfowl / DefaultConfigProvider.cs
Last active October 7, 2023 07:18
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 / AUsage.cs
Last active September 17, 2023 10:26
Header propagation HttpClientFactory middleware
View AUsage.cs
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");
});
}
View Optimize.cs
using System.Text.Json;
using System.Text.Json.Serialization;
var data = new[]
{
"Hello"u8.ToArray(),
"Twitter"u8.ToArray(),
"Optimize"u8.ToArray()
}
.Select(m => new Message(m)).ToArray();
@davidfowl
davidfowl / Example1.cs
Last active July 7, 2023 15:18
How .NET Standard relates to .NET Platforms
View Example1.cs
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)