Skip to content

Instantly share code, notes, and snippets.

View ramonsmits's full-sized avatar

Ramon Smits ramonsmits

View GitHub Profile
@ramonsmits
ramonsmits / string-format-extension.cs
Last active December 17, 2015 10:19 — forked from terenced/string-format-extension.cs
Added alignment support for named values.
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
namespace StringExtensions
{
/// <remarks>
/// http://james.newtonking.com/archive/2008/03/27/formatwith-string-format-extension-method.aspx
@ramonsmits
ramonsmits / TimeSpanExtensions.cs
Last active July 6, 2018 12:53
Some simple TimeSpan extension methods that add approximations for total weeks, months and years in a timespan.
using System;
public static class TimeSpanExtensions
{
const double DaysInWeek = 7d;
const double DaysInMonth = 365d / 12d;
const double DaysInYear = 365d;
public static double TotalWeeks(this TimeSpan val)
{
@ramonsmits
ramonsmits / DateTimeExtension.cs
Last active April 16, 2018 19:00
This extension class helps to easily convert to/from unix timestamps.
using System;
/// <summary>
/// This extension class helps to easily convert to/from unix timestamps.
/// </summary>
public static class DateTimeExtension
{
private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
/// <summary>
@ramonsmits
ramonsmits / gist:7563502
Created November 20, 2013 13:52
Example of Mandrill webhook JSON data.
[
{
"event": "send",
"msg": {
"ts": 1365109999,
"subject": "This an example webhook message",
"email": "example.webhook@mandrillapp.com",
"sender": "example.sender@mandrillapp.com",
"tags": [
"webhook-example"
@ramonsmits
ramonsmits / gist:f7d20dbfd5335e6b1afc
Created June 18, 2015 19:42
NServiceBus behavior to share persistence database connection
using System;
using System.Data;
using System.Diagnostics;
using NServiceBus;
using NServiceBus.Persistence.NHibernate;
using NServiceBus.Pipeline;
using NServiceBus.Pipeline.Contexts;
class MyContextBehaviorRegistration : INeedInitialization
{
@ramonsmits
ramonsmits / gist:9d11e05276fd45763964
Created June 25, 2015 20:16
NServiceBus Unit of work that calls .SaveChanges on a DbContext using Func<DbContext>
busConfiguration.RegisterComponents(r => r.ConfigureComponent<ReceiverDataContext>(DependencyLifecycle.InstancePerUnitOfWork));
busConfiguration.RegisterComponents(r => r.ConfigureComponent<IDbConnection>(b => b.Build<NHibernateStorageContext>().Connection, DependencyLifecycle.InstancePerUnitOfWork));
class DbContextUnitOfWork : IManageUnitsOfWork
{
private readonly Func<ReceiverDataContext> createContext;
public DbContextUnitOfWork(Func<ReceiverDataContext> createContext)
{
@ramonsmits
ramonsmits / purge_all_msmq_queues_with_journal.ps1
Created October 7, 2015 14:01
Purge all MSMQ private queues and corresponding journal queues
[void] [Reflection.Assembly]::LoadWithPartialName("System.Messaging")
[System.Messaging.MessageQueue]::GetPrivateQueuesByMachine("LOCALHOST") | % { $_.Purge(); }
echo "All MSMQ queues purged"
[System.Messaging.MessageQueue]::GetPrivateQueuesByMachine("LOCALHOST") | ForEach-Object {
$queueFormatName = "$($_.Path);JOURNAL"
#Write-Host $queueFormatName -foregroundcolor cyan
@ramonsmits
ramonsmits / ThrottleThroughputWhenCheckFailsUsingScheduler.cs
Created November 10, 2015 14:58
Throttle NServiceBus throughput when a custom check fails using the scheduler
using System;
using NServiceBus;
using NServiceBus.Logging;
class ThrottleThroughputWhenCheckFailsUsingScheduler : IWantToRunWhenBusStartsAndStops
{
NServiceBus.Unicast.UnicastBus bus;
Schedule schedule;
ILog Log = LogManager.GetLogger(typeof(CheckBreaker));
@ramonsmits
ramonsmits / ThrottleThroughputWhenCheckFailsUsingPeriodicCheck.cs
Last active November 10, 2015 15:03
Throttle NServiceBus throughput when a custom check fails using service control custom checks
using System;
using NServiceBus;
using NServiceBus.Logging;
using ServiceControl.Plugin.CustomChecks;
class ThrottleThroughputWhenCheckFails : PeriodicCheck
{
NServiceBus.Unicast.UnicastBus bus;
ILog Log = LogManager.GetLogger(typeof(IntegrationCustomCheck));
@ramonsmits
ramonsmits / EndpointConfig.cs
Last active December 23, 2015 10:30
NServiceBus Host EndpointConfiguration initializing Log4net xml configuration, app domain diagnostic events and using Profile to target NHibernate persistence
using System;
using NServiceBus;
using NServiceBus.Hosting.Profiles;
using NServiceBus.Log4Net;
using NServiceBus.Logging;
public class EndpointConfig : IConfigureThisEndpoint
{
public EndpointConfig()
{