2

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?

user1022465
  • 103
  • 6
  • 1
    What do you mean by 'support' (is it 'how can I do that' or 'is there a ready-to-use extension')? What do you mean by 'easy to use'? – Pavel Gatilov Feb 03 '13 at 18:46
  • With easy to use i mean primarily a ready-to-use extension like the ones that ninject provides – user1022465 Feb 03 '13 at 19:16
  • Please, post a link to a page that shows what you want, or paste a code snippet. For someone who doesn't know Ninject it's quite difficult to guess what you want. – Pavel Gatilov Feb 03 '13 at 19:41

3 Answers3

3

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.

Travis Illig
  • 23,195
  • 2
  • 62
  • 85
  • using NServiceBus seems to me like "using a sledge-hammer to crack a nut" for my purposes, but bbvcommon EventBroker looks small and simple to use. – user1022465 Feb 06 '13 at 08:25
  • I wasn't making recommendations of specific brokers to use, just using those as examples. There are others, too, I'm sure. It'll be up to you to evaluate your options and determine what's best for you. – Travis Illig Feb 06 '13 at 16:09
2

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.

Pavel Gatilov
  • 7,579
  • 1
  • 27
  • 42
0

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.

Dmitri Nesteruk
  • 23,067
  • 22
  • 97
  • 166