Skip to content

Instantly share code, notes, and snippets.

Stephen Cleary StephenCleary

Block or report user

Report or block StephenCleary

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@StephenCleary
StephenCleary / Try.cs
Last active Mar 16, 2019
Try (error) monad in C#
View Try.cs
public static class Try
{
public static Try<T> FromException<T>(Exception exception) => Try<T>.FromException(exception);
public static Try<T> FromValue<T>(T value) => Try<T>.FromValue(value);
public static Try<T> Create<T>(Func<T> func) => Try<T>.Create(func);
}
public sealed class Try<T>
{
public static Try<T> FromException(Exception exception) => new Try<T>(exception, default, true);
@StephenCleary
StephenCleary / InMemoryConfiguration.cs
Created Sep 6, 2017
AddJson and AddJsonObject extension methods for IConfigurationBuilder
View InMemoryConfiguration.cs
public sealed class InMemoryFileProvider : IFileProvider
{
private readonly string _json;
public InMemoryFileProvider(string json)
{
_json = json;
}
public IFileInfo GetFileInfo(string subpath) => new FileInfo(_json);
@StephenCleary
StephenCleary / Auth0Authenticator.cs
Created Sep 6, 2017
Auth0 authenticator for use with Azure Functions
View Auth0Authenticator.cs
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Protocols;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Microsoft.IdentityModel.Tokens;
@StephenCleary
StephenCleary / JsonCreationConverter.cs
Created Sep 6, 2017
A JsonConverter that determines the type of the deserialized object by its JSON representation. Combination of several approaches including CustomCreatorConverter and https://stackoverflow.com/questions/22537233/json-net-how-to-deserialize-interface-property-based-on-parent-holder-object/22539730#22539730
View JsonCreationConverter.cs
/// <summary>
/// Creates a custom object based on the json values.
/// </summary>
/// <typeparam name="T">The object type to convert.</typeparam>
public abstract class JsonCreationConverter<T> : JsonConverter
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
@StephenCleary
StephenCleary / coordination.ts
Created Aug 18, 2017
AsyncEx... for TypeScript
View coordination.ts
class Future<T> {
private resolver: (value: T) => void; // readonly
private rejecter: (error: Error) => void; // readonly
private _isCompleted: boolean = false;
constructor() {
this.promise = new Promise((resolve, reject) => {
this.resolver = resolve;
this.rejecter = reject;
});
@StephenCleary
StephenCleary / AnonymousDisposable.cs
Created Mar 24, 2017
LockAsync abstraction around SemaphoreSlim
View AnonymousDisposable.cs
using System;
using System.Threading;
/// <summary>
/// A base class for disposables that need exactly-once semantics in a threadsafe way. All disposals of this instance block until the disposal is complete.
/// </summary>
/// <remarks>
/// <para>If <see cref="Dispose()"/> is called multiple times, only the first call will execute the disposal code. Other calls to <see cref="Dispose()"/> will wait for the disposal to complete.</para>
/// </remarks>
public sealed class AnonymousDisposable : IDisposable
@StephenCleary
StephenCleary / async.cs
Last active Feb 9, 2017 — forked from davidfowl/async.cs
Async quiz: How many threads are used? What does it print?
View async.cs
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
// The more evil version. :)
class Program
{
static void Main(string[] args)
{
View CustomerOrderDetailsViewModel.cs
internal class AwaitableMessages
{
public static Task<T> NextMessageAsync<T>()
{
var tcs = new TaskCompletionSource<T>();
Messenger.Default.Register<T>(null, item => tcs.TrySetResult(item));
return tcs.Task;
}
}
@StephenCleary
StephenCleary / UnitTests.cs
Created Mar 3, 2015
How Task.Run responds to CancellationToken
View UnitTests.cs
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class TaskRunCancellationTokenUnitTests
{
[TestMethod]
public void CancellationTokenPassedToSynchronousTaskRun_CancelsTaskWithTaskCanceledException()
@StephenCleary
StephenCleary / UnitTests.cs
Last active Aug 29, 2015
Wait/Result can expect OperationCanceledException as its AggregateException.InnerException, regardless of how the cancellation was done.
View UnitTests.cs
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class WaitCanTreatInnerExceptionAsOperationCanceledException
{
[TestMethod]
public void CancellationTokenPassedToStartNew_CanTreatAsOperationCanceledException()
You can’t perform that action at this time.