title: MSMQ in c# with Sender and Receiver classes
body: <p>Alright, I'm again struggling with some piece of code, and it doesn't work.
  Basically, I have a simple Student class with name, surname, and group number:</p>
  
  <pre><code>[XmlRoot]
      public class Student
      {
          string name;
          string surname;
          int group;
  
          public Student(string _name, string _surname, int _group)
          {
              name= _name;
              surname= _surname;
              group= _group;
          }
  
          public Student()
          {
          }
  
          public void writeAttributes()
          {
              Console.WriteLine("{0}{1}{2}",name, surname, group);
          }
      }
  </code></pre>
  
  <p>Then, my solution in Visual Studio, has two projects: one, the sender, the other the receiver.
  What I'm trying to accomplish is to send an instance of this class through MSMQ(asynchronously).
  Here's the sender code:</p>
  
  <pre><code>class Program
      {
          static MessageQueue mq;
  
          static void SendMessage(object sender, EventArgs e)
          {
              System.Messaging.Message msg = new System.Messaging.Message();
  
              try
              {
                  msg.Label = "Message Label";
  
                  mq.Send(new Student("Name", "Surname", 1000));
                  Console.WriteLine("We send a Student");
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex.Message);
              }
          }
  
          static void Main(string[] args)
          {
              Timer timer1 = new Timer();           
  
              if (MessageQueue.Exists(@".\private$\NewPrivateQ"))
                  mq = new MessageQueue(@".\private$\NewPrivateQ");
              else
                  mq = MessageQueue.Create(@".\private$\NewPrivateQ");    
  
              mq.Formatter = new XmlMessageFormatter(new Type[] { typeof(Student) });
  
              //init the timer
              System.Timers.Timer myTimer = new System.Timers.Timer();
              myTimer.Elapsed += new System.Timers.ElapsedEventHandler(SendMessage);
              myTimer.Interval = 3000;
              myTimer.Enabled = true;
  
              while (true) ;
          }
      }
  </code></pre>
  
  <p>I use a timer, to send the information every 3 seconds.
  And, here's the receiver code:</p>
  
  <pre><code>class Program
      {
          static void OnMessageArrival(IAsyncResult ar)
          {
              MessageQueue mq = (MessageQueue)ar.AsyncState;
  
              try
              {
                  Message msg = mq.EndReceive(ar);
  
                  var message = (Student)msg.Body;
                  //Console.WriteLine(message);
                  message.writeAttributes();
              }
              catch(Exception ex)
              {
                  Console.WriteLine(ex.Message);                
              }
              finally
              {
                  mq.BeginReceive(TimeSpan.FromSeconds(5), mq,
                  new AsyncCallback(OnMessageArrival));
              }
          }
  
          static void Main(string[] args)
          {
              MessageQueue mq = new MessageQueue(@".\private$\NewPrivateQ");
  
              mq.Formatter = new XmlMessageFormatter(new Type[] { typeof(Student) });
  
              while (true)
              {
                  try
                  {
                      IAsyncResult iar = mq.BeginReceive(TimeSpan.FromSeconds(5), mq, new AsyncCallback(OnMessageArrival));                   
                  }
                  catch (Exception ex)
                  {
                      Console.WriteLine(ex.Message);
                  }
              }
          }
      }
  </code></pre>
  
  <p>I don't receive anything. I've tried a multitude of ways. I've tried to change the XmlFormatter to BinaryFormatter, with no success.</p>
  
  <p>Another helpful information: the Student class it's included in both projects: the sender, and the receiver.</p>