Skip to content

Instantly share code, notes, and snippets.

@benfoster
benfoster / gist:9543337
Created March 14, 2014 07:16
Resolving backpressure manually in node.js
var http = require('http'),
fs = require('fs');
var server = http.createServer(function(request, response) {
var file = fs.createWriteStream('upload.jpg'),
fileBytes = request.headers['content-length'],
uploadedBytes = 0;
request.on('data', function(chunk) {
uploadedBytes += chunk.length;

Keybase proof

I hereby claim:

  • I am benfoster on github.
  • I am benfoster (https://keybase.io/benfoster) on keybase.
  • I have a public key ASAHVC9RNoa9g8n5MtUGFrXPGegaMX-gzzUSdmnbeKJSwgo

To claim this, I am signing this object:

@benfoster
benfoster / pipeline.cs
Created June 23, 2016 07:24
c# Pipeline
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PipelineDemo
{
// http://www.rantdriven.com/post/2009/09/16/Simple-Pipe-and-Filters-Implementation-in-C-with-Fluent-Interface-Behavior.aspx
public interface IFilter<TContext>
{
@benfoster
benfoster / gist:7562771
Created November 20, 2013 13:02
Testing custom Authorize Attribute in ASP.NET Web API
using NSubstitute;
using NUnit.Framework;
using System.Collections.ObjectModel;
using System.Net;
using System.Net.Http;
using System.Security.Claims;
using System.Security.Principal;
using System.Threading;
using System.Web.Http;
using System.Web.Http.Controllers;
@benfoster
benfoster / gist:4488755
Created January 8, 2013 22:48
Patching in ASP.NET Web API
using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
@benfoster
benfoster / gist:4017221
Created November 5, 2012 13:36
Complex validation in ASP.NET Web API
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Dependencies;
@benfoster
benfoster / gist:4416655
Last active June 19, 2021 18:32
A lightweight message bus using TPL DataFlow
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
namespace TDFDemo
{
class Program
{
@benfoster
benfoster / Global.asax.cs
Created February 10, 2016 10:33
Theme-able View Engine in ASP.NET MVC 4/5
private void InitializeViewEngine()
{
HostingEnvironment.RegisterVirtualPathProvider(new DynamicAssetVirtualPathProvider());
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new ThemableViewEngine
{
Theme = ctx => DependencyResolver.Current.GetService<ISiteContext>().Theme.ThemeName
});
public static void Execute(ChargeCommand command)
{
var pipeline = new PipelineBuilder<ChargeContext>()
.Register(new TimingHandler())
.Register(new LoggingHandler())
.Register(new ValidationHandler(
validationPipeline =>
{
validationPipeline.Register(new AmountValidator(maxAmount: 500));
}
@benfoster
benfoster / gist:9304548
Created March 2, 2014 10:19
Quick and easy OWIN pipeline hooks
using AppFunc = Func<IDictionary<string, object>, Task>;
public static class OwinPipelineHookExtensions
{
public static IAppBuilder UseHooks(
this IAppBuilder app,
Action<IDictionary<string, object>> before = null,
Action<IDictionary<string, object>> after = null)
{
return app.Use(new Func<AppFunc, AppFunc>(next => (async env =>