【发布时间】:2012-02-24 15:58:59
【问题描述】:
我正在尝试使用 NServiceBus 将消息从控制台进程(与启动 NServiceBus.Host.exe 的库中包含的 PubSub 示例非常相似)发送到我的 WPF 应用程序。
这就是我所做的。 在 WPF App.xaml 类中我已经定义了
public static IBus Bus;
同一类,在 application_startup 处理程序中:
Bus = Configure.With().DefaultBuilder().BinarySerializer()
.MsmqTransport().IsTransactional(true).PurgeOnStartup(false)
.UnicastBus().ImpersonateSender(false).LoadMessageHandlers()
.CreateBus()
.Start();
Bus.Subscribe<EventMessage>((message) =>
{
switch (message.EventType)
{
case EventType.CreateSingleStay:
break;
case EventType.MoveToStartUpState:
case EventType.MoveToOperativeState:
case EventType.MoveToOutOfOrderState:
break;
case EventType.InputSignalDetected:
break;
}
return true;
});
这是 WPF 应用程序的 app.config:
<configSections>
<section name="MsmqTransportConfig" type="NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core" />
<section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core" />
</configSections>
<MsmqTransportConfig InputQueue="PresentationInputQueue" ErrorQueue="PresentationErrorQueue" NumberOfWorkerThreads="5" MaxRetries="5" />
<UnicastBusConfig>
<MessageEndpointMappings>
<add Messages="PlusMatic.EventsTest" Endpoint="PlusMaticPublisherInputQueue" />
</MessageEndpointMappings>
</UnicastBusConfig>
然后,我的 NServiceBus.Host 发布者,非常简单的 c# 类库,从调试 NServiceBus.Host.exe 可执行文件开始:
namespace PlusMatic.EventsTest
{
class EndpointConfig : IConfigureThisEndpoint, AsA_Publisher { }
}
...
namespace PlusMatic.EventsTest
{
public class TestPlusMaticServerEndpoint : IWantToRunAtStartup
{
public IBus Bus { get; set; }
private Thread _loopConsole;
#region Implementation of IWantToRunAtStartup
public void Run()
{
string readLine = string.Empty;
bool publishIEvent = true;
while (readLine != "j")
{
readLine = Console.ReadLine();
var eventMessage = publishIEvent
? Bus.CreateInstance<IEvent>()
: new EventMessage();
...
Bus.Publish(eventMessage);
Console.WriteLine("Published event with Id {0}.", eventMessage.EventId);
publishIEvent = !publishIEvent;
}
}
public void Stop()
{
}
}
}
这是我的事件类
namespace PlusMatic.EventsTest
{
[Serializable]
public class EventMessage : IEvent
{
public Guid EventId { get; set; }
public DateTime? Time { get; set; }
public EventType EventType { get; set; }
public MoveToStateEvent NextApplicationState { get; set; }
public InputEvent InputSignal { get; set; }
public IProductCard<Card> Card { get; set; }
}
public interface IEvent : IMessage
{
Guid EventId { get; set; }
DateTime? Time { get; set; }
IProductCard<Card> Card { get; set; }
EventType EventType { get; set; }
MoveToStateEvent NextApplicationState { get; set; }
InputEvent InputSignal { get; set; }
}
}
发布者中的 App.config:
<configSections>
<section name="MsmqTransportConfig" type="NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core" />
</configSections>
<MsmqTransportConfig ErrorQueue="error" NumberOfWorkerThreads="1" MaxRetries="5"/>
就是这样。 没有任何效果。 我从控制台项目发布事件,但我的 WPF 应用程序中没有任何反应。
也许我需要更多练习……;)
我使用的是 RC4 版本的 NService Bus 3.0 版,但我想这不是问题。
感谢任何可以提供帮助的人!
L
【问题讨论】:
-
请注意,我认为 MsmqTransportConfig 在 NServiceBus 3 中无效,我很确定您需要通过流利的配置来执行此操作
-
发布者端定义的端点
PlusMaticPublisherInputQueue在哪里? -
如果您使用 NServiceBus.Integration 配置文件启动控制台应用程序,您是否看到输入队列中的订阅消息通过?
-
不,没有订阅消息。此外,我解决了有关 NSB 新版本 3.0 的一些“细节”,这需要像
.DefiningEventsAs(t => t.Namespace != null && t.Namespace.StartsWith("EventTestMessages"));这样的配置,并且我按照我在抛出的异常(!)中找到的指令更改了发布者输入队列的名称。在新版本中,会自动检测到传输队列的名称,它是命名空间的名称(如果我理解正确的话)。 L
标签: c# wpf nservicebus