Created
January 15, 2018 09:26
-
-
Save SzymonPobiega/baac711abf494be2e903c4f17ccc805d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
using NServiceBus; | |
using NServiceBus.Logging; | |
using NServiceBus.MessageMutator; | |
namespace RabbitMutatorTest | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Start().GetAwaiter().GetResult(); | |
} | |
static async Task Start() | |
{ | |
LogManager.Use<DefaultFactory>().Level(LogLevel.Error); | |
var config = new EndpointConfiguration("RabbitMutatorTest"); | |
config.UsePersistence<InMemoryPersistence>(); | |
config.SendFailedMessagesTo("error"); | |
config.EnableInstallers(); | |
config.RegisterMessageMutator(new Mutator()); | |
config.Recoverability().Delayed(d => d.NumberOfRetries(0)); | |
var transport = config.UseTransport<RabbitMQTransport>(); | |
transport.ConnectionString("host=localhost"); | |
transport.UseConventionalRoutingTopology(); | |
//config.UseTransport<LearningTransport>(); | |
var endpoint = await Endpoint.Start(config); | |
Console.WriteLine("Press <enter> to send a message"); | |
while (true) | |
{ | |
Console.ReadLine(); | |
await endpoint.SendLocal(new MutatorMessage()); | |
} | |
} | |
} | |
class MutatorMessageHandler : IHandleMessages<MutatorMessage> | |
{ | |
public Task Handle(MutatorMessage message, IMessageHandlerContext context) | |
{ | |
var header = context.MessageHeaders["Test"]; | |
Console.WriteLine($"Test header value is: {header}"); | |
throw new Exception("Simulated"); | |
} | |
} | |
public class Mutator : IMutateIncomingTransportMessages | |
{ | |
public Task MutateIncoming(MutateIncomingTransportMessageContext context) | |
{ | |
var attempt = context.Headers.TryGetValue("Test", out string value) | |
? int.Parse(value) | |
: 0; | |
attempt++; | |
context.Headers["Test"] = attempt.ToString(); | |
return Task.CompletedTask; | |
} | |
} | |
class MutatorMessage : IMessage | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment