title: RabbitMQ Can't Send and Receive from same process
body: <p>I'm working through my first steps with RabbitMQ and have a question on why this didn't work.</p>
  
  <p>The basic tutorial (<a href="https://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html" rel="nofollow noreferrer">https://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html</a>) has one executable to send to the broker and another to receive it.</p>
  
  <p>I ran this code through a single console app in Visual Studio and couldn't receive any messages.</p>
  
  <p>If I take the "Receive" code and put it into a separate console app and open that, I get the message (no other code changes).</p>
  
  <p>Can someone explain why I'm not able to have both in the same process?  I had figured that the connection factory would handle independent connections accordingly regardless of whether it was the same process or not.</p>
  
  <hr>
  
  <p>For the sake of completeness (though I doubt it's required), here's the code that didn't work until I pulled out the "Receiver" code and put it into it's own console app:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args) {
  
          Receiver.Receive();
          Console.WriteLine("receiver set up");
  
          System.Threading.Thread.Sleep(5000);
  
          Console.WriteLine("sending...");
          Test.Send();
  
          // can also reverse order of send/receive methods, same result
  
          Console.ReadKey();
  
      }
  }
  
  
  public class Receiver
  {
      public static void Receive() {
  
          var factory = new ConnectionFactory() { HostName = "localhost" };
  
          using (var connection = factory.CreateConnection()) {
  
              using (var channel = connection.CreateModel()) {
  
                  channel.QueueDeclare("hello", false, false, false, null);
  
                  var consumer = new EventingBasicConsumer(channel);
                  consumer.Received += (model, ea) =&gt; {
  
                      var body = ea.Body;
                      var message = Encoding.UTF8.GetString(body);
                      System.Diagnostics.Debug.WriteLine("=====================");
                      System.Diagnostics.Debug.WriteLine(message);
                      System.Diagnostics.Debug.WriteLine("=====================");
  
                  };
  
                  channel.BasicConsume("hello", true, consumer);
  
              }
  
          }
  
      }
  
  }
  
  public class Test
  {
  
      public static void Send() {
  
          var factory = new ConnectionFactory() { HostName = "localhost" };
  
          using (var connection = factory.CreateConnection()) {
  
              using (var channel = connection.CreateModel()) {
  
                  channel.QueueDeclare("hello", false, false, false, null);
  
                  string message = "Check it!";
  
                  var body = Encoding.UTF8.GetBytes(message);
  
                  channel.BasicPublish("", "hello", null, body);
  
              }
          }
  
      }
  
  }
  </code></pre>