Skip to content

Instantly share code, notes, and snippets.

@mrpmorris
mrpmorris / EnumDescriptions.cs
Created May 6, 2022 08:16
Cached lookups for enum [Description] attributes
using System.Collections.Concurrent;
namespace Whatever;
public static class EnumDescriptions
{
private readonly static ConcurrentDictionary<Type, EnumDescriptionsCache> Cache = new();
public static IEnumerable<KeyValuePair<Enum, string>> GetValuesAndDescriptions(Type enumType) =>
GetOrAdd(enumType).GetAll();
@mrpmorris
mrpmorris / IntegrationTestsServer.cs
Created May 12, 2022 15:20
ASP.NET integration testing
namespace Whatever
public static class IntegrationTestsServer
{
public static string? IdentityConfirmationCode { get; private set; }
public static string? SignInOneTimeCode { get; private set; }
public static IOptions<GameServerOptions> GameServerOptions { get; set; }
private static readonly HttpClient HttpClient;
private static readonly IConfiguration Configuration;
@mrpmorris
mrpmorris / app.css
Created May 20, 2022 09:12
The CSS I put in the top of every new web app
:root {
box-sizing: border-box;
}
*, ::before, ::after {
box-sizing: inherit;
}
html {
scroll-behavior: smooth;
}
@mrpmorris
mrpmorris / Client.Program.cs
Created June 2, 2022 10:19
Slow TcpClient DNS resolution
using System.Net.Sockets;
int iterations = 10;
long totalElapsed = 0;
for (int i = 0; i < iterations; i++)
{
var tcpClient = new TcpClient();
var sw = System.Diagnostics.Stopwatch.StartNew();
await tcpClient.ConnectAsync("localhost", 6510);
long elapsed = sw.ElapsedMilliseconds;
@mrpmorris
mrpmorris / Program.cs
Created June 4, 2022 18:13
SpinLock extension without a ref
using ConsoleApp11;
var sl = new SpinLock();
for (int i = 0; i < 3; i++)
{
_ = Task.Run(() => DoSomething(ref sl));
}
Console.WriteLine("Ready");
Console.ReadLine();
open System
open System.IO
let printMeanScore(row: string) =
let columns = row.Split('\t')
let name = columns.[0]
let id = columns.[1]
let scores = columns |> Array.skip 2 |> Array.map float
let avg = scores |> Array.average
let min = scores |> Array.min
@mrpmorris
mrpmorris / SomeDialog.razor
Created June 14, 2022 10:52
Example of showing a modal and waiting for a result
<Morris.Blazor.Web.Modal.Modal Visible=@IsVisible title="Invite a user" aria-title="Invite a user">
<div class="card p-3 shadow">
<main>
<div class="modal-content">
<div class="modal-header card-header pb-2">
<h1 class="modal-title lead font-weight-bold" tabindex="-1">Invite a user</h1>
</div>
<div class="modal-body p-3">
</div>
<div class="modal-footer card-footer">
@mrpmorris
mrpmorris / StringHelper.cs
Created June 21, 2022 15:54
ObscureEmailAddress
public static class StringHelper
{
public const byte DefaultUnobscuredLength = 3;
public enum Keep { Start, End };
public static string? ObscureEmailAddress(string? value)
{
if (string.IsNullOrEmpty(value))
return value;
@mrpmorris
mrpmorris / IntegrationTestsServer.cs
Created July 21, 2022 15:37
Web integration testing with asp.net and WebSockets
public static class IntegrationTestsServer
{
static IntegrationTestsServer()
{
ConfigureMocks();
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "IntegrationTesting");
WebApplicationFactory<Program> appBuilder = new WebApplicationFactory<Program>()
.WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices(services =>
@mrpmorris
mrpmorris / DisposableAction.cs
Created September 9, 2022 14:37
DisposableAction
public sealed class DisposableCallback : IDisposable
{
private readonly Action Action;
private readonly string CallerFilePath;
private readonly int CallerLineNumber;
private readonly bool WasCreated;
private bool IsDisposed;
/// <summary>
/// Creates an instance of the class