title: Rebus - how to register an instance of a handler
body: <p>I'm trying to use <code>Rebus</code> in C# as an alternative to Java's <code>org.greenrobot.eventbus.EventBus</code>. Can't figure out how to register an isntance of a handler dynamically.</p>
  
  <hr>
  
  <p><strong>Goal:</strong></p>
  
  <ul>
  <li>Start the bus</li>
  <li>Register handler isntances dynamically at runtime (e.g. add new one when user presses a button)</li>
  <li>Receive some messages by those handlers</li>
  <li>Stop the bus</li>
  </ul>
  
  <hr>
  
  <p><strong>Current code:</strong>(NUnit) It only prints some diagnostic output from Rebus itself, but not the messages.</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System;
  using System.Threading.Tasks;
  using Rebus.Config;
  using Rebus.Transport.InMem;
  using NUnit.Framework;
  using Rebus.Activation;
  using Rebus.Handlers;
  
  [TestFixture]
  public class RebusTests
  {
      BuiltinHandlerActivator activator = new BuiltinHandlerActivator();
      InMemNetwork network = new InMemNetwork(true);
      private string inputQueueName = "inputQueue";
  
      [SetUp]
      public void Setup() {
          Configure.With(activator)
              .Transport(t =&gt; t.UseInMemoryTransport(network, inputQueueName))
              .Start();
      }
  
      [TearDown]
      public void Cleanup() {
          activator.Dispose();
      }
  
      [Test]
      public void TestHandlingStrings() {
          var h1 = new StringHandler("handler 1");
          var h2 = new StringHandler("handler 2");
  
          int workersCount = activator.Bus.Advanced.Workers.Count;
          activator.Bus.Advanced.Workers.SetNumberOfWorkers(0);
          activator.Register(() =&gt; h1);
          activator.Register(() =&gt; h2);
          activator.Bus.Advanced.Workers.SetNumberOfWorkers(workersCount);
  
          activator.Bus.Advanced.SyncBus.SendLocal("Good day, sir.");
      }
  }
  
  public class StringHandler : IHandleMessages&lt;string&gt; {
      public readonly string handlerName;
  
      public StringHandler(string handlerName) {
          this.handlerName = handlerName;
      }
  
      public Task Handle(string message) {
          Console.WriteLine($"1) Handler [{handlerName}] got: {message}");
          return Task.Run(() =&gt; { Console.WriteLine($"2) Handler [{handlerName}] got: {message}"); });
      }
  }
  </code></pre>