Skip to content

Instantly share code, notes, and snippets.

View SteveDunn's full-sized avatar

Steve Dunn SteveDunn

View GitHub Profile
@SteveDunn
SteveDunn / detect_disconnect.cs
Last active May 4, 2022 10:34
WebApi client disconnection
[HttpGet(Name = "GetWeatherForecast")]
public async Task<IEnumerable<WeatherForecast>> Get()
{
//pass this around to long running tasks
CancellationToken cancellationToken = HttpContext.RequestAborted;
if (cancellationToken.IsCancellationRequested)
{
throw null!; // or whatever you want to do
}
return await DoItSlowly(++_callerCount, cancellationToken);
@SteveDunn
SteveDunn / 1_Program.cs
Last active March 5, 2022 07:12
In response to an .NET Runtime API request (https://github.com/dotnet/runtime/issues/62112), The proposal was for a way to use _either_ the default values in a list, **or** the values bound from configuration. This simple gist shows an alternative, that is simply a custom `ICollection`
using System.Collections;
using Microsoft.Extensions.Configuration;
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false);
var options = new MyImageProcessingOptions();
var config = builder.Build();
using System;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
namespace benchmarks
{
[MemoryDiagnoser]
public class ThrowHelperBenchmarks
{
// [GlobalSetup]
@SteveDunn
SteveDunn / GenericConstructorHelper.cs
Created July 30, 2021 21:23
Workaround for inability to create C# generic constraints for the presence of a constructor that takes parameters... i.e. Foo<T>(string name) where T: new(string)
public Func<T1, TResult> CompileConstructor<T1, TResult>()
{
var type1 = typeof(T1);
var parameter1 = Expression.Parameter(type1);
var constructor = typeof(TResult).GetConstructor(new Type[] { type1 });
var body = Expression.New(constructor, parameter1);
var lambda = Expression.Lambda<Func<T1, TResult>>(body, parameter1);
var method = lambda.Compile();
return method;
}
@SteveDunn
SteveDunn / WithCacheCowClient.cs
Created July 23, 2021 20:29
CacheCow client with authentication
public class RunWithCacheCow
{
private readonly HttpClient _client;
public RunWithCacheCow()
{
var httpClientHandler = new HttpClientHandler()
{
Credentials = new NetworkCredential("test", "test"),
};
@SteveDunn
SteveDunn / output.xml
Created July 8, 2021 06:26
The 'preprocess' output from msbuild for a vanilla C# console app
This file has been truncated, but you can view the full file.
<!--
============================================================================================================================================
C:\temp\consoleapp1\consoleapp1.csproj
============================================================================================================================================
-->
<Project DefaultTargets="Build">
<!--
============================================================================================================================================
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk">
This import was added implicitly because the Project element's Sdk attribute specified "Microsoft.NET.Sdk".
@SteveDunn
SteveDunn / convert.cs
Last active March 28, 2020 04:58
A JSON.net JsonConverter that serialises and deserialises a dictionaries where the key is a non-primitive/complex type
/// <summary>
/// Represents a JSON.net <see cref="JsonConverter"/> that serialises and deserialises a <see cref="Dictionary{TKey,TValue}"/>, where
/// <typeparamref name="TKey"/> is a non-primitive type, i.e. a type that is not a string, int, etc.
/// JSON.net uses the string representation of dictionary keys, which can cause problems with complex (non-primitive types).
/// You could override ToString, or add attributes to your type to overcome this problem, but the solution that this type
/// solves is for when you don't have access to the type being [de]serialised.
/// This solution was based on this StackOverflow answer (added the deserialisation part): https://stackoverflow.com/a/27043792/28901
/// </summary>
/// <typeparam name="TKey">The type of the key. Normally a complex type, but can be anything. If it's a non complex type, then this converter isn't needd.</typeparam>
/// <typeparam name="TValue">The value</typeparam>
void onCanvasDraw(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args)
{
args.DrawingSession.Transform = _scale;
args.DrawingSession.Clear(Colors.Aquamarine);
using (var offscreenSession = _offscreenCanvas.CreateDrawingSession())
{
offscreenSession.Clear(Colors.Aquamarine);
choco install chocolatey
choco install googlechrome
choco install 7zip
choco install vlc
choco install sublimetext3
choco install paint.net
choco install tortoisesvn
choco install VirtualCloneDrive
choco install FoxitReader
choco install beyondcompare
@SteveDunn
SteveDunn / Guard.cs
Created February 7, 2012 12:48
A utility class to validate parameters (add to your projects as a linked file)
using System;
using System.Collections ;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis ;
using System.Linq ;
using JetBrains.Annotations ;
// PLEASE DON'T MAKE ANYTHING PUBLIC IN THIS FILE. THIS FILE (AND JETBRAINS.ANNOTATIONS) IS INTENDED
// TO BE INCLUDED AS A LINKED FILE INTO WHATEVER PROJECTS NEEDS THEM.