Skip to content

Instantly share code, notes, and snippets.

View aliostad's full-sized avatar
🐒
I may be slow to respond.

Ali Kheyrollahi aliostad

🐒
I may be slow to respond.
View GitHub Profile
@aliostad
aliostad / BinaryMediaTypeFormatter
Created April 28, 2012 15:17
An ASP.NET Web API media type formatter for application/octet-stream
public class BinaryMediaTypeFormatter : MediaTypeFormatter
{
private static Type _supportedType = typeof (byte[]);
private bool _isAsync = false;
public BinaryMediaTypeFormatter() : this(false)
{
}
@aliostad
aliostad / ThreadExtensions.cs
Created June 9, 2012 16:47
ASYNC/AWAIT - To repro problems Tugberk experienced. VS2012 - .NET 4.5
using System.Diagnostics;
namespace System.Threading
{
public static class ThreadNameTracingExtensions
{
/// <summary>
/// if thread has a name, it leaves the name as it is. If it does not have a name,
/// it sets the thread's name to the module.method
/// It outputs as
@aliostad
aliostad / rotten_tomatoes.json
Created June 15, 2012 12:57
Result from Rotten Tomatoes search API
{"total":26,"movies":[{"id":"12897","title":"The Matrix","year":1999,"mpaa_rating":"R","runtime":136,"critics_consensus":"An ingenious combination of Hong Kong action, ground-breaking Hollywood FX, and an imaginative vision.","release_dates":{"theater":"1999-03-31","dvd":"1999-09-21"},"ratings":{"critics_rating":"Certified Fresh","critics_score":87,"audience_rating":"Upright","audience_score":81},"synopsis":"","posters":{"thumbnail":"http://content7.flixster.com/movie/16/90/52/1690525_mob.jpg","profile":"http://content7.flixster.com/movie/16/90/52/1690525_pro.jpg","detailed":"http://content7.flixster.com/movie/16/90/52/1690525_det.jpg","original":"http://content7.flixster.com/movie/16/90/52/1690525_ori.jpg"},"abridged_cast":[{"name":"Keanu Reeves","id":"162654049","characters":["Neo"]},{"name":"Laurence Fishburne","id":"162669090","characters":["Morpheus"]},{"name":"Carrie-Anne Moss","id":"162669130","characters":["Trinity"]},{"name":"Hugo Weaving","id":"162709905","characters":["Agent Smith"]},{"name":"Glori
@aliostad
aliostad / IEnumerableExtensions.cs
Created June 25, 2012 20:29
ForEachOne for IEnumerable<T> not having to use ToList().ForEach()
using System;
using System.Collections.Generic;
using System.Linq;
namespace System.Collections.Generics
{
public static class IEnumerableExtensions
{
public static IEnumerable<T> ForEachOne<T>(this IEnumerable<T> enumerable, Action<T> action)
{
@aliostad
aliostad / PollingWorker.cs
Created June 26, 2012 15:40
A class that creates a dedicated thread to poll
using System.Collections.Generic;
namespace System.Threading
{
public class PollingWorker : IPollingWorker,IDisposable
{
private class TickAction
{
public string Name { get; set; }
public Action Work { get; set; }
@aliostad
aliostad / gist:3202814
Created July 30, 2012 00:18
Serialisation and deserialisation of HTTP request and response messages in ASP.NET Web API
public interface IHttpMessageSerializer
{
void Serialize(HttpResponseMessage response, Stream stream);
void Serialize(HttpRequestMessage request, Stream stream);
HttpResponseMessage DeserializeToResponse(Stream stream);
HttpRequestMessage DeserializeToRequest(Stream stream);
}
public class MessageContentHttpMessageSerializer : IHttpMessageSerializer
@aliostad
aliostad / CacheCow.Client.Sample.cs
Created August 6, 2012 21:57
CacheCow.Client sample. Requires CacheCow.Client v>=0.1.3
using System;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using CacheCow.Client;
using CacheCow.Client.Headers;
namespace ConsoleApplication1
{
class Program
@aliostad
aliostad / CacheCow.Client.Sample.cs
Created August 6, 2012 21:57
CacheCow.Client sample. Requires CacheCow.Client v>=0.1.3
using System;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using CacheCow.Client;
using CacheCow.Client.Headers;
namespace ConsoleApplication1
{
class Program
@aliostad
aliostad / CommandLineParse.cs
Created August 17, 2012 10:54
Parses commandline into name value pairs
/// <summary>
/// Parses commandline parameters that are in format of
/// -name:value
/// /name=value
/// -name="value 1"
/// </summary>
public static class CommandLineParser
{
private const string CommandLinePattern =
"\\s+[-/](?<name>\\w+)[=:](?:(?<value>[^\\s\\:\"]+)|(?:\"(?<value>.*)\"))";
@aliostad
aliostad / app.config
Created August 29, 2012 11:30
Turn off simple.data tracing
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="simpleData">
<section name="simpleDataConfiguration" type="Simple.Data.SimpleDataConfigurationSection, Simple.Data"/>
</sectionGroup>
</configSections>
<simpleData>
<simpleDataConfiguration traceLevel="Error"/>
</simpleData>