Skip to content

Instantly share code, notes, and snippets.

@phatboyg
Last active January 28, 2016 23:37
Show Gist options
  • Save phatboyg/8428147 to your computer and use it in GitHub Desktop.
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
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...");
}
}
}
}
}
Command: 12345
Command: 12345
Command: 12345
Command: 12345
Command: 12345
Fault: Fault<FaultyCommand>
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...");
}
}
}
}
}
@phatboyg
Copy link
Author

Both the RabbitMQ and MSMQ versions output the same results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment