NServiceBus - NServiceBus.Host作为发布者和WPF应用程序作为订阅者。如何?

问题描述:

我想使用NServiceBus从控制台进程(非常类似于启动NServiceBus.Host.exe库中包含的PubSub示例)发送消息到我的WPF应用程序。NServiceBus - NServiceBus.Host作为发布者和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; 
}); 

这是App.config中的WPF应用程序的:

<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应用程序。

也许我需要一些更多的实践...;)

我使用NService总线3.0版本,至极是RC4版本,但我想这不是问题。

感谢任何人都可以帮忙!

L

+0

只是为了说明我不认为MsmqTransportConfig在NServiceBus 3中是有效的,我敢肯定你需要通过流畅的配置来做到这一点 – 2012-02-17 09:06:17

+0

终端'PlusMaticPublisherInputQueue'在发布端定义在哪里? – 2012-02-17 09:45:30

+0

如果使用NServiceBus.Integration配置文件启动控制台应用程序,您是否在输入队列中看到订阅消息? – 2012-02-17 12:57:19

发现问题。 我订阅了一个“EventMessage”(它实现了“IEvent” - >它实现了“IMessage”)并发布了一个“IEvent”!