我是消息体系结构的新手,所以我可能走错了路。但是我想通过解决一个小问题将NServiceBus慢慢引入团队。
议程中的任命有州。在同一应用程序中,两个用户可能正在同一议程中查看同一约会。他们通过中央服务器上的远程会话启动此应用程序。因此,如果用户1更新约会的状态,我希望用户2查看新状态“实时”。
为了对此进行模拟或进行概念验证,我制作了一个新的控制台应用程序。通过NuGet,我同时获得了NServiceBus和NServiceBus.Host,因为从文档中了解到,我需要两者。而且我知道在生产代码中不建议将所有内容都放在同一程序集中,但是发布者和订阅者很可能最终会在同一程序集中...
在类Program方法Main中,我编写了以下代码:
BusConfiguration configuration = new BusConfiguration();
configuration.UsePersistence<InMemoryPersistence>();
configuration.UseSerialization<XmlSerializer>();
configuration.UseTransport<MsmqTransport>();
configuration.TimeToWaitBeforeTriggeringCriticalErrorOnTimeoutOutages(new TimeSpan(1, 0, 0));
ConventionsBuilder conventions = configuration.Conventions();
conventions.DefiningEventsAs(t => t.Namespace != null
&& t.Namespace.Contains("Events"));
using (IStartableBus bus = Bus.Create(configuration))
{
bus.Start();
Console.WriteLine("Press key");
Console.ReadKey();
bus.Publish<Events.AppointmentStateChanged>(a =>
{
a.AppointmentID = 1;
a.NewState = "New state";
});
Console.WriteLine("Event published.");
Console.ReadKey();
}
在类EndPointConfig方法Customize中,我添加了:
configuration.UsePersistence<InMemoryPersistence>();
configuration.UseSerialization<XmlSerializer>();
configuration.UseTransport<MsmqTransport>();
ConventionsBuilder conventions = configuration.Conventions();
conventions.DefiningEventsAs(t => t.Namespace != null
&& t.Namespace.Contains("Events"));
AppointmentStateChanged是Events文件夹中的一个简单类,如下所示:
public class AppointmentStateChanged: IEvent {
public int AppointmentID { get; set; }
public string NewState { get; set; }
}
AppointmentStateChangedHandler是事件处理程序:
public class AppointmentStateChangedHandler : IHandleMessages<Events.AppointmentStateChanged> {
public void Handle(Events.AppointmentStateChanged message) {
Console.WriteLine("AppointmentID: {0}, changed to state: {1}",
message.AppointmentID,
message.NewState);
}
}
如果我启动一个控制台应用程序,则一切正常。我看到处理程序处理该事件。但是,如果我尝试启动第二个控制台应用程序,则它将崩溃:System.Messaging.MessageQueueException(所请求操作的超时已过期)。所以我一定在做错事,并且让我再次猜测我不理解更高层次的东西。有人能指出我正确的方向吗?
Update Everthing位于AgendaUpdates命名空间中,但事件类位于AgendaUpdates.Events命名空间中。
更新2采取的步骤:
->我通过使用原始和复制的解决方案启动2个Visual Studio进行了测试。然后在IDE中启动两个控制台应用程序。
如果您希望发布者也成为消息的接收者,则需要在配置中指定此消息。
这是在清楚地解释这个文章,其中解决您的问题完全是在文章的末尾。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句