Question

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

Was it helpful?

Solution 2

There currently isnt any way to do this outside of the configuration block. This would have to be exposed somehow in order to do it after the bus is created.

OTHER TIPS

_bus.SubscribeConsumer<SubscriberType>() or _bus.SubscribeConsumer(instanceOfSubscriberType) will subscribe normal consumers. If you want distributed worker consumers, you must do it inside the configuration block - we have not exposed the option to do so on the bus itself.

However, it's really important that you subscribe consumers inside the ServiceBusFactory.New block. Otherwise MT will start consuming messages on the queue and if there are no consumers registered for that type because you haven't done it yet, messages will end up in the error queue.

I don't see any other code, so this might not be a problem but remember each IServiceBus needs unique a RecieveFrom.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top