Я пытаюсь использовать NServiceBus для отправки сообщений из консольного процесса (очень похоже на пример PubSub, включенный в библиотеку, запускающую NServiceBus.Host.exe) в мое приложение 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 Bus версии 3.0, которая находится в RC4релиз, но я полагаю, что это не проблема.
Спасибо всем, кто может помочь!
L