Skip to content

Instantly share code, notes, and snippets.

Avatar

Randy Burden randyburden

View GitHub Profile
@randyburden
randyburden / CustomDataProtectionProvider.cs
Created Sep 20, 2021
CustomDataProtectionProvider for ASP.NET Core that uses a hard-coded AES Key. This is more like the old machine key in classic ASP.NET. This can easily be refactored to have the key supplied from appsettings.json or the database.
View CustomDataProtectionProvider.cs
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
namespace DataProtectionHelpers
{
@randyburden
randyburden / NonOverlappingTimer.cs
Created May 13, 2021
Wraps System.Threading.Timer and guarantees non-overlapping executions.
View NonOverlappingTimer.cs
using System;
using System.Threading;
namespace Helpers
{
///<summary>Provides a mechanism for executing a method at specified intervals without overlapping.</summary>
public class NonOverlappingTimer : IDisposable
{
private readonly Timer _timer;
private static readonly object Lock = new object();
View RetryHelper.cs
using System;
using System.Threading;
namespace Helpers
{
/// <summary>
/// Retry helpers.
/// </summary>
public static class RetryHelper
{
@randyburden
randyburden / EntityFrameworkEnumerationClassPattern.cs
Created Dec 1, 2020
Entity Framework Enumeration Class Pattern
View EntityFrameworkEnumerationClassPattern.cs
public class EmailType
{
// Provides enum-like behavior while the persisted
// reference/lookup/enum table provides referential integrity
public static readonly EmailType Welcome = new EmailType
{
Id = 1,
Name = "Welcome"
};
@randyburden
randyburden / ConnectionStringHelper.cs
Created Mar 21, 2018
C# Connection String helper that can inject a custom application name pattern into a connection string. Custom application format is: <AssemblyName>, Version=X.X.X.X, BuildDate=MM/DD/YYYY 12:00 PM
View ConnectionStringHelper.cs
using System;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
namespace Utilities
{
/// <summary>
@randyburden
randyburden / RegisterWindowsScheduledTasks.ps1
Created Nov 8, 2017
Registers Windows Scheduled Tasks. Taken from a working example I created that ran as a post-deployment step. It also cleans up old deployment directories.
View RegisterWindowsScheduledTasks.ps1
##########################################
### Registers Windows Scheduled Tasks ###
##########################################
<###############################################################################################################
Notes:
- This example was from a working solution I created where this script was run as a post-deploy step for a
console application that served as the entry point for multiple scheduled tasks that could be executed
by supplying the task name.
@randyburden
randyburden / OracleOptionalDateRangeQuery.sql
Created Nov 6, 2017
Oracle PL/SQL example of a query with an optional date range
View OracleOptionalDateRangeQuery.sql
/* Oracle PL/SQL example of a query with an optional date range */
SET SERVEROUTPUT ON;
DECLARE
StartDate DATE := NULL;
EndDate DATE := NULL;
--StartDate DATE := TO_DATE ('01/01/2017', 'mm/dd/yyyy');
--EndDate DATE := TO_DATE ('02/01/2017', 'mm/dd/yyyy');
RecordCount NUMBER;
BEGIN
SELECT COUNT(*)
@randyburden
randyburden / ValidateOrderByClause.cs
Created Oct 19, 2017
C# method to validate a string-based OrderBy clause to be used in a dynamic LINQ statement
View ValidateOrderByClause.cs
internal void ValidateOrderByClause(string orderBy, string[] supportedOrderByFields)
{
if (!string.IsNullOrWhiteSpace(orderBy))
{
var orderBys = orderBy.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
foreach (var orderByClause in orderBys)
{
var orderByClauseParts = orderByClause.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
var orderByClauseField = orderByClauseParts[0];
@randyburden
randyburden / ConfigurationManagerOptionsProvider.cs
Last active Sep 29, 2017
C# strongly named application settings/options implementation. Can be used with Dependency Injection or with static helper method AppSettingsOptionsProvider<T>.Bind();.
View ConfigurationManagerOptionsProvider.cs
using System;
using System.Configuration;
using System.Reflection;
namespace Utilities.Options
{
/// <summary>
/// Uses <see cref="ConfigurationManager"/> to populate options.
/// Finds setting names/keys by <see cref="OptionNameAttribute"/>, {PropertyName}, or {ClassName}.{PropertyName}.
/// </summary>
@randyburden
randyburden / ConsoleColorExtensions.cs
Created Sep 20, 2017
C# extension/helper class for writing colored output to the console. This is sort-of a workaround of not being able to extend the static Console class.
View ConsoleColorExtensions.cs
using System;
namespace Utilities.Extensions
{
public static class ConsoleColorExtensions
{
/// <summary>
/// Writes the specified string value, followed by the current line terminator, to the standard output stream.
/// </summary>
/// <example>