Skip to content

Instantly share code, notes, and snippets.

@JayDouglass
JayDouglass / WCF_REST_Console_ServiceHost.cs
Created June 1, 2011 04:35
WCF REST service host from console
using System.Diagnostics;
using System.ServiceModel.Web;
using System.ServiceModel.Description;
namespace WCFRest.ConsoleServiceHost
{
class Program
{
static void Main(string[] args)
{
@JayDouglass
JayDouglass / WCF REST return HTML.cs
Created June 1, 2011 04:43
Output html from WCF REST service using stream
public Stream GetPage()
{
var page = Properties.Resources.Status; // page from resources
// Get site relative base url for use in javascript ajax requests
var baseUrl = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.BaseUri.AbsolutePath;
page = page.Replace("%BaseURL%", baseUrl);
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(page));
WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
@JayDouglass
JayDouglass / Information-Hiding-Javascript.js
Created June 1, 2011 04:58
Information hiding in JavaScript, private instance variables and methods
function ASDFClass() {
var self = this; // used when you want to reference an instance of this object from private methods
this.PublicIVar = "asdafd";
var privateIVar = "encapsulated";
function PrivateFunction() {
}
this.PublicFunction = function () {
@JayDouglass
JayDouglass / FormsAuthenticationExtensions.vb
Created August 30, 2011 21:28
Allows storing and retrieving an object in FormsAuthentication cookie
Imports Newtonsoft.Json
Imports System.Web
' Gets and auth cookie and stores an object in JSON format in userData
' Useful for storing ids, roles information, and email address
Public Class FormsAuthenticationExtensions
' Calls this to get cookie when logging a user in
Public Shared Function GetAuthCookie(ByVal userName As String, ByVal userData As Object, Optional ByVal createPersistentCookie As Boolean = True) As HttpCookie
Dim cookie = FormsAuthentication.GetAuthCookie(userName, createPersistentCookie)
Dim ticket = FormsAuthentication.Decrypt(cookie.Value)
@JayDouglass
JayDouglass / FindNthOccurenceOfWeekDayForMonth.cs
Created October 28, 2011 02:20
FindNthOccurenceOfWeekDayForMonth
public DateTime FindNthOccurenceOfWeekDayForMonth(int month, int year, DayOfWeek weekDay, int occurence)
{
if(occurence < 1)
{
throw new ArgumentOutOfRangeException("occurence must be greater than 0");
}
// iterate over days, beginning with the first day of the given month and year
// loop until occurence = 0
// when loop exits current will be set to the nth occurence of weekDay
@JayDouglass
JayDouglass / FileInfoExtensions.cs
Created July 11, 2012 15:19
Physical path to website relative path ASP.NET
public static partial class FileInfoExtensions
{
public static string ToWebSiteRelativePath(this FileInfo fileInfo)
{
var applicationPhysicalPath = HostingEnvironment.ApplicationPhysicalPath;
var filePhysicalPath = fileInfo.FullName;
if (filePhysicalPath.StartsWith(applicationPhysicalPath) == false)
throw new ArgumentOutOfRangeException(filePhysicalPath + " is not within application physical path: " + applicationPhysicalPath);
return filePhysicalPath.Replace(applicationPhysicalPath, HostingEnvironment.ApplicationVirtualPath + "/").Replace("\\", "/");
}
@JayDouglass
JayDouglass / DateTimeExtensions.cs
Created July 11, 2012 15:36
Start of week and end of week
namespace Extensions
{
public static class DateTimeExtensions
{
public static DateTime StartOfWeek(DateTime value)
{
var startOfWeek_ = DayOfWeek.Sunday;
var delta = value.DayOfWeek - startOfWeek_;
if (delta < 0)
@JayDouglass
JayDouglass / MissingIndexes.sql
Created July 16, 2012 15:23
Find missing indexes on SQL Server
-- http://www.i-programmer.info/programming/database/3208-improve-sql-performance-find-your-missing-indexes.html
SET TRANSACTION ISOLATION LEVEL
READ UNCOMMITTED
SELECT TOP 20
ROUND(s.avg_total_user_cost *
s.avg_user_impact
* (s.user_seeks + s.user_scans),0)
AS [Total Cost]
,d.[statement] AS [Table Name]
@JayDouglass
JayDouglass / SpreadsheetKeys.js
Created July 18, 2012 21:49
Spreadsheet behavior for gridview textboxes
(function(global, $){
var keyCodes = {
13: 'enter',
38: 'up',
40: 'down',
37: 'left',
39: 'right'
};
var spreadsheet = {
@JayDouglass
JayDouglass / CountTree.js
Created August 5, 2012 07:50
Nathans JS lessons 1 - count tree using recursion and iteration
/*
var count = function(tree) {
if(!tree) {
return 0;
}
return 1 + count(tree.left) + count(tree.right);
};
*/