Last active
January 28, 2016 23:37
-
-
Save phatboyg/8428147 to your computer and use it in GitHub Desktop.
Example code and output to show the exception being thrown and Fault message being published once with MassTransit 2.9.5 and RabbitMQ
This file contains hidden or 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
namespace Faulty | |
{ | |
using System; | |
using MassTransit; | |
public interface FaultyCommand | |
{ | |
string Id { get; } | |
} | |
class Program | |
{ | |
static void Main() | |
{ | |
IServiceBus bus = ServiceBusFactory.New(x => | |
{ | |
x.UseRabbitMq(); | |
x.ReceiveFrom("rabbitmq://localhost/test/faulty_consumer"); | |
x.Subscribe(s => s.Handler<FaultyCommand>((context, message) => | |
{ | |
Console.WriteLine("Command: {0}", message.Id); | |
throw new InvalidOperationException("Expected to fail: " + message.Id); | |
})); | |
}); | |
using (bus) | |
{ | |
IServiceBus faultBus = ServiceBusFactory.New(x => | |
{ | |
x.UseRabbitMq(); | |
x.ReceiveFrom("rabbitmq://localhost/test/fault_monitor"); | |
x.Subscribe(s => s.Handler<IFault>((context, message) => | |
{ | |
Console.WriteLine("Fault: {0}", message.FaultType); | |
})); | |
}); | |
using (faultBus) | |
{ | |
bus.Endpoint.Send<FaultyCommand>(new | |
{ | |
Id = "12345" | |
}); | |
Console.ReadKey(); | |
Console.WriteLine("Exiting..."); | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or 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
Command: 12345 | |
Command: 12345 | |
Command: 12345 | |
Command: 12345 | |
Command: 12345 | |
Fault: Fault<FaultyCommand> |
This file contains hidden or 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
namespace Faulty | |
{ | |
using System; | |
using System.Linq; | |
using MassTransit; | |
using MassTransit.Testing; | |
public interface FaultyCommand | |
{ | |
string Id { get; } | |
} | |
class Program | |
{ | |
static void Main() | |
{ | |
IServiceBus bus = ServiceBusFactory.New(x => | |
{ | |
x.UseMsmq(m => m.UseMulticastSubscriptionClient()); | |
x.ReceiveFrom("msmq://localhost/faulty_consumer"); | |
x.Subscribe(s => s.Handler<FaultyCommand>((context, message) => | |
{ | |
Console.WriteLine("Command: {0}", message.Id); | |
throw new InvalidOperationException("Expected to fail: " + message.Id); | |
})); | |
}); | |
using (bus) | |
{ | |
IServiceBus faultBus = ServiceBusFactory.New(x => | |
{ | |
x.UseMsmq(m => m.UseMulticastSubscriptionClient()); | |
x.ReceiveFrom("msmq://localhost/fault_monitor"); | |
x.Subscribe(s => s.Handler<IFault>((context, message) => | |
{ | |
Console.WriteLine("Fault: {0}", message.FaultType); | |
})); | |
}); | |
using (faultBus) | |
{ | |
bool subscribed = bus.HasSubscription<IFault>().Any(); | |
if (!subscribed) | |
{ | |
Console.WriteLine("Timeout waiting for subscription to Fault event"); | |
} | |
bus.Endpoint.Send<FaultyCommand>(new | |
{ | |
Id = "12345" | |
}); | |
Console.ReadKey(); | |
Console.WriteLine("Exiting..."); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Both the RabbitMQ and MSMQ versions output the same results.