Service Fabric bits and bobs for MassTransit
The MassTransit listener coordinates starting and stopping the bus when the service starts, stops or changes role.
You can use the MassTransit listener with a stateful or stateless service.
To plumb in the listener in to a stateless service we will have to override the CreateServiceInstanceListeners
method on the StatelessService
, we then need to create a ServiceInstanceListener
and pass in a delegate that will create our MassTransitListener
.
public class MyStatelessService : StatelessService
{
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return new []
{
new ServiceInstanceListener(_ => new MassTransitListener(CreateBus()))
};
}
private static IBusControl CreateBus()
{
var busControl = Bus.Factory.CreateUsingRabbitMq(cfg =>
{
cfg.Host(new Uri("rabbitmq://localhost/"), h =>
{
h.Username("guest");
h.Password("guest");
});
});
return busControl;
}
}
To plumb in the listener in to a stateful service we will have to override the CreateServiceReplicaListeners
method on the StatefulService
, we then need to create a ServiceReplicaListener
and pass in a delegate that will create our MassTransitListener
.
public class MyStatefulService : StatefulService
{
protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
{
return new[]
{
new ServiceReplicaListener(_ => new MassTransitListener(CreateBus()))
};
}
private static IBusControl CreateBus()
{
var busControl = Bus.Factory.CreateUsingRabbitMq(cfg =>
{
cfg.Host(new Uri("rabbitmq://localhost/"), h =>
{
h.Username("guest");
h.Password("guest");
});
});
return busControl;
}
}
- Fork
- Hack!
- Pull Request