Re: Message queues



Here is some code. The m_queue.BeginReceive method gets called, but
after a few seconds it throws an exception which is caught, but the ex
variable is null. My MessageArrived method is never called either even
though I have added it to the ReceiveCompleted event. Now I dont know what
to do. This is pretty darn simple code. I can see two messages in the queue
on david-1. I can run the server app on david-1 at the same time as the
remote side is running. The server app creates new entries but still the
MessageArrived handler is not called and a bogus exception is thrown.

Ideas why this does not work?

public partial class Form1 : Form
{
private MessageQueue m_queue;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
try
{
string deviceIP = String.Empty;
IPAddress[] addresses =
Dns.GetHostEntry("david-1").AddressList;
for (int i = 0; i < addresses.Length; ++i)
{
IPAddress addr = addresses[i];
if (addr.AddressFamily == AddressFamily.InterNetwork)
{
deviceIP = addr.ToString();
break;
}
}
if (deviceIP == String.Empty)
{
MessageBox.Show("Unable to retrieve Device IP");
Close();
return;
}

String strPath =
String.Format(@"FormatName:DIRECT=TCP:{0}\PRIVATE$\CallerID", deviceIP);
m_queue = new MessageQueue(strPath);

if (m_queue != null)
{
System.Type[] types = new Type[1];
types[0] = typeof(CallerIDClient.CallerID);
m_queue.Formatter = new XmlMessageFormatter(types);

try
{
m_queue.ReceiveCompleted += new
ReceiveCompletedEventHandler(MessageArrived);
}
catch (MessageQueueException ex)
{
MessageBox.Show(ex.Message);
Close();
}

Thread t1 = new Thread(PeekMessages);
t1.IsBackground = true;
t1.Start();
}
else
{
MessageBox.Show("Queue not created");
Close();
}
}
catch (MessageQueueException ex)
{
MessageBox.Show(ex.Message);
return;
}
}

private void PeekMessages()
{
while (true)
{
try
{
m_queue.BeginReceive();
}
catch (MessageQueueException ex)
{
MessageBox.Show(ex.Message);
return;
}
}
}

public void MessageArrived(Object source, ReceiveCompletedEventArgs
args)
{
MessageQueue queue = source as MessageQueue;
Message message = queue.EndReceive(args.AsyncResult);

CallerIDClient.CallerID callerid = message.Body as
CallerIDClient.CallerID;
if (callerid != null)
{
ListViewItem lvi = new ListViewItem(callerid.Name);
lvi.SubItems.Add(callerid.Number);
listView1.Items.Add(lvi);
}
}

private void AddItem(String strID)
{
Message message = m_queue.PeekById(strID);
CallerIDClient.CallerID callerid = message.Body as
CallerIDClient.CallerID;
if (callerid != null)
{
ListViewItem lvi = new ListViewItem(callerid.Name);
lvi.SubItems.Add(callerid.Number);
listView1.Items.Add(lvi);
}
}
}



"Simon Hart [MVP]" <srhartone@xxxxxxxxx> wrote in message news:35C3C7D8-7973-466E-BDDF-4EB3F9E33E52@xxxxxxxxxxxxxxxx
Are you running the following line:
MessageQueue.Exists(@"david-1\private$\callerid") on a remote machine (not on
david-1)? if so this is not supported for private queues. Checking the
existence of remote queues can only be done if the queue is public - in which
case active directory is required.

When executing the Exists method on a local machine the proper syntax is:
MessageQueue.Exists(@".\private$\callerid").

Period '.' meaning localhost machine.
--
Simon Hart
Visual Developer - Device Application Development MVP
http://simonrhart.blogspot.com

.



Relevant Pages

  • Messaging Queues
    ... MessageArrived handler is not called and a bogus exception is thrown. ... private void Form1_Load ... string deviceIP = String.Empty; ... CallerIDClient.CallerID callerid = message.Body as ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: Message queues
    ... I can run the server app on david-1 at the same time as the ... string deviceIP = String.Empty; ... MessageQueue queue = source as MessageQueue; ... CallerIDClient.CallerID callerid = message.Body as ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Message queues
    ... Sounds like you need to define a public queue instead of a private one. ... private void Form1_Load ... string deviceIP = String.Empty; ... CallerIDClient.CallerID callerid = message.Body as ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Message queues
    ... This link indicates that you can send and receive messages to private queues if a direct connection is available. ... Sounds like you need to define a public queue instead of a private one. ... string deviceIP = String.Empty; ... CallerIDClient.CallerID callerid = message.Body as ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Message queues
    ... Does a public queue require a domain server to be in place, that is, a server that has Active Directory? ... private void Form1_Load ... CallerIDClient.CallerID callerid = message.Body as ...
    (microsoft.public.dotnet.framework.compactframework)