Skip to content

Instantly share code, notes, and snippets.

View codingoutloud's full-sized avatar
💭
Building.

Bill Wilder codingoutloud

💭
Building.
View GitHub Profile
@codingoutloud
codingoutloud / BlobSemaphore.cs
Created February 13, 2013 20:02
Use Windows Azure Blob to create a semaphore spanning the cloud.
// intended to be called only ONCE in real installations - or for a clean test run when no Jobs container exists
public static void GloballyInitializeJobManager(bool quiet)
{
var blobContainer = AzureStorageAccess.GetBlobContainer(JobContainerName);
bool didNotExistCreated = blobContainer.CreateIfNotExist();
if (!quiet) System.Diagnostics.Debug.Assert(didNotExistCreated); // else, we probably should not be calling this method
var blob = blobContainer.GetBlobReference(JobGlobalJobIdSequencePath);
if (!blob.Exists())
{
blob.UploadText(JobGlobalJobIdSequenceStartingValue);
@codingoutloud
codingoutloud / AzureCustomRetryPolicy.cs
Created February 13, 2013 19:28
Show Custom Retry Policy for Windows Azure Storage SDK.
// Azure Storage SDK 1.7
public static RetryPolicy TrackingRetryExponential(int maxRetryCount, TimeSpan minBackoff, TimeSpan maxBackoff, TimeSpan deltaBackoff, ILog logger)
{
logger.InfoFormat(
"Setting TrackingRetryExponential: maxRetryCount = {0}, minBackoff = {1}, maxBackoff = {2}, deltaBackoff = {3}",
maxRetryCount, minBackoff, maxBackoff, deltaBackoff);
return () => (int currentRetryCount, Exception lastException, out TimeSpan retryInterval) =>
{
@codingoutloud
codingoutloud / blob-cache-control-header.cs
Created February 13, 2013 19:24
Snippet of Azure Client SDK 1.7 code that demonstrates setting ContentType and CacheControl headers.
// .NET Storage SDK 1.7
CloudBlob destBlob = TargetContainer.GetBlobReference(destFileName);
destBlob.Properties.ContentType = "image/jpeg";
destBlob.Properties.CacheControl = "x-ms-blob-cache-control: public, max-age=31556926"; // cache for up to one year
try
{
destBlob.UploadFile(sourceFilespec);
}
@codingoutloud
codingoutloud / temp-tidy.ps1
Created December 10, 2012 20:27
PowerShell script to tidy up the application TEMP area on Windows by deleting files older than 7 days
# The intent is that the command is to be scheduled to be run by the Windows Scheduler once a day: http://stackoverflow.com/a/8888886
# For Windows Azure, a new Windows Scheduler task is best added by a Startup Script and, like any other machine with the default PowerShell security settings, will need the PowerShell ExecutionPolicy configured: http://blogs.msdn.com/b/jimoneil/archive/2011/02/07/azure-startup-tasks-and-powershell-lessons-learned.aspx
# Bill Wilder @codingoutloud
# See also http://blog.codingoutloud.com/2012/12/10/tidy-the-temp-folder-on-windows-azure
get-childitem $TEMP -recurse | where {$_.lastwritetime -lt (get-date).adddays(-7)} |% {remove-item $_.fullname -force -recurse -verbose}
@codingoutloud
codingoutloud / RunningAsAdministrator.cs
Created December 5, 2012 21:27
Am I running as an Administrator?
using System.Security.Principal;
// Is this thread running with Administrator role?
private static bool RunningAsAdministrator()
{
var identity = WindowsIdentity.GetCurrent();
if (identity == null) return false; // Administrator should always have access to this
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
@codingoutloud
codingoutloud / RandomTokenGenerator.cs
Created December 4, 2012 04:14
Generate a random string that is URL safe.
using System;
using System.Security.Cryptography;
using System.Web;
namespace DevPartners
{
// author: Bill Wilder, @codingoutloud
// original: https://gist.github.com/4200537
public static class RandomTokenGenerator
{
@codingoutloud
codingoutloud / DumpExpiredCerts.cs
Created December 1, 2012 01:18
Dump information about expired certificates from the Windows Certificate Store
using System;
using System.Security.Cryptography.X509Certificates;
namespace DumpExpiredCertificates
{
internal class Program
{
private static void Main(string[] args)
{
// Iterates through all of the X.509 digital certificates installed in the certificate store
@codingoutloud
codingoutloud / myconventions.md
Last active October 13, 2015 02:38
Decided to make a list of conventions I practice in development... They aren't necessarily *right*, but are what they are.
@codingoutloud
codingoutloud / GenerateCryptographicallyStrongUrlSafeRandomToken.cs
Last active May 11, 2016 22:24
Generate a random(ish) token that is safe to be used in a URL. Allow caller to specify strength though number of generated random chars.
// Bill Wilder | @codingoutloud | http://www.cloudarchitecturepatterns.com | Nov 2012
// Original: https://gist.github.com/4121999
// Similar GenerateUrlSafeRandomToken.cs, except upgraded to use crypto-strength RNG
// Add reference to System.Web.dll
public static string GenerateUrlSafeCryptographicallyStrongRandomToken(int strength = 16)
{
using (var rng = new RNGCryptoServiceProvider())
{
var randomBytes = new byte[strength];
rng.GetBytes(randomBytes);
@codingoutloud
codingoutloud / TraceToComputeEmulator.cs
Last active November 17, 2016 09:41
Enable Diagnostic Trace Logging for Windows Azure Compute Emulator from ASP.NET
// Code snippet for use in Windows Azure Cloud Services.
// The EnableDiagnosticTraceLoggingForComputeEmulator method can be called from ASP.NET
// code to enable output from the System.Diagnostics.Trace class to appear in the
// Windows Azure Compute Emulator. The method does nothing when deployed to the cloud,
// when run outside the compute emulator, when run other than in DEBUG, or run repeatedly.
//
// The code uses Reflection to dynamically load the needed assembly and create the
// specific TraceListener class needed.
//
// EXAMPLE INITIALIZING FROM Global.asax.