2
votes

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
Just to note i dont think the MsmqTransportConfig is valid in NServiceBus 3, im pretty sure you need to do this via the fluent configNot loved
Where is the endpoint PlusMaticPublisherInputQueue defined on the publisher side?Davin Tryon
If you start up your console app with the NServiceBus.Integration profile, do you see a subscription message in the input queue go through?Adam Fyles
No, no subscription message. Moreover, I solved some "details" about new version 3.0 of NSB, which requires configuration like this .DefiningEventsAs(t => t.Namespace != null && t.Namespace.StartsWith("EventTestMessages")); and I changed the name of the publisher input queue following the instruction I found inside the exception thrown (!). In new version the name of the transportation queue is automatically detected and it is the name of the namespace (if I understood correctly). Lzero51

1 Answers

0
votes

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