NServiceBus - NServiceBus.Host 作为发布者,WPF 应用程序作为订阅者。怎么做?

发布于 2025-01-06 17:02:52 字数 3950 浏览 4 评论 0原文

我正在尝试使用 NServiceBus 将消息从控制台进程(与启动 NServiceBus.Host.exe 的库中包含的 PubSub 示例非常相似)发送到我的 WPF 应用程序。

这就是我所做的。 定义了

public static IBus Bus;

在 WPF App.xaml 类中,我在 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 应用程序中没有任何反应。

也许我需要更多练习...;)

我正在使用 NService Bus 版本 3.0,它是 RC4 版本,但我认为这不是问题。

感谢任何人都可以提供帮助!

L

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

[旋木] 2025-01-13 17:02:52

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文