MEF+Unity+Masstransit+Topshelf

148 views
Skip to first unread message

DotNetSyndrome

unread,
Aug 20, 2015, 3:34:24 PM8/20/15
to masstransit-discuss
I am trying to work in Masstransit into a .NET solution whereby I am using Manoj Kumars article as a basis for integrating MEF & Unity DI. 

http://www.developer.com/net/dependency-injection-best-practices-in-an-n-tier-modular-application.html

REST messages are POSTed to a WEBAPI project. The API POST method massages the HTTP message(turns it into a domain POCO) and publishes the important bits to a RabbitMQ que.

Dom.WebApi & Dom.MessageQueues are the two differenct assemblies/projects.

We are using TopShelf as well. Here is the code

using System.Diagnostics;
using MassTransit;
using Microsoft.Practices.Unity;
using RabbitMQ.Client;

namespace Dom.MessageQueues.Queues
{
  
    public class QueZero : IQue<MyMessage>
    {

        private string _recieveFromUri;
        private IServiceBus _serviceBus;
        private IPlayer _player;
        private IQue<AnotherMessage> _queOne;
        private IUnityContainer _container;

        public QueZero(string recieveFromUri)
        {
            _recieveFromUri = recieveFromUri;
        }

        public QueBit(string recieveFromUri, IUnityContainer container)
        {
            _recieveFromUri = recieveFromUri;
            _container = container;
        }

        //TOPSHELF
        public void Start( )
        {
            _container.RegisterInstance<IServiceBus>(ServiceBusFactory.New(sbc =>
            {
                sbc.UseRabbitMq();
                sbc.ReceiveFrom(_recieveFromUri);

                //tell service bus configurator to subscribe the types registered in container
                sbc.Subscribe(s => s.LoadFrom(_container));

            }));

            //register instance of service bus with UnityContainer
            //container.RegisterInstance<IServiceBus>(_serviceBus); 
        }
       
        public void PublishToQue(MyMessage aMessage)
        {
            _serviceBus.Publish<MyMessage>(aMessage);
        }


        //TOPSHELF
        public void Stop()
        {
            _serviceBus.Dispose();
        }       
    }
}

Here is the WebApi ModuleInit.cs file, one for every assembly

using System.ComponentModel.Composition;
using MassTransit;
using RabbitMQ.Client;

namespace Dom.WebApi
{
    [Export(typeof(IModule))]
    public class ModuleInit : IModule
    {
        public void Initialize(IModuleRegistrar registrar)
        {
            registrar.RegisterType<IQue<MyMessage>, QueZero>();
            registrar.RegisterTypeWithContainerControlledLife<Consumes<IMyMessage>.All, MessageDetailGetter>(true);

            registrar.RegisterInstance<IServiceBus>(ServiceBusFactory.New(sbc =>
            {
                sbc.UseRabbitMq();
                sbc.ReceiveFrom("rabbitmq://localhost/quebitx");

                //tell service bus configurator to subscribe the types registered in container
                sbc.Subscribe(x => x.lo)
                sbc.Subscribe(s => s.LoadFrom(???)); //****CONTAINER IS NOT VISIBLE FROM HERE****

            }));
        }
    }
}

Chris Patterson

unread,
Aug 20, 2015, 3:41:04 PM8/20/15
to masstrans...@googlegroups.com
I'm curious why you need to subscribe consumers in your WebAPI - those consumers should typically be in your windows service (using Topshelf, yea!). The WebAPI should be Sending messages to the endpoint (queue) where the consumers for those messages are running.


--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/masstransit-discuss/bf4eb347-7083-4e5a-b16a-5b75228b97f9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

DotNetSyndrome

unread,
Aug 20, 2015, 3:51:29 PM8/20/15
to masstransit-discuss
I am actually trying to do an Integration test(MS TEST Assembly) for the WEBAPI project, and follow it onto it's subsequent assembly, the MessageQueue assembly. The expected end result would be: one message in the 'ready' column of the RabbitMQ web page management plugin. 
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.

DotNetSyndrome

unread,
Aug 20, 2015, 4:13:15 PM8/20/15
to masstransit-discuss
I am commited to writing unit tests(state-based and interact-based) as well as integration tests(observing results on external states/systems)!

Chris Patterson

unread,
Aug 21, 2015, 10:49:56 AM8/21/15
to masstrans...@googlegroups.com
My question still stands, why are you registering consumers in your WebAPI? 

Consumers are typically subscribed to the bus in a windows service -- where the lifecycle of the service can be controlled and isolated (making them autonomous). The consumer classes and messages types are in separate assemblies, which can and should be very well unit tested (The MassTransit.TestFramework makes this super easy to test asynchronous consumers using NUnit 2.6.4).

The only consumers that should be in the WebAPI are those that:

1. Update cached state at the WebAPI layer
2. Are responses to requests that were sent via messaging (not a preferred thing, but it's done all the time).
3. Event messages observed and routed via SignalR to the browser.

I just want to give you some guidance to make sure you're heading down a path to success, versus having to deal with problems and complexity due to the choices you're suggesting.


To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.

To post to this group, send email to masstrans...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages