Can't get EventAggregator working with AutoFac in ASP.NET

94 views Asked by At

I have a need for a simple EventAggregator in my ASP.NET MVC application where I'm using AutoFac. I've had a look at https://github.com/NimaAra/Easy.MessageHub - I really like the simplicity and lightweight as I feel a complete service bus implementation might be overkill for my needs. But I'm all ears if someone has a better approach :)

However - I can't seem to get the setup to work with my web application. I have a very simple setup:

HomeController (just to initiate the call)

public class HomeController : Controller
    {
        private readonly IPublishStuffService _publishStuffService;
        private readonly IMessageHub _hub;

        public HomeController(IPublishStuffService publishStuffService, IMessageHub hub)
        {
            _publishStuffService = publishStuffService;
            _hub = hub;

            _hub.RegisterGlobalHandler((type, eventObject) => Debug.WriteLine($"Type: {type} - Event: {eventObject}"));
        }

        public ActionResult Index()
        {
            _publishStuffService.PublishStuff("Hello world");
            return View();
        }

    }

The PublishStuffService

public class PublishStuffService : IPublishStuffService
    {
        private readonly IMessageHub _hub;

        public PublishStuffService(IMessageHub hub)
        {
            _hub = hub;
        }

        public void PublishStuff(string message)
        {
            _hub.Publish(message);
        }
    }

SubscribeToStuffService

public class SubscribeToStuffService : ISubscribeToStuffService
    {
        private readonly IMessageHub _hub;

        public SubscribeToStuffService(IMessageHub hub)
        {
            _hub = hub;
            _hub.Subscribe<string>(HandleSubscription);
        }

        public void HandleSubscription(string message)
        {
            Debug.WriteLine("===================");
            Debug.WriteLine(message);
            Debug.WriteLine("===================");
        }
    }

My GlobalHandler prints out the message "Hello world" fine - but my subscribing service never gets called. I'm figuring it might be a problem with my AutoFac setup - but I'm unsure how to handle the situation.

My AutoFac configuration (using AutoFac.MVC5) looks like this:

var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(HomeController).Assembly);

builder.RegisterModelBinders(typeof(HomeController).Assembly);
builder.RegisterModelBinderProvider();

builder.RegisterModule<AutofacWebTypesModule>();
builder.RegisterSource(new ViewRegistrationSource());
builder.RegisterFilterProvider();

// Register individual components
builder.RegisterType(typeof(SubscribeToStuffService)).As<ISubscribeToStuffService>().InstancePerLifetimeScope();
builder.RegisterType(typeof(PublishStuffService)).As<IPublishStuffService>().InstancePerLifetimeScope();
builder.RegisterType(typeof(MessageHub)).As<IMessageHub>().SingleInstance();

var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

Can anyone help me figure out how to ensure that the message hub is persisted across my entire web application so that I can have different classes subscribe to different events published to the message hub. Basically I'm looking for the Notification part of Jimmy Bogards Mediatr library - which works perfect by the way - but I feel that the implementation of Mediatr might be a bit much if I only intend to use the Notification setup :)

UPDATE I can actually get the subscriber events to fire if I add a dependency to ISubscribeToStuffService in the same place as I publish (here in my HomeController):

public class HomeController : Controller
    {
        private readonly IPublishStuffService _publishStuffService;
        private readonly IMessageHub _hub;
        private readonly ISubscribeToStuffService _subscribeToStuffService;

        public HomeController(IPublishStuffService publishStuffService, IMessageHub hub, ISubscribeToStuffService subscribeToStuffService)
        {
            _publishStuffService = publishStuffService;
            _hub = hub;
            _subscribeToStuffService = subscribeToStuffService;
        }
        ...
    }

This apparently resolves the subscriber. But that's not really helpful as it defeats the purpose of have a loose coupling between my dependencies.

So the question becomes - how do I get AutoFac to resolve my subscribers without having to add them as dependencies everywhere I publish something to the message bus? Can this be done?

UPDATE 2 Found this nifty little pub/sub extension for AutoFac: https://github.com/jonstelly/AutofacEvents. It does everyting I need right out of the box in terms of getting an event aggregator up and running with AutoFac.

0

There are 0 answers