Skip to content

Instantly share code, notes, and snippets.

View droyad's full-sized avatar

Robert Wagner droyad

View GitHub Profile
var all = @"// redacted";
var firstLetters = all
.Split('\n')
.Select(n => n.Trim().ToUpper())
.Select(n => n.Substring(n.IndexOf(" ") + 1)[0]);
var counts = from l in firstLetters
group l by l into g
select new
@droyad
droyad / gist:6602748
Created September 18, 2013 00:26
Scale image
var size = image.Size;
var aspectRatio = size.Width / size.Height;
var newSize = aspectRatio > 1
? new SizeF(maxHeightOrWidth, maxHeightOrWidth / aspectRatio)
: new SizeF(maxHeightOrWidth * aspectRatio, maxHeightOrWidth);
private void RestoreDatabase(Server server)
{
Program.Log("Restoring {0} on {1}".With(_options.Database, _options.Server));
if (server.Databases[_options.Database] != null)
server.KillAllProcesses(_options.Database);
else
new Database(server, _options.Database).Create();
@droyad
droyad / gist:6162191
Created August 6, 2013 05:13
Exception format
public static string Format(this Exception exception, bool includeStackTrace = false)
{
var sb = new StringBuilder();
Format(sb, exception, false);
if (includeStackTrace)
{
sb.AppendLine();
Format(sb, exception, true);
}
return sb.ToString();
@droyad
droyad / knockout-tags.js
Created August 2, 2013 00:02
Knockout binding for Tagit
ko.bindingHandlers.tags = {
init: function (element, valueAccessor, allBindingsAccessor) {
var bind = function() {
var observable = valueAccessor();
observable($(element).tagit("assignedTags").join(','));
};
var options = {
allowSpaces: true,
@droyad
droyad / gist:6136445
Created August 2, 2013 00:02
Knockout binding for Tagit
ko.bindingHandlers.tags = {
init: function (element, valueAccessor, allBindingsAccessor) {
var bind = function() {
var observable = valueAccessor();
observable($(element).tagit("assignedTags").join(','));
};
var options = {
allowSpaces: true,
@droyad
droyad / gist:5899850
Last active December 19, 2015 04:49
Reimplementation of NewGuid()
public static Guid NewHotGuid()
{
using (var client = new HttpClient())
{
var result = client.GetStringAsync("http://secretgeek.net/HotGuids/").Result;
var guid = Regex.Match(result, @"id='guid'\>\{([^\}]+)").Groups[1].Value;
return Guid.Parse(guid);
}
}
@droyad
droyad / gist:5627410
Last active December 17, 2015 14:49
Registering a WebAPI filter with #autofac based on action method attributes
builder.RegisterAuthorizationFilterForAttribute<MustBeLoggedInFilter, MustBeLoggedInAttribute>(ThisAssembly);
public static class AutofacExtensions
{
public static IRegistrationBuilder<object, IConcreteActivatorData, SingleRegistrationStyle> RegisterAuthorizationFilterForAttribute<TService, TAttribute>(
this ContainerBuilder builder,
params Assembly[] controllerAssemblies
) where TAttribute : Attribute
public static void ForSingleMaybe<T>(this IEnumerable<T> sequence, Action<T> action)
{
using(var enumerator = sequence.GetEnumerator())
{
if(!enumerator.MoveNext())
return;
var item = enumerator.Current;
if(enumerator.MoveNext())
throw new InvalidOperationException("Sequence contains too many elements");
void Main()
{
var result = MakeCalls("ABCDEFGHIJKLMNOP", c => CallWebService(c), 2).Result;
result.Dump();
}
public async System.Threading.Tasks.Task<string> CallWebService(char c)
{
await System.Threading.Tasks.Task.Delay(2000);
return c.ToString() + DateTime.Now.Second.ToString();