Sunday, 25 August 2013

MassTransit Worker registration AFTER creating the bus

MassTransit Worker registration AFTER creating the bus

So I've just pulled the latest MT from NuGet and have been coding with it
like a bandit. Love it so far. Now the scenario I am currently
implementing is a generic "bus" wrapper that one of the implementations is
MT. So in the concrete implementation of the MT I implement the Bus in the
constructor... lets call it 'register'
/// <summary>
/// Registers this instance with a particular bus. Every instance with
the same name
/// will end up on the same bus
/// </summary>
/// <param name="networkName">The common key to share with other
instances on the network</param>
public void Register(string networkName)
{
_bus = ServiceBusFactory.New(x =>
{
x.ReceiveFrom("msmq://localhost/the_wheels_on_the_bus");
x.SetPurgeOnStartup(true);
x.SetNetwork(networkName);
x.UseMsmq();
x.UseMulticastSubscriptionClient();
});
}
Now, abstract members allow the external code to register handlers using
this implementation
/// <summary>
/// Abstract method to implement on the bus for allow for Responding
to Request/Response calls
/// </summary>
/// <typeparam name="TIn">The message to be received from the
requester.<remarks>Implements <see
cref="ICorrelatedMessage"/>ICorrelatedMessage</remarks> and must have
a value</typeparam>
/// <typeparam name="TOut">The message to be returned to the
requester.<remarks>Implements <see
cref="ICorrelatedMessage"/>ICorrelatedMessage</remarks> and must have
a value</typeparam>
/// <param name="hostedClassesFunc">The func to invoke to get the
appropriate data</param>
protected override void RegisterHandler<TIn, TOut>(Func<TIn, TOut>
hostedClassesFunc)
{
_bus.SubscribeHandler<TIn>(msg =>
{
var output = hostedClassesFunc.Invoke(msg);
var context = _bus.MessageContext<TIn>();
context.Respond(output);
});
if (typeof(TIn).GetInterfaces().Contains(typeof(ICachableItem))
{
//mark it as a worker
//_bus.Worker ?? <-- How can i register this guy here?
}
}
So the question lies here, Can i even register a worker here? I cant seem
to find anywhere to inject a worker on the bus at this point.
Thanks in advance for the help.. i hop the code ends up looking right

No comments:

Post a Comment