Skip to content

Instantly share code, notes, and snippets.

View jchadwick's full-sized avatar

Jess Chadwick jchadwick

View GitHub Profile
@jchadwick
jchadwick / gist:2345865
Created April 9, 2012 19:25
IsAppHarbor
bool IsAppHarbor
{
get { return !string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["appharbor.commit_id"]); }
}
@jchadwick
jchadwick / EventHandlerExtensions.cs
Created April 18, 2012 20:55
Extension Methods to make triggering event handlers easier
using System;
public static class EventHandlerExtensions
{
public static void SafeInvoke(this EventHandler handler, EventArgs args = null, object sender = null)
{
if (handler != null)
handler(sender, args ?? EventArgs.Empty);
}
@jchadwick
jchadwick / ConcurrentList.cs
Created April 19, 2012 18:11
Thread-safe List (ConcurrentList<T>)
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
public class ConcurrentList<T> : ICollection<T>
{
private readonly ConcurrentDictionary<T, object> _store;
@jchadwick
jchadwick / JsonMessageFormatter.cs
Created April 20, 2012 18:47
MSMQ Message JSON Formatter
using System;
using System.IO;
using System.Messaging;
using System.Text;
using Newtonsoft.Json;
public class JsonMessageFormatter : IMessageFormatter
{
private static readonly JsonSerializerSettings DefaultSerializerSettings =
new JsonSerializerSettings {
@jchadwick
jchadwick / ControlCommand.cs
Created April 20, 2012 19:11
Facade for Cross-Queue MSMQ communication
public struct ControlCommand
{
public static readonly ControlCommand Message = 0;
private readonly int _value;
public ControlCommand(int value)
{
_value = value;
}
@jchadwick
jchadwick / InMemoryAspNet.cs
Created May 16, 2012 19:24
In-Memory ASP.NET Website
using System;
using System.Web;
using System.Web.Hosting;
using System.IO;
public class InMemoryAspNet
{
public string PhysicalDirectory { get; set; }
public InMemoryAspNet()
@jchadwick
jchadwick / MarkdownRazorViewEngine.cs
Created May 25, 2012 18:25
ViewEngine with Markdown and Razor support
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using MarkdownDeep;
public class MarkdownRazorViewEngine : RazorViewEngine
{
@jchadwick
jchadwick / gist:2889702
Created June 7, 2012 16:09
Promote XElement to attribute
private static void PromoteElementToAttribute(XElement element, string xname)
{
var names = element.Descendants(xname).ToArray();
foreach(var name in names)
{
if (name != null && name.Parent != null)
{
name.Parent.SetAttributeValue(xname, name.Value);
name.Remove();
}
@jchadwick
jchadwick / gist:2956175
Created June 19, 2012 19:55
Get Project path for VS Test Runner
// Yeah, this is crazy, right? Stupid Visual Studio test runner...
var testPathReplacer = new System.Text.RegularExpressions.Regex(@"\\(?:(TestResults\\[^\\]*\\Out)|([^\\]*\\bin\\[^\\]*))");
var assemblyDirectory = Path.GetDirectoryName(GetType().Assembly.Location);
assemblyDirectory = testPathReplacer.Replace(assemblyDirectory, string.Empty);
assemblyDirectory = Path.Combine(assemblyDirectory, PROJECT_DIRECTORY);
@jchadwick
jchadwick / QueuedService.cs
Last active January 11, 2019 15:03
A helper class that provides delayed processing of a message via MSMQ
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Messaging;
using NLog;
/// <summary>
/// A facade over Microsoft's MSMQ
/// </summary>
public abstract class QueuedService : IDisposable