Skip to content

Instantly share code, notes, and snippets.

View martincostello's full-sized avatar
🚀
Coding stuff

Martin Costello martincostello

🚀
Coding stuff
View GitHub Profile
@martincostello
martincostello / verify-nuget-attestations.ps1
Last active November 6, 2025 11:00
Verify NuGet Package DLL Attestations
<#
.SYNOPSIS
Downloads a specific NuGet package (.nupkg), extracts it, and runs
`gh attestation verify --owner <owner> <file>` for each .dll found under the extracted contents.
Writes an error to the console if `gh` returns a non-zero exit code for any file,
and exits with non-zero code if any verification fails.
.PARAMETER Package
The NuGet package ID (e.g. Polly.Core).
@martincostello
martincostello / BigIntegerBenchmarks.cs
Created August 16, 2024 16:32
BigInteger go brrr in .NET 9
using System.Numerics;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Jobs;
namespace DotNetBenchmarks;
[MemoryDiagnoser]
[SimpleJob(RuntimeMoniker.Net80, baseline: true)]
[SimpleJob(RuntimeMoniker.Net90)]
@martincostello
martincostello / OpenApiYamlEndpoint.cs
Created July 31, 2024 18:58
Generate YAML using Microsoft.AspNetCore.OpenApi
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Writers;
namespace Microsoft.AspNetCore.OpenApi;
public static class OpenApiYamlEndpoint
{
@martincostello
martincostello / NSubstituteExtensions.cs
Last active October 31, 2025 13:19
Mocking Moq for NSubstitute
// Copyright (c) Martin Costello, 2023. All rights reserved.
// Licensed under the Apache 2.0 license.
using System.Linq.Expressions;
using NSubstitute.Core;
using NSubstitute.ExceptionExtensions;
namespace NSubstitute;
/// <summary>
@martincostello
martincostello / AddNumbers.Tests\AddNumbers.Tests.csproj
Created December 4, 2021 15:34
An extension method to turn a string into a sequence of numbers using .NET 6 Generic Math
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<EnablePreviewFeatures>true</EnablePreviewFeatures>
<ImplicitUsings>enable</ImplicitUsings>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Shouldly" Version="4.0.3" />
</ItemGroup>
<ItemGroup>
@martincostello
martincostello / SystemTextJsonContentSerializerForSourceGenerator.cs
Last active August 28, 2024 12:57
A class for plugging the JSON source generator with Refit
using System.Net;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Refit;
/// <summary>
/// A class representing an implementation of <see cref="IHttpContentSerializer"/> that can be
/// used with the <c>System.Text.Json</c> source generators. This class cannot be inherited.
@martincostello
martincostello / HttpResponseJsonExtensions.cs
Last active November 28, 2021 09:26
Extensions for using the JSON source generator with Minimal APIs
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
namespace Microsoft.AspNetCore.Http;
/// <summary>
/// Provides extension methods for writing a JSON serialized value to the HTTP response.
/// </summary>
/// <remarks>
@martincostello
martincostello / Program.cs
Last active May 21, 2022 10:54
Converts C# source files to use file-scoped namespaces
// There may be corner cases where the naive string matching incorrectly
// changes your source file. Check your diff before committing any changes.
using System.Text;
// Change to whatever your favourite indentation is
const string Indent = " ";
// Get all the C# files in the specified directory
var fileNames = Directory.GetFiles(args[0], "*.cs", SearchOption.AllDirectories);
@martincostello
martincostello / Console.Tests\Console.Tests.csproj
Last active June 22, 2021 16:31
.NET global usings for code and tests
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
</ItemGroup>
@martincostello
martincostello / update-dotnet-sdk.yml
Last active November 14, 2020 14:26
GitHub Actions workflow to update the .NET SDK
name: update-dotnet-sdk
on:
schedule:
- cron: '0 * * * *'
workflow_dispatch:
jobs:
update-dotnet-sdk:
name: Update .NET SDK
runs-on: ubuntu-latest
steps: