因此,我正在尝试(基于此 EasyNetQ 教程:快速入门 - EasyNetQ)使用涉及发布者和订阅者的简单 EasyNetQ 消息传递架构,它似乎并没有像预期的那样工作。我的发布者和订阅者都是 Visual Studio 2015 中的 Windows 服务项目,它们之间发送的消息是自定义类型 (TextMessage) 的一个实例,它是一个简单的类库,如下所示:
namespace Messaging.Messages
{
public class TextMessage
{
public string Text { get; set; }
}
}
我的发布者看起来像这样:
namespace Messaging.Publisher
{
public partial class ReportService : ServiceBase
{
private Timer timer = null;
public ReportService()
{
this.InitializeComponent();
}
protected override void OnStart(string[] args)
{
Library.WriteErrorLog("Report Publisher Service started");
using (var bus = RabbitHutch.CreateBus("host=localhost"))
{
bus.Publish(new TextMessage
{
Text = "Hello"
});
}
}
protected override void OnStop()
{
this.timer.Enabled = false;
Library.WriteErrorLog("Test window service has stopped");
}
}
}
所以没什么好看的。它所做的只是发布一条 TextMessage 类型的消息并将日志记录到文本文件“PublisherLogFile.txt”中:
namespace Messaging.Publisher
{
public static class Library
{
public static void WriteErrorLog(string Message)
{
StreamWriter sw = null;
try
{
sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\PublisherLogFile.txt", true);
sw.WriteLine(DateTime.Now.ToString(CultureInfo.InvariantCulture) + ": " + Message);
sw.Flush();
sw.Close();
}
catch (Exception)
{
throw;
}
}
}
}
订阅者看起来像这样:
namespace Messaging.Subscriber
{
public partial class ReportSubscriberService : ServiceBase
{
public ReportSubscriberService()
{
this.InitializeComponent();
}
protected override void OnStart(string[] args)
{
WriteErrorLog("Report Subscriber Service started");
using (var bus = RabbitHutch.CreateBus("host=localhost"))
{
bus.Subscribe<TextMessage>("testId", HandleTextMessage);
}
}
protected override void OnStop()
{
WriteErrorLog("Exiting Report Subscriber Service");
}
private static void HandleTextMessage(TextMessage textMessage)
{
WriteErrorLog("Got message: " + textMessage.Text);
}
private static void WriteErrorLog(string Message)
{
try
{
var sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\SubscriberLogFile.txt", true);
sw.WriteLine(DateTime.Now.ToString(CultureInfo.InvariantCulture) + ": " + Message);
sw.Flush();
sw.Close();
}
catch (Exception)
{
throw;
}
}
}
}
也很简单。它所做的只是接收 TextMessage 类型的消息,并将其 Text 属性的值打印到日志文件“SubscriberLogFile.txt”中。问题是它似乎没有收到消息,因为它没有登录到上面的文本文件。看起来我的订阅者中的HandleTextMessage处理程序从未被调用过。这是“SubscriberLogFile.txt”的内容:
另外,查看 RabbitMQ 管理控制台,没有创建连接或通道,只有一个队列:
和 RabbitMQ 日志:
当我第一次做同样的实验时,不同之处在于发布者和订阅者是控制台应用程序而不是 Windows 服务,一切似乎都很好。这里可能是什么问题?