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
@ramonsmits
ramonsmits / EndpointInstance.sql
Created Dec 16, 2019
Retrieves MSMQ endpoint instance data stored in a SQL Server instead from the file system.
View EndpointInstance.sql
CREATE TABLE [dbo].[EndpointInstance]
(
[EndpointName] VARCHAR(128) NOT NULL,
[Discriminator] VARCHAR(128) NULL DEFAULT NULL,
[Machine] VARCHAR(256) NOT NULL
)
View jitter.cs
static class Jitter
{
static readonly Random Random = new Random();
public static TimeSpan Next(int count, TimeSpan first)
{
double jitter;
lock (Random) jitter = Random.NextDouble(); // Needed to make random threadsafe
return TimeSpan.FromTicks((long) (first.Ticks * Math.Pow(2, count + jitter)));
}
View EnforceTransactionScopePromotionBehavior.cs
// Usage: endpointConfiguration.Pipeline.Register(typeof(EnforceTransactionScopePromotionBehavior), nameof(EnforceTransactionScopePromotionBehavior));
public class EnforceTransactionScopePromotionBehavior : IBehavior<IIncomingPhysicalMessageContext, IIncomingPhysicalMessageContext>
{
public Task Invoke(IIncomingPhysicalMessageContext context, Func<IIncomingPhysicalMessageContext, Task> next)
{
var transaction = System.Transactions.Transaction.Current;
if(transaction==null) throw new InvalidOperationException($"No ambient TransactionScope active, disable {nameof(EnforceTransactionScopePromotionBehavior)} behavior");
System.Transactions.TransactionInterop.GetTransmitterPropagationToken(transaction);
return next(context);
}
@ramonsmits
ramonsmits / CustomOptionsWrapper.cs
Last active Nov 18, 2019
Wrap IEndpointInstance to set custom options on all sends with NServiceBus 7
View CustomOptionsWrapper.cs
using System;
using System.Threading;
using System.Threading.Tasks;
using NServiceBus;
public class CustomOptionsWrapper : IEndpointInstance
{
readonly IEndpointInstance innerInstance;
readonly IPrincipalAccessor principalAccessor;
@ramonsmits
ramonsmits / BehaviorContextBagFlow.cs
Created Sep 27, 2019
NServiceBus - Sample that shows the flow of context bag extension data from the incoming to outgoing context
View BehaviorContextBagFlow.cs
// LogManager.Use<DefaultFactory>().Level(LogLevel.Debug);
// var pipeline = endpointConfiguration.Pipeline;
// pipeline.Register(new IN(), nameof(IN));
// pipeline.Register(new OUT(), nameof(OUT));
class IN : Behavior<IIncomingLogicalMessageContext>
{
public const string Key = "count";
static long count;
static readonly ILog Log = LogManager.GetLogger<IN>();
@ramonsmits
ramonsmits / LogMessageHeadersMutator.cs
Created Sep 18, 2019
NServiceBus - Log message headers for all incoming messages via mutator
View LogMessageHeadersMutator.cs
using System.Text;
using System.Threading.Tasks;
using NServiceBus.Logging;
using NServiceBus.MessageMutator;
class LogMessageHeadersMutator : IMutateIncomingTransportMessages
{
static readonly ILog Log = LogManager.GetLogger<LogMessageHeadersMutator>();
public Task MutateIncoming(MutateIncomingTransportMessageContext context)
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");
You can’t perform that action at this time.