Skip to content

Instantly share code, notes, and snippets.

View ramonsmits's full-sized avatar

Ramon Smits ramonsmits

View GitHub Profile
@ramonsmits
ramonsmits / VerifyBodyMessageMutator.cs
Created October 24, 2018 14:26
NServiceBus verify message body by calculating the sha1 value when sending and verifying it when receiving the message
using System;
using System.Security.Cryptography;
using System.Threading.Tasks;
using NServiceBus.MessageMutator;
/// <summary>
/// endpointConfiguration.RegisterMessageMutator(new VerifyBodyMessageMutator());
/// </summary>
public class VerifyBodyMessageMutator : IMutateIncomingTransportMessages, IMutateOutgoingTransportMessages
{
@ramonsmits
ramonsmits / DoThatWhenXHappened.cs
Created October 25, 2018 16:32
NServiceBus - Split an event into multiple independent task by having isolated handlers send a local command for each task
class DoThatWhenXHappened
: IHandleMessages<XHappened>
, IHandleMessages<DoThat>
{
async Task Handle(XHappened msg, IMessageHandlerContext ctx)
{
await ctx.SendLocal(new DoThat());
}
async Task Handle(DoThat msg, IMessageHandlerContext ctx)
@ramonsmits
ramonsmits / GuidUtility.cs
Last active July 6, 2022 09:18
GuidUtility
// Source: https://github.com/LogosBible/Logos.Utility/blob/master/src/Logos.Utility/GuidUtility.cs
using System;
using System.Security.Cryptography;
using System.Text;
/// <summary>
/// Helper methods for working with <see cref="Guid"/>.
/// </summary>
static class GuidUtility
{
@ramonsmits
ramonsmits / AddMessageContextIdentifierstoExceptionBehavior.cs
Last active November 8, 2018 10:07
NServiceBus - Add message context identifiers to exception;
//
// 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
{
@ramonsmits
ramonsmits / ComputeHashAsync.cs
Created April 16, 2019 14:24
ComputeHashAsync extension as there is no async version in .net framework or .net core
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 / conventions.cs
Last active May 8, 2019 10:07
NServiceBus using type attributes in unobtrusive mode
// 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 / LastInputHelper.cs
Created May 10, 2019 08:49
Get Windows User idle time and correcting for 32 bit API uptime rollover
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 / NoGatewayPersistence.cs
Last active June 3, 2019 19:48
NServiceBus 5 - Fake gateway deduplication persistence if dedupe in the gateway is not needed
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 / InMemoryLRUGatewayDeduplication_v5.cs
Last active June 3, 2019 20:35
NServiceBus in memory least recently used gateway deduplication
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 / MapHeaderToPropertyBehavior.cs
Created July 12, 2019 07:27
NServiceBus - Map a header value to a message property to be used in a saga
using System;
using System.Threading.Tasks;
using NServiceBus;
using NServiceBus.Pipeline;
class Program
{
static async Task Main(string[] args)
{
var cfg = new EndpointConfiguration("test");