Configuring, Testing and Probing NServiceBus with IronPython
I have recently been working with NServiceBus quite a bit and whilst I was working on a WPF IronPython Console - User Control / Window, it occurred to me that it would be a useful tool in testing and monitoring behaviour of my services whilst running under NServiceBus. Let’s have a look at the simplest of examples by configuring up an IBus and publishing messages using Udi’s PubSub sample.
The first change to Udi’s sample code comes in the publisher where the main method’s body no longer contains any configuration code whatsoever.
using System; using Common.Logging; using NServiceBus; using Messages; using System.Collections.Generic; using System.ComponentModel; using Org.TechA.Wpf.Controls; using NServiceBus.Unicast.Transport.Msmq.Config; using NServiceBus.Unicast.Transport.Msmq; using NServiceBus.ObjectBuilder; //register the extensions for configuration //so IronPython can see the extension methods [assembly: Microsoft.Scripting.Runtime.ExtensionType( typeof(Configure), typeof(ConfigureSpringBuilder))] [assembly: Microsoft.Scripting.Runtime.ExtensionType( typeof(Configure), typeof(ConfigureMsmqSubscriptionStorage))] [assembly: Microsoft.Scripting.Runtime.ExtensionType( typeof(Configure), typeof(ConfigureXmlSerializer))] [assembly: Microsoft.Scripting.Runtime.ExtensionType( typeof(Configure), typeof(ConfigureMsmqTransport))] [assembly: Microsoft.Scripting.Runtime.ExtensionType( typeof(Configure), typeof(ConfigureUnicastBus))] [assembly: Microsoft.Scripting.Runtime.ExtensionType( typeof(Configure), typeof(ConfigureUnicastBus))] namespace Server { class Program { static IBus bus = null; [STAThread] static void Main() { LogManager.GetLogger(“hello”).Debug(“Started.”); var ir_window = new ConsoleWindow(); ir_window.ShowDialog(); } } }
What’s been added here is the decoration of the assembly with a set of ExtensionType attributes from the DLR to allow us to program against the NServiceBus fluent interfaces extension methods from IronPython. The other thing to note in the code above is that the configuration code from the PubSub sample is removed and what now remains is to create and run an instance of the IronPython Application Shell, effectively embedding IronPython into my NServiceBus publisher. Once this is done we are all set to start scripting our services and NServiceBus. If I run this code above I expect to see the the IronPython Application Scripting Window pop up ready for action.
Configure the Bus and Send a message
Here’s a look at a sample script typed at the console window (see above) to configure the bus and start sending some messages.
#import the relevant libraries from System import * from NServiceBus import * import clr #add a reference to the messages #library from the pubsub sample #and import it clr.AddReference(“Messages”) from Messages import * #configure the IBus #NOTE: type this line as a WHOLE in the #IronPython Console Window bus = Configure.With() .SpringBuilder() .MsmqSubscriptionStorage() .XmlSerializer().MsmqTransport() .IsTransactional(True).PurgeOnStartup(False) .UnicastBus() .ImpersonateSender(False) .CreateBus() .Start() #create some messages msg1 = EventMessage() msg1.EventId = Guid.NewGuid() msg1.Duration = TimeSpan(99999) msg1.Time = DateTime.Now msg2 = EventMessage() msg2.EventId = Guid.NewGuid() msg2.Duration = TimeSpan(99999) msg2.Time = DateTime.Now.AddYears(-1) #prove that the messages have values print msg1.EventId.ToString() print msg2.EventId.ToString() #put the messages in an array msgs = Array[EventMessage]((msg1, msg2)) #publish the messages bus.Publish[EventMessage](msgs)
Running the Code
After running this script above we can expect our subscribers to start receiving the messages and we can check to see if indeed the Guid and DateTime property values in the messages match up once being received by the subscribers.
As you can see, our EventId values and other properties all match those created using a REPL experience with IronPython embedded as our publisher. The Dynamic languages turn out to be useful in other places when it comes to NServiceBus but we will leave that for another post soon.
Also, I would like to point out that you could leave the configuration code in the C# code that starts up the publisher and push the IBus instance as a variable into the scope of the IronPython runtime and then execute a similar (smaller) script against it. The IronPython Application Shell has a method that allows you to add variables into the scope ad-hoc from the statically typed side of the publisher. Here’s how to achieve that:
[STAThread] static void Main() { LogManager.GetLogger(“hello”).Debug(“Started.”); bus = NServiceBus.Configure.With() .SpringBuilder() .MsmqSubscriptionStorage() .XmlSerializer() .MsmqTransport() .IsTransactional(true) .PurgeOnStartup(false) .UnicastBus() .ImpersonateSender(false) .CreateBus() .Start(); var ir_window = new ConsoleWindow(); var vars = new Dictionary<string, object>(); vars.Add(“bus”, bus); ir_window.ctlIronPythonConsole.OnVariablesAdded(vars); ir_window.Title = “NServiceBus IronPython Publisher”; ir_window.ShowDialog(); }
You can find the full code here and if your interested in downloading The IronPython Application Shell, it can be found here.
Below is the link to the full sample including the the NServiceBus PubSub sample, however you should remember to setup the queues that are required (by the NServiceBus samples themselves) by running the VbScript that comes with the download of NServiceBus itself.
NServiceBus-Pub/Sub Example with IronPython Application Shell
No comments








