MSMQ: OnReceiveCompleted bliver ikke kaldt
Jeg har med udgangspunkt i downloaded kode forsøgt at sende gennem en MSMQ.Alt ser ud til at køre, men receiver klassen registrerer ikke at der er sendt en message til køen, i hvert fald, ser det ikke ud til at OnReceiveCompleted bliver kaldt.
using System;
using System.Collections.Generic;
using System.Text;
using System.Messaging;
namespace ConsoleReqRepTest
{
class Program
{
static void Main(string[] args)
{
QueueGateway gate = new QueueGateway();
bool oprettetKøer = false;
try
{
gate.CreateMessageQueue("RequestQueue", true);
gate.CreateMessageQueue("ReplyQueue", true);
gate.CreateMessageQueue("InvalidQueue", true);
oprettetKøer = true;
}
catch (Exception ex)
{
oprettetKøer = false;
Console.WriteLine("Fejl i oprettelse af kø: " + ex.Message);
}
if (oprettetKøer)
{
Requestor req = new Requestor("RequestQueue", "ReplyQueue");
Replier rep = new Replier("RequestQueue", "InvalidQueue");
req.Send();
}
}
}
}
**************************************************************
using System;
using System.Collections.Generic;
using System.Text;
using System.Messaging;
namespace ConsoleReqRepTest
{
class QueueGateway
{
// Oprettele af kø
public bool CreateMessageQueue(string MessageQueueName, bool Transactional)
{
bool Success = false;
MessageQueue messageQueue;
try
{
if (Transactional)
{
messageQueue = MessageQueue.Create(@".\private$\" + MessageQueueName, true);
}
else
{
messageQueue = MessageQueue.Create(@".\private$\" + MessageQueueName, false);
}
Success = true;
}
catch (MessageQueueException ex)
{
Success = false;
Console.WriteLine("Fejl i oprettelse af kø" + ex.Message);
}
return Success;
}
// Fjernelse af kø
public bool DeleteQueue(string MessageQueueName, out string ErrorText)
{
bool Success = false;
try
{
MessageQueue.Delete(MessageQueueName);
Success = true;
ErrorText = "";
}
catch (MessageQueueException ex)
{
ErrorText = ex.Message;
Success = false;
}
return Success;
}
}
}
***********************************************************
using System;
using System.Collections.Generic;
using System.Text;
using System.Messaging;
namespace ConsoleReqRepTest
{
class Requestor
{
private MessageQueue requestQueue;
private MessageQueue replyQueue;
public Requestor(String requestQueueName, String replyQueueName)
{
requestQueue = new MessageQueue(@".\private$\" + requestQueueName);
replyQueue = new MessageQueue(@".\private$\" + replyQueueName);
replyQueue.MessageReadPropertyFilter.SetAll();
((XmlMessageFormatter)replyQueue.Formatter).TargetTypeNames = new string[] { "System.String,mscorlib" };
}
public void Send()
{
Message requestMessage = new Message();
requestMessage.Body = "Hello world.";
requestMessage.ResponseQueue = replyQueue;
requestQueue.Send(requestMessage);
Console.WriteLine("Sent request");
Console.WriteLine("\tTime: {0}", DateTime.Now.ToString("HH:mm:ss.ffffff"));
Console.WriteLine("\tMessage ID: {0}", requestMessage.Id);
Console.WriteLine("\tCorrel. ID: {0}", requestMessage.CorrelationId);
Console.WriteLine("\tReply to: {0}", requestMessage.ResponseQueue.Path);
Console.WriteLine("\tContents: {0}", requestMessage.Body.ToString());
Console.Read();
}
public void ReceiveSync()
{
Message replyMessage = replyQueue.Receive();
Console.WriteLine("Received reply");
Console.WriteLine("\tTime: {0}", DateTime.Now.ToString("HH:mm:ss.ffffff"));
Console.WriteLine("\tMessage ID: {0}", replyMessage.Id);
Console.WriteLine("\tCorrel. ID: {0}", replyMessage.CorrelationId);
Console.WriteLine("\tReply to: {0}", "<n/a>");
Console.WriteLine("\tContents: {0}", replyMessage.Body.ToString());
Console.Read();
}
}
}
************************************************************
using System;
using System.Collections.Generic;
using System.Text;
using System.Messaging;
using System.Threading;
namespace ConsoleReqRepTest
{
class Replier
{
private MessageQueue invalidQueue;
public Replier(String requestQueueName, String invalidQueueName)
{
MessageQueue requestQueue = new MessageQueue(@".\private$\" + requestQueueName);//Skal muligvis være ".\\" istedet men kaster da en MessageQueueException
invalidQueue = new MessageQueue(@".\private$\" + invalidQueueName);//Skal muligvis være ".\\" istedet
requestQueue.MessageReadPropertyFilter.SetAll();
((XmlMessageFormatter)requestQueue.Formatter).TargetTypeNames = new string[] { "System.String,mscorlib" };
requestQueue.ReceiveCompleted += new ReceiveCompletedEventHandler(OnReceiveCompleted);
requestQueue.BeginReceive();
}
public void OnReceiveCompleted(Object source, ReceiveCompletedEventArgs asyncResult)
{
MessageQueue requestQueue = (MessageQueue)source;
Message requestMessage = requestQueue.EndReceive(asyncResult.AsyncResult);
requestMessage.Formatter = new XmlMessageFormatter(new string[] { "System.String, mscorlib" });
try
{
Console.WriteLine("Received request");
Console.WriteLine("\tTime: {0}", DateTime.Now.ToString("HH:mm:ss.ffffff"));
Console.WriteLine("\tMessage ID: {0}", requestMessage.Id);
Console.WriteLine("\tCorrel. ID: {0}", "<n/a>");
Console.WriteLine("\tReply to: {0}", requestMessage.ResponseQueue.Path);
Console.WriteLine("\tContents: {0}", requestMessage.Body.ToString());
string contents = requestMessage.Body.ToString();
MessageQueue replyQueue = requestMessage.ResponseQueue;
Message replyMessage = new Message();
replyMessage.Body = contents;
replyMessage.CorrelationId = requestMessage.Id;
replyQueue.Send(replyMessage);
Console.WriteLine("Sent reply");
Console.WriteLine("\tTime: {0}", DateTime.Now.ToString("HH:mm:ss.ffffff"));
Console.WriteLine("\tMessage ID: {0}", replyMessage.Id);
Console.WriteLine("\tCorrel. ID: {0}", replyMessage.CorrelationId);
Console.WriteLine("\tReply to: {0}", "<n/a>");
Console.WriteLine("\tContents: {0}", replyMessage.Body.ToString());
//Console.Read();
}
catch (Exception)
{
Console.WriteLine("Invalid message detected");
Console.WriteLine("\tType: {0}", requestMessage.BodyType);
Console.WriteLine("\tTime: {0}", DateTime.Now.ToString("HH:mm:ss.ffffff"));
Console.WriteLine("\tMessage ID: {0}", requestMessage.Id);
Console.WriteLine("\tCorrel. ID: {0}", "<n/a>");
Console.WriteLine("\tReply to: {0}", "<n/a>");
//Console.Read();
requestMessage.CorrelationId = requestMessage.Id;
invalidQueue.Send(requestMessage);
Console.WriteLine("Sent to invalid message queue");
Console.WriteLine("\tType: {0}", requestMessage.BodyType);
Console.WriteLine("\tTime: {0}", DateTime.Now.ToString("HH:mm:ss.ffffff"));
Console.WriteLine("\tMessage ID: {0}", requestMessage.Id);
Console.WriteLine("\tCorrel. ID: {0}", requestMessage.CorrelationId);
Console.WriteLine("\tReply to: {0}", requestMessage.ResponseQueue.Path);
//Console.Read();
}
requestQueue.BeginReceive();
}
}
}
Kan det være noget med stien til kø'erne ?????