I need to access a static property from a console application (NServiceBus selfhost) while the console application is running. Is this possible?
When the NServiceBus console is executed, a static property is set in my starup class:
public static IBus Bus { get; private set; }
public static void Init()
{
Bus = NServiceBus.Configure.With()
.Log4Net()
.DefaultBuilder()
.XmlSerializer()
.MsmqTransport()
.IsTransactional(false)
.PurgeOnStartup(false)
.UnicastBus()
.LoadMessageHandlers()
.ImpersonateSender(false)
.CreateBus()
.Start();
}
Another class within the NServiceBus console application calls for the static bus property. I am only referencing the associated console application dll to obtain this message object. I create this message object to call it’s public method “SendRouteMessageReceived”. However, I need to set the bus here to the bus that was instantiated during execution of the NServiceBus console (shown in code above).
public class OrderMessaging
{
public IBus Bus { get; set; }
public void SendRouteMessageReceived(LabRoutingUpdateMessage routingUpdateMessage)
{
Bus.Send(new RouteMessageReceived(routingUpdateMessage));
}
}
C# code from my WPF application to instantiate the message object and send the message to NServiceBus:
MyBus.OrderManagement.OrderMessaging routeMessageReceived = new MyBus.OrderManagement.OrderMessaging();
I use the following code from my WPF application to start the NServiceBus (selfhost)console application:
System.Diagnostics.Process.Start("MyApplication.exe");
Can someone provide example code how I might execute a console application from within a WPF application and access a static property that was instantiated in the console app?
Update: I must add that the Bus object is started a separate host console exe, while I also start the “Message” NServiceBus console separately via this code:
System.Diagnostics.Process.Start("MyApplication.exe"); (this is the exe where the bus is instantiated – this is the selfhost service bus console application)
System.Diagnostics.Process.Start("NServiceBus.Host.exe"); (this is the service host where my message object class is used in my wpf application)
Is there a better way to start both services outside visual studio to keep the static bus property shared between both hosted services?
My advise: Don’t. You want interprocess communication through messaging (ie via NServiceBus and MSMQ). Not via windows api and other complicated stuff.
Define your messages in a separate assembly. Define your Init function (bootstrap method) in a shared assembly and give each application its own static IBus, wich is initialized once at the start of each application through the Init function.
I suspect you want a basic publisher / subscriber construction. A nice example to accomplish this is given here.