Skip to content

Instantly share code, notes, and snippets.

View Shazwazza's full-sized avatar

Shannon Deminick Shazwazza

View GitHub Profile
@Shazwazza
Shazwazza / TaskHelpers
Created May 9, 2014 01:13
Snippet of System.Threading.Tasks.TaskHelpers used in Web Api formatters to RunSynchronously
internal static Task<TResult> RunSynchronously<TResult>(Func<TResult> func, CancellationToken cancellationToken = default(CancellationToken))
{
if (cancellationToken.IsCancellationRequested)
{
return Canceled<TResult>();
}
try
{
return FromResult(func());
@Shazwazza
Shazwazza / SyncTask
Created May 9, 2014 01:19
Returns a synchronous Task instance
var task = new Task(() =>
{
//do stuff;
});
task.RunSynchronously();
return task;
@Shazwazza
Shazwazza / gist:cdb64b221f8fdc568acf
Created May 4, 2015 04:25
hal with templated embedded resource hrefs
{
"TotalResults": 14,
"TotalPages": 3,
"Page": 1,
"_links": {
"self": {
"href": "/beers?page=1"
},
"next": {
"href": "/beers?page=2"
@Shazwazza
Shazwazza / gist:6d75bca3f98d7ef98378
Created June 11, 2015 14:57
Umbraco 7.3 - Enable DatabaseServerMessenger
public class LoadBalancingConfigurationEventHandler : ApplicationEventHandler
{
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
//Replace the server messenger:
ServerMessengerResolver.Current.SetServerMessenger(new BatchedDatabaseServerMessenger(
applicationContext,
true,
//You can customize some options by setting the options parameters here:
@Shazwazza
Shazwazza / gist:931885e682b8fe34220e
Created September 10, 2014 00:01
CachedPartial with contextualKeyBuilder
@Html.CachedPartial(
//partial view name
"myPartialView",
//the model to pass the view - this will be the same object passed to the contextualKeyBuilder delegate
Model,
//seconds to cache
123,
//the key builder delegate, accepts the model passed in to the cached partial and the current
// viewdata dictionary of the partial view
contextualKeyBuilder: (model, viewdata) =>
@Shazwazza
Shazwazza / cast-vs-oftype.md
Last active January 4, 2017 07:06
Linq Cast vs OfType

With BencharkDot net, this benchmark creates a list of 10,000 strings then tests either OfType<T> or Cast<T>

Here's the benchmark code:

/// <summary>
/// Want to check what is faster OfType or Cast when a enurable all has the same items
/// </summary>
[Config(typeof(Config))]
public class LinqCastBenchmarks
@Shazwazza
Shazwazza / DisableUserPermissionsNodes.cs
Created May 18, 2017 02:00
For Umbraco, the ability to not render any user permissions tree nodes based on the current user
using System.Threading;
using umbraco.cms.presentation.Trees;
using Umbraco.Core;
using Umbraco.Core.Security;
namespace Test
{
public class MyStartup : ApplicationEventHandler
{
/// <summary>
@Shazwazza
Shazwazza / UsingUmbracoRelationService.cs
Created May 18, 2017 01:22
Using the Umbraco Relation Service to relate items on a saved event based on property values
using System;
using System.Linq;
using System.Text.RegularExpressions;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using HtmlAgilityPack;
using Umbraco.Web;
using Umbraco.Web.Templates;
@Shazwazza
Shazwazza / MyHttpModule.cs
Last active December 15, 2020 13:43
How to change HttpRequest.IsSecureConnection
// Install this http module which will replace the HttpContext.Current at the beginning
// of the request with a wrapped one
public class MyHttpModule : IHttpModule
{
private static Lazy<FieldInfo> _workerRequestType = new Lazy<FieldInfo>(()
=> typeof(HttpContext).GetField("_wr", BindingFlags.NonPublic | BindingFlags.Instance));
public void Init(HttpApplication context)
{
context.BeginRequest += Context_BeginRequest;
@Shazwazza
Shazwazza / UserAdminCreateOnly.cs
Created May 18, 2017 01:51
Using Umbraco events to enforce a specific user to only being allowed to create users but not update them
using System.Threading;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Core.Security;
namespace Test
{
public class MyStartup : ApplicationEventHandler
{