Does Rebus support web app publishing message and subscribing to message

842 Views Asked by At

I am new to Rebus. There are one questions i want to ask:

  1. It is a good idea to make web app publish message and subscribe to message. And does Rebus support this features.

I test Server mode , however it does not work. It handles the message only one message(from pubsubsample.websubscriber1.input queue) when web app starts.

BTW,It works well on One-way client mode.(Send message only)

Here is my code segment for server mode:

 public class CheckStatus : IHandleMessages<NewTradeRecorded>
{
    readonly IBus bus;

    public CheckStatus(IBus bus)
    {
        this.bus = bus;
    }

    public void Handle(NewTradeRecorded message)
    {

    }
 }

Asp.net MVC

protected void Application_Start()
    {

        using (var adapter = new BuiltinContainerAdapter())
        {
            adapter.Register(() => new CheckStatus(adapter.Bus));

           Configure.With(adapter)
                   .Transport(t => t.UseMsmqAndGetInputQueueNameFromAppConfig())
                     .MessageOwnership(o => o.FromRebusConfigurationSection())
                     .CreateBus()
                     .Start();

           adapter.Bus.Subscribe<NewTradeRecorded>();

        }
    }

web.config

<rebus  inputQueue="pubsubsample.websubscriber1.input" errorQueue="pubsubsample.websubscriber1.error" workers="1" maxRetries="5">
<endpoints>
  <add messages="Trading.Messages" endpoint="trading.input"/>
</endpoints>

1

There are 1 best solutions below

1
On BEST ANSWER

To answer your first question, whether Rebus supports publishing and subscribing from the same process, the answer is yes - there's no technical reason why you cannot subscribe to messages and publish the same messages from the same process, and that includes your web application.

Whether you should is another thing :)

Web applications in .NET are kind of transient in nature, i.e. they're recycled when IIS decides that it's time to recycle, and then it's usually not the best idea to subscribe to messages, because your application might not be running when an event is published, so it's not around to handle it.

And then, when it wakes up because IIS dispatches a web request to it, you might have 1,000,000 events waiting to be handled by your application, which will take quite a while to chew through.

In some cases, I've heard of people wanting to use Rebus pub/sub in web applications to keep a cache updated in the web app - but then they had severe issues, coming from the fact that IIS supports overlapping two instances of the same web application - iow, suddenly, for a short while, two instances of the same web applications were running, thus allowing a web application about to shut down to snatch a few events that should have been handled by the new instance.

For these reasons, in general I would not recomment doing pub/sub in web applications.

So, why doesn't your pub/sub thing work? Well - first thing: Don't dispose the container adapter immediately after creating it! :)

Do this instead:

static readonly _stuffToDispose = new List<IDisposable>();

protected void Application_Start()
{
    var adapter = new BuiltinContainerAdapter();

    _stuffToDispose.Add(adapter);

    adapter.Register(() => new CheckStatus(adapter.Bus));

    Configure.With(adapter)
             .Transport(t => t.UseMsmqAndGetInputQueueNameFromAppConfig())
             .MessageOwnership(o => o.FromRebusConfigurationSection())
             .CreateBus()
             .Start();

    adapter.Bus.Subscribe<NewTradeRecorded>();
}

protected void Application_End()
{
    _stuffToDispose.ForEach(d => d.Dispose());
}

This way, you bus will not stop handling messages immediately after your web app has started.