0

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?

1
  • What is the reason behind having another process? You can host NSB right in your WPF app.
    – Adam Fyles
    Dec 14, 2011 at 1:09

1 Answer 1

1

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.

2
  • I am interested to know more about the bootstrap method. Would it look something like this? using NServiceBus; namespace MyClient { public class ClientInit : IWantCustomInitialization { public void Init() { public static IBus Bus { get; private set; } public static void Init() { Bus = NServiceBus.Configure.With() .Log4Net() ..... .Start(); } } }
    – EnLaCucha
    Dec 12, 2011 at 10:55
  • sorry, hate the return carriage, was not done. I mean like this: public static IBus Bus { get; private set; } public static void Init() { Bus = NServiceBus.Configure.With() .Log4Net() ... .CreateBus() .Start(); } If so, how would I call this from wpf app to start NServiceBus.host.exe and set the IBus Bus static property within the wpf application at same time? Is this init file stored in the NServiceBus project and run when I execute NServiceBus.host.exe? Do you have example code?
    – EnLaCucha
    Dec 12, 2011 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.