Skip to content

Instantly share code, notes, and snippets.

Avatar
:octocat:
Working on .NET 6

David Fowler davidfowl

:octocat:
Working on .NET 6
View GitHub Profile
@davidfowl
davidfowl / dotnetlayout.md
Last active Jul 21, 2021
.NET project structure
View dotnetlayout.md
$/
  artifacts/
  build/
  docs/
  lib/
  packages/
  samples/
  src/
  tests/
@davidfowl
davidfowl / AUsage.cs
Last active Jul 19, 2021
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");
});
}
@davidfowl
davidfowl / Global.asax.cs
Last active Jul 18, 2021
ASP.NET MVC and ServiceCollection sample
View Global.asax.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Microsoft.Extensions.DependencyInjection;
using WebApplication16;
using WebApplication16.Controllers;
@davidfowl
davidfowl / Program.cs
Last active Jul 6, 2021
A minimal fully asynchronous C# ASP.NET Core 3.0 application with routing (learn more about ASP.NET Core here https://docs.microsoft.com/en-us/aspnet/core/?view=aspnetcore-3.0)
View Program.cs
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
public class Program
{
public static void Main(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
@davidfowl
davidfowl / bedrock-server.cs
Last active Jul 1, 2021
A sample of the current API usage with bedrock's listener layer
View bedrock-server.cs
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
@davidfowl
davidfowl / Example1.cs
Last active Jun 13, 2021
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)
View HeaderPropagationV2.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Threading;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
namespace header_propagation
View Multitenancy.cs
using System;
using System.Collections.Generic;
using System.Threading;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
/*
- Don't need to support different services per tenant.
- Tenant services lifetime outlasts the request.
@davidfowl
davidfowl / DynamicController.cs
Last active Jun 4, 2021
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 Solution.cs
var t1 = DoFooAsync(obj);
var t2 = DoBarAsync(obj);
var t = await WhenAnySuccessOrAllFail(t1, t2);
async Task WhenAnySuccessOrAllFail(params Task[] tasks)
{
var remaining = new List<Task>(tasks);
while (remaining.Count > 0)