Question

Does Autofac support an easy to use message broker for doing many-to-many publish/subscribe event messaging?

Edit:

public class Service1
{
    [Publish("message://an-event")]
    public event Event<EventParams> AnEvent;

    public void DoSomething()
    {
        // do something
        // [...]

        // publish message
        if (AnEvent != null)
            AnEvent(this, new EventParams(something))
    }
}

public class Service2
{
    [Subscribe("message://an-event")]
    public OnAnEvent(object sender, EventParams eventParams)
    {
        Console.WriteLine("Hello World from Service2")
    }
}

public class Service3
{
    [Subscribe("message://an-event")]
    public OnAnEvent(object sender, EventParams eventParams)
    {
        Console.WriteLine("Hello World from Service3")
    }
}

The cool thing about Ninjects message broaker extension is its simplicity. You only have to add annotations to events that publish something and methods that subscribe to this event. The container handles automatically the connection. You only have to bind the services.

Has autofac something similar? If not, whats the best way to do something like this with autofac?

Was it helpful?

Solution

Autofac does not currently ship out of the box with any specific message broker support. Autofac being an IoC container, not a message broker, if anything were to be added, it would be extensions supporting an external system the way Ninject's bbeventbroker works - possible assistance in wiring up event handling but not management of the actual brokering proper. (Just like Autofac doesn't do its own MVC or WCF implementations - just helpers to interface with existing systems.)

You could roll your own integration with a system like the bbvcommon Event Broker or NServiceBus using things like Autofac's lifetime events and/or custom registration sources but exactly how that would need to be done and what that would look like would depend on the system with which you're integrating. You could look at the source for other integration components to get ideas and patterns.

If you do get something working, you might consider contributing it for inclusion as an Autofac.Extras project.

OTHER TIPS

Guessing that you want something like https://github.com/ninject/ninject.extensions.weakeventmessagebroker, the answer is certainly 'Yes'. In fact, I don't understand why would a message broker implementation be tied to an IoC container. They are completely independent components with completely separate tasks. You can pick up any message broker implementation and register it with any IoC container.

You can even pick the ninject.extensions.weakeventmessagebroker and register it with Autofac like this:

builder.RegisterType<EventReflectionStrategy>().As<IPlanningStrategy>();
builder.RegisterType<EventBindingStrategy>().As<IActivationStrategy>();
builder.RegisterType<WeakEventMessageBroker>().As<IWeakEventMessageBroker>()
       .SingleInstance();

and then add IWeakEventMessageBroker as a dependency to a type to use it. I haven't tried it myself though.

Maybe it's not as much ready-to-use, but it's still very easy.

Update

I've edited the code sample to turn the broker into a singleton, which should be required for it to work as expected.

The message broker architecture isn't itself tied to the container you're using. Take a look at this article for some examples of .NET message brokers, including one based on Rx.

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