如何在控制台应用程序中创建发布者

发布于 2024-09-26 07:29:00 字数 1843 浏览 1 评论 0原文

我刚刚开始使用 NServiceBus,并已更新 PubSub 示例以使用 .NET 4.0 Framework。这工作完全没问题。它在“NServiceBus.Host.exe”环境中运行一个发布者和两个订阅者 - 因此它负责设置总线实例并执行任何相关订阅。这一切都工作正常(正如您所期望的),但我现在试图将发布者从“NServiceBus.Host.exe”中运行移到它自己的控制台应用程序中(最终我想从网站发布消息,以便这似乎是朝这个方向迈出的一小步)。

如果我启动 3 个项目(我的控制台应用程序、Sub1 和 Sub2),它会在我的本地计算机上创建 5 msmq,但不是端点.config...订阅 Q,而是创建通用的“nservicebus_subscriptions”Q。如果我启用日志,我看到 MyPublisherInputQueue 有 3 个(已处理的)完成消息,错误代码为 0,而订阅者 1 输入队列和订阅者 2 输入队列各有一个。这一切看起来都不错,但如果我随后发布消息,发布者似乎不会抛出任何错误,但没有消息到达订阅者(他们只是等待消息)。此外,任何 MQ 的新消息或日志中都不会出现任何消​​息。

我显然错过了一些步骤 - 控制台应用程序是否未开放订阅?如果是这样,需要采取哪些步骤来做到这一点?当您在 nservicebus.host.exe 中托管发布者时,我缺少运行哪些步骤?

为了创建控制台应用程序,我执行了以下操作:

  1. 在 pubsub 解决方案中,创建了一个控制台应用程序。
  2. 在控制台应用程序中添加了对 mymessages、nservicebus.dll 和 nservicebus.core.dll 的引用
  3. 从 NServiceBus 网站上现有的 pub 代码和示例,将以下代码添加到“Main()”

代码:

IBus Bus = Configure.With()
                    .Log4Net()
                    .DefaultBuilder()
                    .MsmqSubscriptionStorage()
                    .XmlSerializer()
                    .MsmqTransport()
                    .UnicastBus()
                        .LoadMessageHandlers()
                    .CreateBus()
                    .Start();

bool publishIEvent = true;
do
{
    Console.ReadLine();
    var eventMessage = publishIEvent ? Bus.CreateInstance<IEvent>() : new EventMessage();

    eventMessage.EventId = Guid.NewGuid();
    eventMessage.Time = DateTime.Now.Second > 30 ? (DateTime?)DateTime.Now : null;
    eventMessage.Duration = TimeSpan.FromSeconds(99999D);

    Bus.Send(eventMessage);

    Console.WriteLine("Published event with Id {0}.", eventMessage.EventId);

} while (true);
  1. 为使用现有“publisher”app.config 的内容创建新的控制台应用程序。

  2. 向控制台应用程序项目的命令行参数添加了“NServiceBus.Integration”。

I'm just starting out with NServiceBus and have updated the PubSub sample to work with .NET 4.0 Framework. That's working perfectly ok. This runs one publisher and two subscribers within the "NServiceBus.Host.exe" environment - so it is that which takes responsibility for setting up an instance of the Bus and doing any relevant subscriptions. That's all working fine (as you'd expect) but I'm now trying to move the publisher out of being run within "NServiceBus.Host.exe" into it's own console application (eventually I would like to publish messages from a website so this seems a good small step in that direction).

If I start the 3 projects (my console app, Sub1 and Sub2) it creates 5 msmq on my local machine, but instead of the endpoint.config...subscriptions Q, it creates a generic "nservicebus_subscriptions" Q. If I enable journals, I see that the MyPublisherInputQueue has three (processed) completion messages with an errorcode 0, whilst the subscriber1inputqueue and subscriber2inputqueue each have one. This all seems good, but if I then publish messages, the publisher doesn't appear to throw any errors, but no messages make it as far as the subscribers (they just sit waiting for a message). Also no messages appear in either the new message or journal for any of the MQs.

I'm obviously missing some step(s) - Is the console application not opening itself up for subscriptions? If so, what steps are needed to do that? What steps am I missing that run when you host the publisher in the nservicebus.host.exe?

To create the console application I've done the following:

  1. Within the pubsub solution, created a console application.
  2. Added references in the console application to mymessages, nservicebus.dll and nservicebus.core.dll
  3. From the existing pub code and example on the NServiceBus website, added following code to "Main()"

Code:

IBus Bus = Configure.With()
                    .Log4Net()
                    .DefaultBuilder()
                    .MsmqSubscriptionStorage()
                    .XmlSerializer()
                    .MsmqTransport()
                    .UnicastBus()
                        .LoadMessageHandlers()
                    .CreateBus()
                    .Start();

bool publishIEvent = true;
do
{
    Console.ReadLine();
    var eventMessage = publishIEvent ? Bus.CreateInstance<IEvent>() : new EventMessage();

    eventMessage.EventId = Guid.NewGuid();
    eventMessage.Time = DateTime.Now.Second > 30 ? (DateTime?)DateTime.Now : null;
    eventMessage.Duration = TimeSpan.FromSeconds(99999D);

    Bus.Send(eventMessage);

    Console.WriteLine("Published event with Id {0}.", eventMessage.EventId);

} while (true);
  1. Created an app.config for the new console application using the contents of the existing "publisher" app.config.

  2. Added "NServiceBus.Integration" to command line arguments for console application project.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单挑你×的.吻 2024-10-03 07:29:00

如果您查看日志,NServiceBus 可能会告诉您需要将发布者配置为事务性的。方法是在 .MsmqTransport() 之后包含 .IsTransactional(true)。

If you look at your logs, it is likely that NServiceBus is telling you that you need to have your publisher be configured to be transactional. The way to do that is to include .IsTransactional(true) after .MsmqTransport().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文