Skip to content

Instantly share code, notes, and snippets.

Ramon Smits ramonsmits

Block or report user

Report or block ramonsmits

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View SizeFormatter.cs
// https://stackoverflow.com/questions/281640/how-do-i-get-a-human-readable-file-size-in-bytes-abbreviation-using-net
public static class SizeFormatter
{
static String StrFormatByteSize (long length, IFormatProvider provider)
{
const int kb = 1024;
string[] suffix = { "B", "KB", "MB", "GB", "TB", "PB", "EB" };
if (length == 0) return "0" + suffix[0];
var bytes = Math.Abs(length);
var index = (int)Math.Floor(Math.Log(bytes, kb));
@ramonsmits
ramonsmits / OriginatingMachineLatencyBehavior-v7.cs
Created Jul 18, 2019
NServiceBus Machine Latency Windows Performance Counter Sample
View OriginatingMachineLatencyBehavior-v7.cs
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Threading.Tasks;
using NServiceBus;
using NServiceBus.Pipeline;
class OriginatingMachineLatencyBehavior : Behavior<IIncomingPhysicalMessageContext>
{
ConcurrentDictionary<string, (PerformanceCounter Counter, PerformanceCounter Base)> machineCounters = new ConcurrentDictionary<string, (PerformanceCounter,PerformanceCounter)>();
@ramonsmits
ramonsmits / LogHandler-v5.cs
Created Jul 16, 2019
NServiceBus - Log all incoming messages handler
View LogHandler-v5.cs
using System;
using NServiceBus;
using NServiceBus.Logging;
class LogHandler : IHandleMessages<object>
{
static readonly ILog Log = LogManager.GetLogger<LogHandler>();
public IBus Bus { get; set; }
@ramonsmits
ramonsmits / MapHeaderToPropertyBehavior.cs
Created Jul 12, 2019
NServiceBus - Map a header value to a message property to be used in a saga
View MapHeaderToPropertyBehavior.cs
using System;
using System.Threading.Tasks;
using NServiceBus;
using NServiceBus.Pipeline;
class Program
{
static async Task Main(string[] args)
{
var cfg = new EndpointConfiguration("test");
@ramonsmits
ramonsmits / InMemoryLRUGatewayDeduplication_v5.cs
Last active Jun 3, 2019
NServiceBus in memory least recently used gateway deduplication
View InMemoryLRUGatewayDeduplication_v5.cs
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Runtime.CompilerServices;
using NServiceBus;
using NServiceBus.Features;
using NServiceBus.Gateway.Deduplication;
using NServiceBus.Persistence;
/// <remarks>All collection operations are O(1)</remarks>
@ramonsmits
ramonsmits / NoGatewayPersistence.cs
Last active Jun 3, 2019
NServiceBus 5 - Fake gateway deduplication persistence if dedupe in the gateway is not needed
View NoGatewayPersistence.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using NServiceBus.Features;
using NServiceBus.Gateway.Deduplication;
// Usage:
//
// busConfiguration.UsePersistence<NoGatewayDeduplicationPersistence, StorageType.GatewayDeduplication>();
@ramonsmits
ramonsmits / LastInputHelper.cs
Created May 10, 2019
Get Windows User idle time and correcting for 32 bit API uptime rollover
View LastInputHelper.cs
static class LastInputHelper
{
/// <remarks>http://www.pinvoke.net/default.aspx/user32.GetLastInputInfo</remarks>
[DllImport("User32.dll")]
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
struct LASTINPUTINFO
{
public uint cbSize;
public uint dwTime;
@ramonsmits
ramonsmits / conventions.cs
Last active May 8, 2019
NServiceBus using type attributes in unobtrusive mode
View conventions.cs
// Usage: endpointConfiguration.Conventions().Apply();
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Interface)]
public class MessageAttribute : Attribute
{
}
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Interface)]
public sealed class EventAttribute : MessageAttribute
{
@ramonsmits
ramonsmits / ComputeHashAsync.cs
Created Apr 16, 2019
ComputeHashAsync extension as there is no async version in .net framework or .net core
View ComputeHashAsync.cs
using System.IO;
using System.Threading.Tasks;
namespace System.Security.Cryptography
{
public static class HashAlgorithmExt
{
public static async Task<byte[]> ComputeHashAsync(this HashAlgorithm algorithm, Stream stream, int blockSize = 0x14000)
{
var buffer = new byte[blockSize];
@ramonsmits
ramonsmits / AddMessageContextIdentifierstoExceptionBehavior.cs
Last active Nov 8, 2018
NServiceBus - Add message context identifiers to exception;
View AddMessageContextIdentifierstoExceptionBehavior.cs
//
// var pipeline = endpointConfiguration.Pipeline;
// pipeline.Register(behavior: new AddMessageContextIdentifierstoExceptionBehavior(), description: "Assigns the incoming message context identifiers to the exception for diagnostical purposes.");
//
class AddMessageContextIdentifierstoExceptionBehavior : Behavior<ITransportReceiveContext>
{
public override async Task Invoke(ITransportReceiveContext context, Func<Task> next)
{
try
{
You can’t perform that action at this time.