Skip to content

Instantly share code, notes, and snippets.

View ramonsmits's full-sized avatar

Ramon Smits ramonsmits

View GitHub Profile
@ramonsmits
ramonsmits / RateGate.cs
Last active December 8, 2021 06:55
RateGate originally from Jack Leitch, now async and using Stopwatch.GetTimestamp() instead of Environment.TickCount
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// Used to control the rate of some occurrence per unit of time.
/// </summary>
/// <remarks>
@ramonsmits
ramonsmits / StartableEndpointExtensions.cs
Created February 13, 2018 18:43
NServiceBus StartWithRetry extension method to retry the start for a number of times
static class StartableEndpointExtensions
{
public static async Task<IEndpointInstance> StartWithRetry(
this IStartableEndpoint startableEndpoint,
int maxAttempts = 10
)
{
var attempts = 0;
while (true)
{
@ramonsmits
ramonsmits / ShippingSpeedMetricCollector.cs
Last active March 14, 2018 12:39
Business metric collector and calculation sample using NServiceBus events and sagas
using System;
using System.Threading.Tasks;
using NServiceBus;
class ShippingSpeedMetricCollector
: Saga<ShippingSpeedData>
, IAmStartedByMessages<OrderCreated>
, IHandleMessages<OrderCancelled>
, IHandleMessages<OrderShipped>
{
@ramonsmits
ramonsmits / FirstChanceExceptionLogging.cs
Last active May 27, 2019 07:23
Log unhandled and first chance app domain exceptions for NServiceBus
// Enable debug logging in NServiceBus
LogManager.Use<DefaultFactory>().Level(LogLevel.Debug);
// Create first change logger using type full name, to allow for filtering specific exceptions and/or namespaces when using NLog or log4net
var isDebugEnabled = LogManager.GetLogger("FirstChanceException").IsDebugEnabled
var appDomain = AppDomain.CurrentDomain;
appDomain.UnhandledException += (sender, ea) => LogManager.GetLogger("UnhandledException").Fatal(ea.ExceptionObject.GetType().Name, (Exception)ea.ExceptionObject);
if(isDebugEnabled) appDomain.FirstChanceException += (sender, ea) => LogManager.GetLogger("FirstChanceException." + ea.Exception.GetType().FullName).Debug(ea.Exception.Message, ea.Exception);
@ramonsmits
ramonsmits / RavenBatchOptimizer.cs
Created March 16, 2018 12:15
Code sample provided by RavenHQ for batching up writes
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Raven.Client;
/// <summary>
/// This class allows performing high throughput document store operations, using a batching mechanism
/// That mechanism collects up to 128 documents into a single batch and stores them in a single operation, reducing
@ramonsmits
ramonsmits / .cs
Created March 20, 2018 14:35
NServiceBus: Run the installers by invoking `endpointConfiguration.EnableInstallers()` and then `Endpoint.Create(..)` to run installers but not start the endpoint instance
if (args.Length == 1 && args[0] == "install")
{
await Console.Out.WriteLineAsync("Running installers...").ConfigureAwait(false);
var endpointConfiguration = CreateConfiguration();
endpointConfiguration.EnableInstallers();
await Endpoint.Create(endpointConfiguration).ConfigureAwait(false);
return; // Exit application, we are not going to start the endpoint and process messages
}
@ramonsmits
ramonsmits / .cs
Created March 22, 2018 13:51
NServiceBus add StackTrace information to outgoing message
// Via send options.
var sendOptions = new SendOptions();
sendOptions.SetHeader("StackTrace", System.Environment.StackTrace);
await context.Send(myMessage, sendOptions).ConfigureAwait(false);
// Via outgoing message mutator, requires registering the mutator via the endpoint configuration.
class AddStackTraceMutator : IMutateOutgoingTransportMessages
{
public Task MutateOutgoing(MutateOutgoingTransportMessageContext context)
@ramonsmits
ramonsmits / SqlServerTransportConfigurationExtensions.cs
Created March 27, 2018 09:56
NServiceBus 6 SQL Transport Multi-Instance mode support for connection strings in app.config with legacy 'queue schema' argument
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Common;
using System.Data.SqlClient;
namespace NServiceBus
{
using Transport.SQLServer;
@ramonsmits
ramonsmits / queueinfo.ps1
Created March 27, 2018 11:25
Get some statistics about all incoming/outgoing queue message counts with Powershell
Get-MsmqQueue | select QueueName, MessagesInQueue, BytesInQueue, JournalMessageCount
Get-MsmqOutgoingQueue | select DestinationQueueFormatName, MessageCount, State, EodNoAckCount, EodNoReadCount
@ramonsmits
ramonsmits / viamutator.cs
Created April 4, 2018 14:40
NServiceBus - Add a strack trace to every message send
class AddStackTraceMutator : IMutateOutgoingTransportMessages
{
public Task MutateOutgoing(MutateOutgoingTransportMessageContext context)
{
context.OutgoingHeaders["StackTrace"] = System.Environment.StackTrace;
return Task.CompletedTask;
}
}
endpointConfiguration.RegisterMessageMutator(new AddStackTraceMutator ());