Skip to content

Instantly share code, notes, and snippets.

View DamianEdwards's full-sized avatar
🏠
Working from home

Damian Edwards DamianEdwards

🏠
Working from home
View GitHub Profile
@DamianEdwards
DamianEdwards / TrimMessagesTrigger.sql
Last active December 14, 2015 12:38
Trigger to delete old messages from a table, effectively turning it into a ring buffer
CREATE TRIGGER [SignalR].[TrimMessages_3]
ON [SignalR].[Messages_3]
AFTER INSERT
AS
BEGIN
DECLARE @MaxTableSize int,
@BlockSize int,
@MaxPayloadId bigint;
SET @MaxTableSize = 10000;
@DamianEdwards
DamianEdwards / install.sql
Last active December 14, 2015 14:58
SignalR SQL scale-out message bus install script
-- Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
DECLARE @SCHEMA_NAME nvarchar(32),
@SCHEMA_ID int,
@SCHEMA_TABLE_NAME nvarchar(100),
@TARGET_SCHEMA_VERSION int,
@CREATE_MESSAGE_TABLE_DML nvarchar(1000),
@MESSAGE_TABLE_NAME nvarchar(100),
@MESSAGE_TABLE_COUNT int;
@DamianEdwards
DamianEdwards / Program.cs
Last active December 15, 2015 03:19
Demonstrates the behavior of SqlDataReader when inserts are happening during the Read() loop. Even with ReadUncommitted isolation for the consumer, the only records returned are those that existed when ExecuteReader() was called. UPDATE: Seems with big enough numbers the behavior can change, presumably due to how SQL decides to do locking (row, …
using System;
using System.Data;
using System.Data.SqlClient;
using System.Threading;
namespace DataReaderBehavior
{
class Program
{
private static readonly string _connectionString = @"Data Source=(local);Initial Catalog=master;Integrated Security=SSPI;";
@DamianEdwards
DamianEdwards / Program.cs
Created March 19, 2013 05:54
Demonstrates that SqlDependency notifications can occur concurrently with the command that set them up. This might cause issues in certain cases if the application isn't expecting multiple commands to be executed at the same time.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Threading;
namespace SqlDependencyConcurrency
{
class Program
{
private static readonly string _connectionString = "Data Source=(local);Initial Catalog=Sample;Integrated Security=SSPI;";
@DamianEdwards
DamianEdwards / Program.cs
Last active December 15, 2015 12:29
Number crazy
using System;
namespace NumberCrazy
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Number(1) == 1: {0}", new Number(1) == 1);
Console.WriteLine("Number(2) > 1: {0}", new Number(2) > 1);
@DamianEdwards
DamianEdwards / _use.js
Last active December 16, 2015 19:30
SignalR client hub invocation pipeline
var conn = $.hubConnection("url"),
hub = conn.createHubProxy("chat");
hub.pipeline.use({
incoming: function(next) {
return function(context) {
console.log("Before incoming invocation for method " + context.methodName + " on hub " + context.hubName + " with " + context.args.length + " arguments");
next(context);
console.log("After incoming invocation for method " + context.methodName + " on hub " + context.hubName + " with " + context.args.length + " arguments");
};
@DamianEdwards
DamianEdwards / Default.aspx
Created July 17, 2013 04:00
Simple way to do async UI loading in ASP.NET Web Forms with standard controls
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Threading" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
uint viewIndex = 0;
uint.TryParse(Request.QueryString["view"], out viewIndex);
views.ActiveViewIndex = (int)(viewIndex < views.Views.Count ? viewIndex : 0);
@DamianEdwards
DamianEdwards / MyHub.cs
Created July 31, 2013 23:51
Hub methods with progress support
public class MyHub : Hub
{
// The hub method must be Task returning and accept an IProgress<T>
public async Task DoLongRunningThing(IProgress<int> progress)
{
for (var i = 0; i <= 100; i+=5)
{
await Task.Delay(200);
progress.Report(i);
}
public class ChatViewModel : IChatClient
{
private readonly ObservableCollection<Message> _messages = new ObservableCollection<Message>();
public IObservableCollection<Message> Messages
{
get { return _messages; }
}
void IChatClient.NewMessage(string message, string user)
@DamianEdwards
DamianEdwards / Helpers.cs
Created February 20, 2014 19:39
Razor ideas
[TagHelper("@form")]
public static TagBuilder FormTagHelper(this RazorPage page, TagBuilder element, string action, string controller, IDictionary<string, string> route)
{
element.Attributes["action"] = Url.Action(action, controller, route);
// Could implicitly write out anti-forgery token as nested hidden input field here too
}
[TagHelper("@label")]
public static TagBuilder LabelTagHelper(this RazorPage page, TagBuilder element, string name, string @class)