NServiceBus - NServiceBus.Host as publisher and WP

2019-07-19 04:56发布

问题:

I'm trying to use NServiceBus to send messages from a console process (very similar to PubSub example included in the library which starts NServiceBus.Host.exe) to my WPF application.

This what I did. In WPF App.xaml class I have defined

public static IBus Bus;

same class, in application_startup handler:

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;
});

this is app.config of WPF application:

<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> 

Then, my NServiceBus.Host publisher, very simple c# class library which starts in debug NServiceBus.Host.exe executable:

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()
    {

    }
}
}

And this my event class

    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 in publisher:

    <configSections>
        <section name="MsmqTransportConfig" type="NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core" />
    </configSections>

    <MsmqTransportConfig ErrorQueue="error" NumberOfWorkerThreads="1" MaxRetries="5"/>

That's it. Nothing works. I publish events from console project, and nothing happens in my WPF App.

Maybe I need some more practice...;)

I am using NService Bus version 3.0, wich is in RC4 release, but I suppose this is not the problem.

Thanks to anyone can help!

L

回答1:

Found the problem. I had subscribing an "EventMessage" (which implements "IEvent" -> which implements "IMessage") and publishing an "IEvent"!