Skip to content

Instantly share code, notes, and snippets.

View ramonsmits's full-sized avatar

Ramon Smits ramonsmits

View GitHub Profile
@ramonsmits
ramonsmits / NaturalComparer.cs
Created August 30, 2022 08:46
NaturalComparer to compare string with numerics
//
// Based on https://stackoverflow.com/a/5641272/199551
//
public class NaturalComparer : IComparer<string>
{
public static readonly NaturalComparer Instance = new NaturalComparer();
private NaturalComparer() { }
public int Compare(string x, string y)
@ramonsmits
ramonsmits / CheckSizeBehavior.cs
Created June 16, 2022 15:59
NServiceBus 7 - Behavior that validates if the serialized body does not exceed a configurable limit
//
// Usage:
//
// var endpointConfiguration = new EndpointConfiguration("MyEndpoint")
// ...
// endpointConfiguration.Pipeline.Register(new CheckSizeBehavior(CheckSizeBehavior.StandardTierMaxSize), nameof(CheckSizeBehavior));
//
using NServiceBus.Pipeline;
class CheckSizeBehavior : IBehavior<IOutgoingPhysicalMessageContext, IOutgoingPhysicalMessageContext>
@ramonsmits
ramonsmits / ValidateDestinationExistsBehavior.cs
Last active June 15, 2022 14:14
NServiceBus 7 - Validate if the Azure Service Bus unicast destination exists
// Usage:
//
// var endpointConfiguration = new EndpointConfiguration("MyEndpoint")
// ...
// var connectionString = Environment.GetEnvironmentVariable("AzureServiceBus_ConnectionString");
// var managementClient = new ManagementClient(connectionString);
// endpointConfiguration.Pipeline.Register(new ValidateDestinationExistsBehavior (managementClient), nameof(ValidateDestinationExistsBehavior));
//
using Microsoft.Azure.ServiceBus.Management;
using NServiceBus.Pipeline;
@ramonsmits
ramonsmits / ExceptionEnricher.cs
Created April 30, 2022 12:47
SeriLog log event enricher that adds exception type, message, data, and md5 of ToString()
using System;
using System.Collections;
using System.Linq;
using Serilog.Core;
using Serilog.Events;
class ExceptionEnricher : ILogEventEnricher
{
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
@ramonsmits
ramonsmits / DsmrReaderTeslaMateLoadBalancer.cs
Last active January 16, 2023 11:54
Tesla charging load balancer based on events from DSMR Reader and TeslaMate
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using MQTTnet;
class DsmrTeslaLoadBalancer
@ramonsmits
ramonsmits / TransactionManagerHelper.cs
Last active April 7, 2022 06:42
Override the TransactionManager.MaximumTimeout via reflection
static class TransactionManagerHelper
{
public static void ConfigureTransactionTimeout(TimeSpan timeout)
{
#if NETFRAMEWORK
SetTransactionManagerField("_cachedMaxTimeout", true);
SetTransactionManagerField("_maximumTimeout", timeout);
#else
SetTransactionManagerField("s_cachedMaxTimeout", true);
SetTransactionManagerField("s_maximumTimeout", timeout);
@ramonsmits
ramonsmits / Shuffle.cs
Created April 7, 2022 06:38
Fisher–Yates shuffle on list items
static class ListExtensions
{
static Random rng = new Random();
public static void Shuffle<T>(this IList<T> list)
{
// From https://stackoverflow.com/a/1262619/199551 but refactored via Resharper
int n = list.Count;
while (n > 1)
{
@ramonsmits
ramonsmits / log4net.md
Created July 15, 2021 10:39
Exlude NServiceBus.LicenseManager from log output for Serilog, NLog, and Log4net
<logger name="NServiceBus.LicenseManager" additivity="false">
    <level value="OFF" />        
</logger>
@ramonsmits
ramonsmits / MySaga_MarkAsComplete.cs
Created July 2, 2021 14:39
NServiceBus - Call saga `MarkAsComplete()` in separate handler to prevent message loss due to removed saga state
class MySaga : Saga<MySagaData>,
IAmStartedByMessages<MessageA>,
IAmStartedByMessages<MessageB>
IHandleMessages<CompleteMySaga>
{
public Task Handle(MessageA message, IMessageHandlerContext context)
{
Data.A = true;
await Check(context);
}
@ramonsmits
ramonsmits / sc-email-notification-http-api.md
Created May 11, 2021 12:15
Configure ServiceControl email notifications via HTTP API

To enable ServiceControl email notifications via scripting it is required to send

  1. [Configure] POST to http://localhost:33333/api/notifications/email with Content-Type: application/json and body like:
  2. [Enable] POST to http://localhost:33333/api/notifications/email/toggle with Content-Type: application/json and body like:

Configure body:

{
"smtp_server" : "localhost",
"smtp_port" : "25",