Skip to content

Instantly share code, notes, and snippets.

View JamesNK's full-sized avatar

James Newton-King JamesNK

View GitHub Profile
@JamesNK
JamesNK / CompletePartialElements.cs
Last active March 17, 2025 00:31
CompletePartialElements.cs
public static void CompletePartialElements(MarkdownDocument document)
{
var lastChild = document.LastChild;
while (lastChild is ContainerBlock containerBlock)
{
lastChild = containerBlock.LastChild;
}
if (lastChild is not LeafBlock leafBlock)
{
@JamesNK
JamesNK / configuration.md
Last active October 28, 2024 10:51
.NET Aspire app host configuration

General

ASPIRE_ALLOW_UNSECURED_TRANSPORT

  • Description: Allows communication with the app host without https. ASPNETCORE_URLS (dashboard address) and DOTNET_RESOURCE_SERVICE_ENDPOINT_URL (app host resource service address) must be secured with HTTPS unless true.
  • Default: false

Resource service

DOTNET_RESOURCE_SERVICE_ENDPOINT_URL

  • Description: Configures the address of the resource service hosted by the app host. Automatically generated with launchSettings.json to have a random port on localhost. For example, https://localhost:17037.
@JamesNK
JamesNK / spec.md
Last active June 19, 2023 23:56
System.Net.Http metrics enrichment with callbacks

System.Net.Http:

// Need to support adding multiple callbacks to support adding metrics from multiple places:
// 1. Creating HttpRequestMessage
// 2. During HTTP request pipeline in delegating handlers
// 3. In diaganostic event callbacks
public static class HttpOptionsExtensions
{
    public static void AddCustomMetricsTagsCallback(
@JamesNK
JamesNK / Program.cs
Created May 19, 2023 04:04
Detect HttpClient response end
using System.Diagnostics;
using System.Net;
var stopwatch = Stopwatch.StartNew();
var endCallback = () => Console.WriteLine($"{stopwatch.ElapsedTicks} - Response ended");
var httpClient = new HttpMessageInvoker(new DetectEndRequestHandler(new SocketsHttpHandler(), endCallback));
Console.WriteLine($"{stopwatch.ElapsedTicks} - Send request");
var response = await httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, "https://www.google.com"), CancellationToken.None);
Console.WriteLine($"{stopwatch.ElapsedTicks} - Response headers received");
@JamesNK
JamesNK / validate-schema.ps1
Last active July 1, 2021 15:38
Validate Schema from PowerShell
$ErrorActionPreference = "Stop"
$NewtonsoftJsonPath = Resolve-Path -Path "bin\Newtonsoft.Json.dll"
$NewtonsoftJsonSchemaPath = Resolve-Path -Path "bin\Newtonsoft.Json.Schema.dll"
Add-Type -Path $NewtonsoftJsonPath
Add-Type -Path $NewtonsoftJsonSchemaPath
$source = @'
public class Validator
@JamesNK
JamesNK / Newtonsoft.Json.nuspec
Last active March 22, 2021 04:33
The Great Newtonsoft.Json.nuspec Collaboration
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>Newtonsoft.Json</id>
<version></version>
<title>Json.NET</title>
<description>Json.NET is a popular high-performance JSON framework for .NET</description>
<authors>James Newton-King</authors>
<language>en-US</language>
<projectUrl>http://www.newtonsoft.com/json</projectUrl>
BenchmarkDotNet=v0.11.4, OS=Windows 10.0.18363
AMD Ryzen 7 1700, 1 CPU, 16 logical and 8 physical cores
.NET Core SDK=3.1.400-preview-015151
[Host] : .NET Core 2.1.17 (CoreCLR 4.6.28619.01, CoreFX 4.6.28619.01), 64bit RyuJIT
DefaultJob : .NET Core 2.1.17 (CoreCLR 4.6.28619.01, CoreFX 4.6.28619.01), 64bit RyuJIT
Before:
| Method | messageCount | Mean | Error | StdDev | Gen 0/1k Op | Gen 1/1k Op | Gen 2/1k Op | Allocated Memory/Op |
|--------------------------------------------------------------------- |------------- |--------------:|--------------:|--------------:|------------:|------------:|------------:|--------------------:|
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
static class Program
#region Copyright notice and license
// Copyright 2019 The gRPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
@JamesNK
JamesNK / ZalgoParameterTransformer.cs
Created September 26, 2018 22:31
ZalgoParameterTransformer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Routing;
namespace MvcSandbox
{