0
votes

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!

2

2 Answers

2
votes

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.

1
votes

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