如何从 WPF 应用程序发送消息来测试 NServiceBus.host 服务器?

发布于 2024-12-20 15:02:40 字数 1170 浏览 1 评论 0原文

我使用以下代码从 wpf 应用程序启动 NServisBus.host.exe 文件:

System.Diagnostics.Process.Start("NServiceBus.Host.exe");

我的 NServiceBus 主机有以下端点设置:

public class EndpointConfig : IConfigureThisEndpoint, AsA_Server  
{
}

我的 NServiceBus 主机服务器上有以下类。我需要在我的 wpf 应用程序中创建此类对象(在执行 NServiceBus.host.exe 之后)。

public class OrderMessaging 
{
    public IBus Bus { get; set; }

    public void SendRouteMessageReceived(LabRoutingUpdateMessage routingUpdateMessage)
    {
        Bus.Send(new RouteMessageReceived(routingUpdateMessage));
    }
}

然后,我将通过引用的 dll 文件(在我的 wpf 应用程序中)访问该消息类,并在我的 wpf 应用程序中使用以下代码向服务器发送消息:

OrderManagement.OrderMessaging routeMessageReceived = new OrderManagement.OrderMessaging();
routeMessageReceived.Bus = _bus;   //Main problem is here! Wpf app does does not have handle on NServiceBus server Bus.
routeMessageReceived.SendRouteMessageReceived(_routing);

我可以向我的 NServiceBus.host 发送消息吗使用 WPF 应用程序的 .exe(当使用 process.start 运行时)?有没有一种方法可以使用自定义初始化在我的 wpf 应用程序中托管我自己的 NServiceBus 并仍然正确配置我的端点?或者有什么方法可以从我的 wpf 应用程序中获取总线的句柄? 非常感谢任何示例代码。提前致谢!

I start the NServisBus.host.exe file from my wpf application using the following code:

System.Diagnostics.Process.Start("NServiceBus.Host.exe");

I have the following endpoint settings for my NServiceBus host:

public class EndpointConfig : IConfigureThisEndpoint, AsA_Server  
{
}

I have the following class on my NServiceBus host server. I need to create this class object in my wpf application (after the NServiceBus.host.exe has been executed).

public class OrderMessaging 
{
    public IBus Bus { get; set; }

    public void SendRouteMessageReceived(LabRoutingUpdateMessage routingUpdateMessage)
    {
        Bus.Send(new RouteMessageReceived(routingUpdateMessage));
    }
}

I will then access this message class via it's referenced dll file (in my wpf app), and use the following code in my wpf application to send a message to the server:

OrderManagement.OrderMessaging routeMessageReceived = new OrderManagement.OrderMessaging();
routeMessageReceived.Bus = _bus;   //Main problem is here! Wpf app does does not have handle on NServiceBus server Bus.
routeMessageReceived.SendRouteMessageReceived(_routing);

Is it possible for me to send a message to my NServiceBus.host.exe (when running using process.start) using a WPF application? Is there a way I can use custom initialization to host my own NServiceBus within my wpf app and still configure my endpoints correctly? Or is there a way I can get a handle on the Bus from within my wpf application?
Any example code is greatly appreciated. Thanks in advance!

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

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

发布评论

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

评论(2

撩动你心 2024-12-27 15:02:41

听起来您希望 WPF 应用程序访问 NServiceBus.Host.exe 托管端点中的确切总线实例。如果这是真的,则表明您认为只有一辆巴士,但事实并非如此。

“总线”对于您的端点来说就像以太网卡对于您的计算机一样。每台计算机都需要一个以太网卡,它们使用它们来相互通信。同样,“总线”就像您的以太网卡。每个进程都有自己的进程,它们使用它们相互发送消息。

NServiceBus.Host.exe 使托管端点变得简单,无论是从控制台窗口,还是让您能够将其安装为 Windows 服务,以便它始终运行且可用。您应该永远不需要执行Process.Start()。它还使得总线的配置非常简单——几乎是自动的。您不必执行复杂的Configure.With()...流畅的配置位。

对于任何其他应用程序类型(Windows 控制台应用程序、Windows 窗体应用程序、WPF 应用程序),您需要自己“托管”总线。这意味着执行整个流畅的配置位并将生成的“IBus”实例分配给一个公共静态变量,您可以从该应用程序的任何部分访问该变量。 (在 NServiceBus.Host 版本中,您不必拥有对总线的公共静态引用,因为主机将其创建为单例,并且依赖项注入容器会将其插入到您创建的任何需要它的类中。)

一旦 WPF 应用程序和 NServiceBus.Host.exe 进程都启动并初始化了各自的总线,WPF 应用程序的总线就可以向 NServiceBus.Host.exe 的总线发送消息。

It sounds like you want your WPF application to access the exact Bus instance in your NServiceBus.Host.exe hosted endpoint. If that's true, it indicates that you think there is just one Bus, and this is not the case.

The "Bus" is to your endpoints a lot like an Ethernet card is to your computer. Every computer needs an Ethernet card, and they use them to communicate with each other. In the same way, the "Bus" is like your Ethernet card. Every process has its own, and they use them to send messages to one another.

NServiceBus.Host.exe makes hosting an endpoint simple, either from a console window, or it gives you the ability to install it as a Windows Service so that it is always running and available. You should never need to do a Process.Start(). It also makes the configuration of the Bus very simple - almost automatic. You don't have to do the complicated Configure.With()... fluent configuration bit.

With any other application type (Windows Console App, Windows Forms App, WPF App) you need to "host" the Bus yourself. That means doing the whole fluent configuration bit and assigning the resulting "IBus" instance to a public static variable that you can access from any part of that application. (In the NServiceBus.Host version, you don't have to have a public static reference to the Bus because the Host creates it as a singleton, and the Dependency Injection container will insert it into any class you create that asks for it.)

Once your WPF application and NServiceBus.Host.exe process are both started up and have initialized their respective buses, the WPF app's Bus can send a message to the NServiceBus.Host.exe's Bus.

一口甜 2024-12-27 15:02:41

您想使用 process.start 听起来有点奇怪。为什么不将主机作为 Windows 服务运行并让它一直运行,以便它接收传入的任何消息?但无论如何,如果进程正在运行并且加载消息处理程序,它应该处理消息。

当然,如果您将消息发送到充当主机输入队列的队列,则只需发送到该队列即可。

我认为您是在问是否可以从 wpf 应用程序在主机应用程序中使用总线实例,如果可以,答案是您不想这样做。只需在 wpf 应用程序中创建一个总线实例。像 nservicebus 这样的东西的全部目的是促进通过不同端点的可靠的进程间通信。就用那个吧。

您可以像任何其他应用程序一样在 wpf 应用程序中设置总线并使用它来发送消息。如果您希望 wpf 应用程序也处理消息,则需要为包含处理程序的程序集加载消息处理程序。

it sounds a little odd that you want to use process.start. why not just run the host as a windows service and have it running all the time so it picks up any messages that come in? one way or another, though, if the process is running and it loads message handlers, it should process the messages.

sure, if you send a message to the queue that is serving as the input queue for your host, just send to that queue.

i think you're asking if you can use the instance of the bus in your host application from the wpf application, and if so, the answer is that you wouldn't want to. just create a bus instance in the wpf application. the whole point of something like nservicebus is to facilitate reliable interprocess communication via different endpoints. just use that.

you can set up a bus in the wpf application just like any other application and use it to send messages. if you want the wpf application to also handle messages, you'll need to load message handlers for the assembly containing your handler(s).

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