我想使用MSMQ Multicast功能创建Publisher和订阅者模型。 我已经在链接中按照了答案而没有成功MSMQ - Cannot receive from Multicast queues 在本地计算机上发送和接收的消息。
发信人:
using (var helloQueue = new MessageQueue("FormatName:MULTICAST=234.1.1.1:8001"))
{
while (true)
{
var stopWatch = new Stopwatch();
stopWatch.Start();
for (var i = 0; i < 1000; i++)
{
SendMessage(helloQueue,
string.Format("{0}: msg:{1} hello world ", DateTime.UtcNow.Ticks, i));
}
stopWatch.Stop();
Console.ReadLine();
Console.WriteLine("====================================================");
Console.WriteLine("[MSMQ] done sending 1000 messages in " + stopWatch.ElapsedMilliseconds);
Console.WriteLine("[MSMQ] Sending reset counter to consumers.");
SendMessage(helloQueue, "reset");
Console.ReadLine();
}
}
接收器:
int messagesReceived = 0;
var messages = new Queue<string>(5000);
var filePath = typeof(Subscriber).FullName + ".txt";
var path = @".\private$\hello-queue";
using (var helloQueue = new MessageQueue(path))
{
helloQueue.MulticastAddress = "234.1.1.1:8001";
while (true)
{
var message = helloQueue.Receive();
if (message == null)
return;
var reader = new StreamReader(message.BodyStream);
var body = reader.ReadToEnd();
messagesReceived += 1;
messages.Enqueue(body);
Console.WriteLine(" [MSMQ] {0} Received {1}", messagesReceived, body);
if (string.CompareOrdinal("reset", body) == 0)
{
messagesReceived = 0;
File.WriteAllText(filePath, body);
messages.Clear();
}
}
}
我在多播绑定的注册表中添加了密钥,其中IP显示在事件日志中(不确定)。 我们在队列中指定的MulticastAddress是特定的东西,还是我们可以使用指定范围内的任何东西?
答案 0 :(得分:0)
只需更改端口号即可解决此问题。休息很好。