Skip to content

Instantly share code, notes, and snippets.

View ramonsmits's full-sized avatar

Ramon Smits ramonsmits

View GitHub Profile
ILogger logger = null;// Get logger
var appDomain = AppDomain.CurrentDomain;
appDomain.UnhandledException += (sender, ea) => Log.Fatal((Exception)ea.ExceptionObject, "UnhandledException");
if (logger.IsEnabled(LogEventLevel.Debug))
{
appDomain.FirstChanceException += (sender, ea) =>
{
var ex = ea.Exception;
@ramonsmits
ramonsmits / outboxcleanup.sql
Last active July 27, 2023 19:38
NServiceBus SQL Persistent outbox cleanup
DECLARE @DispatchedBefore datetime = GETUTCDATE()-1 -- Removes entries older then 24 hours
DECLARE @BatchSize INT = 4000 -- Avoid batch sizes over 4.000 to prevent lock escalation
WHILE 1 = 1
BEGIN
DELETE TOP (@BatchSize) FROM [dbo].[EndpointNameOutboxData] WITH (ROWLOCK)
WHERE Dispatched = 'true' AND DispatchedAt < @DispatchedBefore;
IF @@ROWCOUNT < @BatchSize -- Important that @@ROWCOUNT is read immediately after the DELETE
BREAK
END
@ramonsmits
ramonsmits / ReportFailedDurationsBehavior.cs
Created July 25, 2023 16:27
NServiceBus - Report processing durations for failed messages
//
// endpointConfiguration.Pipeline.Register(new ReportFailedDurationsBehavior(), nameof(ReportFailedDurationsBehavior));
//
class ReportFailedDurationsBehavior : IBehavior<IIncomingLogicalMessageContext, IIncomingLogicalMessageContext>
{
static readonly ILog Log = LogManager.GetLogger(typeof(ReportFailedDurationsBehavior));
static readonly bool IsDebugEnabled = Log.IsDebugEnabled;
static readonly TimeSpan WarningThreshold = TimeSpan.FromSeconds(30);
@ramonsmits
ramonsmits / Fedora38-VM.ps1
Last active July 24, 2023 12:55 — forked from PatrickLang/Fedora34-VM.ps1
Hyper-V example setup for Fedora Workstation 38
# Echo the commands as they're run
Set-PSDebug -Trace 1
# Dump Windows version
Get-ComputerInfo | Format-Table WindowsVersion, OsVersion
# Create a UEFI VM, secure boot enabled, use the secure boot settings for the
$vm = New-VM -Generation 2 -Name "Fedora 38" -Path .
$vm | Set-VMFirmware -EnableSecureBoot On -SecureBootTemplate "MicrosoftUEFICertificateAuthority"
@ramonsmits
ramonsmits / TaskRunBehavior.cs
Created July 17, 2023 09:32
NServiceBus 6+ behavior to run the incoming message
// If all handlers are compute bound or IO isn't happening early it can result in less concurrent processing as depending on the
// transport and it being full async and no IO is happening message processing could be sequential. This behavior will immediately
// do a Task.Run
//
// Usage:
// endpointConfiguration.Pipeline.Register(new TaskRunBehavior(), nameof(TaskRunBehavior));
//
class TaskRunBehavior : IBehavior<IIncomingPhysicalMessageContext, IIncomingPhysicalMessageContext>
{
public Task Invoke(IIncomingPhysicalMessageContext context, Func<IIncomingPhysicalMessageContext, Task> next)
@ramonsmits
ramonsmits / PathWalker.cs
Created July 10, 2023 18:29
Search for a file in the directory tree
using System;
using System.IO;
using System.Linq;
static class PathWalker
{
public static string Find(string filename, string path)
{
if (filename == null) throw new ArgumentNullException(nameof(filename));
if (path == null) throw new ArgumentNullException(nameof(path));
@ramonsmits
ramonsmits / Shovel.cs
Last active July 4, 2023 16:09
Simple messaging bridge forwarder acting as a shovel between different NServiceBus transports using NServiceBus 8.x with NServiceBus.Raw 4.x
using NServiceBus;
using NServiceBus.Logging;
using NServiceBus.Raw;
using NServiceBus.Routing;
using NServiceBus.Transport;
CancellationTokenSource cts = new();
Console.CancelKeyPress += (s, ea) =>
{
ea.Cancel = true;
@ramonsmits
ramonsmits / PluginLoadContext.cs
Last active June 9, 2023 07:29
Plugin load context that probes if a framework specific version exists
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
/// <summary>
/// Based on:
///
/// - https://learn.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support
@ramonsmits
ramonsmits / drop-tables.sql
Created May 23, 2023 07:38
TSQL: Create DROP TABLE queries for all tables in database
SELECT 'DROP TABLE ' + '[' + TABLE_SCHEMA + '].[' + TABLE_NAME + ']'
FROM INFORMATION_SCHEMA.TABLES
ORDER BY TABLE_SCHEMA, TABLE_NAME
netsh advfirewall firewall add rule name="ServicePulse - TCP 9090" dir=in action=allow protocol=TCP localport=9090
netsh advfirewall firewall add rule name="ServiceControl - TCP 33333" dir=in action=allow protocol=TCP localport=33333
netsh advfirewall firewall add rule name="ServiceControl.Monitoring - TCP 33633" dir=in action=allow protocol=TCP localport=33633
netsh advfirewall firewall add rule name="ServiceControl.Audit - TCP 44444" dir=in action=allow protocol=TCP localport=44444