Archive

Archive for the ‘IronPython’ Category

Hacking on the NServiceBus Generic Host with IronPython–TBC.

June 21st, 2011 Simon Segal No comments

This (now series of) posts(s) will be something of a stream of consciousness, an exploration that has no single objective, rather a journey of discovery with no guarantee (sought nor otherwise) of any practical application. Something tells me that there is some niche lurking behind the premise however beyond my initial instinct I am prepared to let any hidden benefits (if there are any) reveal themselves to me along the way.

A while back I posted on how it was possible to self host an NServiceBus endpoint in a custom built IronPython / WPF REPL process. Since that time NServiceBus has experienced several feature enhancements and now supports a rich hosting model of its own, the Generic Host. I wont regurgitate what has already been written about the Generic Host as its been covered in several places and if you wont to know more about it I suggest you look at these links:

The Generic Host

The Profiles

Sample

So Why an IronPython Host

In short, the ability to throw ad-hoc messages at your NServiceBus ecosystem and observe your systems behaviour (eye to eye) and some dynamic debugging! The typical debugging scenario has me sitting with four or more console windows open acting as either clients, servers and or publishers and some of those windows will be waiting for input (from me), the kind of input you typically see in a console application “Press y to send a message” (usually a fairly statically crafted and compiled one). When speaking about clients and subscribers sometimes (going into production) that just might be a windows desktop application.

In the world of dynamic languages developers have come to expect the ability to test / play with / mess with / debug their code in a Repeat Evaluate Print Loop which has some significant benefits.

Start-up

Using the NServiceBus Full Duplex sample I added a reference to my hacked version of the Generic Host and set the project to start-up the executable as per the usual approach (when you have chosen not to self host in your own executable).

repl_host_debug_props

After starting the Server project the console fires up as expected and is then followed by the IronPython console where the hosted Bus (instance) has been made available to the script scope context we have started in.

Next I proceed to hack out a message and send it on to the Bus. What isn’t clear below is that Message1 is actually an already dialled up instance of a RequestDataMessage as defined in the message project that is part of the Full Duplex sample that comes in the NServiceBus download.

NServiceBus REPL

Here is the Message Handler (in the Server project) for the RequestDataMessage (in the script above Message1 is an instance of this type.

image

Once the script above gets executed then the REPL reports back to us (I have hijacked the STD/IO and redirected it to the REPL’s TextWriter.

repl_host_first_results

Ok, so it seems obvious enough that the Generic Host is now starting up in the context of our Python REPL and I can adhoc hand craft messages and observe the behaviour of the hosted endpoint, but there are some still some issues, one immediate and obvious one is that the REPL Host is doing two things or rather taking on two independent roles namely the Client and Server. If you run the Full Duplex sample you will note that there are two processes in the solution, a client configured AsA_Client and another configured AsA_Server; when I run the Server project using the Generic Host Shell I am freeing the console window and instead running the REPL window in its place. What this means quite literally is that in this instance, the REPL Host is allowing me to send messages to the Server Endpoint (which it too is hosting), without the Client endpoint running at all, thereby effectively spoofing some of the behaviour found in the Client process (project) without actually loading the client endpoint at all.

Alternatively I can run both projects (Client and Server) as I normally would, with the exception that they are both being hosted in the IronPython host. This (arguably) offers more fidelity with respect to the two services operating entirely within their own process which is in line with a production deployment.

clientAndServer

What the image above demonstrates is that we now have two REPL Hosts running simultaneously, one endpoint configured AsA_Client and the other AsA_Server endpoint.

I am going to leave part 1 here for now and end with an idea that has come to mind: perhaps it would be useful to run a solution (furnished with hosted endpoints) and have all the REPL hosts start up within a container, perhaps an MDI host where all the REPL windows tile / cascade? It also occurs that perhaps such this REPL host would be useful for inspecting and returning source messages to their queues or performing functions like those found in the Grid UI or perhaps even something akin to an integrated shell where an Administrator might walk up to a machine hosting NServiceBus endpoints and issue those endpoints with administrative messages? Anyway I will meditate on it for a while and perhaps stay open for some outside input.

In the next post I will talk about some of the issues facing me currently and should be able to include a link to the working example by then. In the meantime you can find the old post here and download the REPL as it was back then.

Finally, the prototype IronPython console I built some time ago is really just that, a prototype and given that I plan to use it as a live runtime tool embedded in my commercial products (at some point in time) then its really time to take it from prototype to working draft and that means its time to get serious about styling the window beyond battleship grey.

Share/Save/Bookmark

IronPython Tools for Visual Studio with Expression Blend

May 9th, 2010 Simon Segal No comments

Just as I was about to embark on  a new project (codenamed PONGO), the IronPython team announced a CTP 2.0 drop of the IronPython tools for Visual Studio 2010 which is exciting on a number of different levels.

If IronRuby and IronPython eventually make it all the way to Visual Studio integration in the full sense (that is packaged as part of Visual Studio itself) that would be a mark of first class citizenry.

What about Expression Blend?

Personally I work in both Blend and Visual Studio and even though the Visual Studio designer is somewhat improved and it’s intellisense is far superior than that of Expression Blend, it still does not provide me with the same fidelity or fluency in the design experience as I get from Blend. This leaves me with a problem when it comes to building applications where IronPython is the choice language.

It would have been unrealistic (at this stage) to expect Blend to open IronPython projects created in Visual Studio and sure enough that is the not case. What might be useful is Blend having a “design XAML only mode”, enabling it to open XAML files individually, taking advantage of the design experience it has to offer. Still not ready to give up on using Blend with XAML files that were part of a Visual Studio IronPython project, I set out to find a way.

Symbolic Links

The first thing I tried out was to see if Symbolic and Hard linked files would make the difference? First up I tried a symbolic link using the MKLINK utility:

MKLINK [[/D] | [/H] | [/J]] Link Target

Before doing this I created two projects, the first one an IronPython Tools project generated in Visual Studio and the second one a C# project created in Blend.

PongoProjects

It should be obvious from the naming which project is which. In the blend project I removed the default XAML file generated by Blend.

Next came the file linking, I used only the Link and Target command line options, creating a symbolic link using the same name as the .XAML file in my IronPython project.

MKLINK pongo.xaml d:\temp\PongoBlend\pongo.xaml

Having successfully created the soft linked symbolic file I tried to add it to the Blend project only to find that it would not open – which, to blunt I wasn’t expecting to work. I was a little more hopeful however when it came to the Hard Linked file which I setup as follows:

MKLINK \H pongo.xaml d:\temp\PongoBlend\pongo.xaml

This did in fact open correctly in Blend and worked for a brief moment, when I changed something in Blend – Visual Studio would prompt me that the file had been changed outside the editor, unfortunately my celebrations were short lived when I found that the files were not updating in both locations when one or the other was subject to an edit. After some investigation and testing it appears that once opening the file in both the IDE’s the links would somehow eventually become broken and so the symbolic linking idea was now officially a lost prospect.

Duelling Projects

Finally I decided to synchronize two separate .XAML files in each of the two different projects, so the challenge was to find the most unobtrusive way of working in both IDE’s and easily syncing the the .XAML files after edits had been made. Given that one of the IDE’s was running a dynamic language with a REPL built in, I thought it shouldn’t prove too difficult.

pongvsnet_interactive

What I did was to add two .py source files into the project, each one with some code that would sync in one direction, either from Blend to Visual Studio and vice versa. Sure its  far from a perfect solution but it just might be a price I’m willing to pay in the short term to get the benefit of using Blend. In the long term I really do hope that as the dynamic languages gain support in Visual Studio we also get some integrated ways to work in both IDE’s.

pongvsnet_with_xaml_ui

Here’s the very same Pongo.xaml file (or copy of it to be precise) in Expression Blend, but before you get too excited, the “send to interactive console” context menu option seems to suffer some kind of inconsistent behaviour (on my machine any way), causing the File copy operation in my .py files to work on only some occasions.

pong_in_blend

Undeterred I decided to fall back to an even more disintegrated experience and opt for running the synchronizing .py files in IPY.exe itself. This is easily achieved by associating .py files to the IPY.exe in Visual Studio using the context menu’s “open with” option, which is a something existing Visual Studio users will be very familiar with.

Like I said its far from a perfect solution and to be frank it’s somewhat annoying however my frustration is another thing altogether if I’m faced with doing all my layout and design in Visual Studio – so I will live with it for the moment. Hopefully the IronPython team and or the Expression Blend Team can find a solution that flows changes through more seamlessly in the short term and the perhaps in the long term allow us to open IronPython and IronRuby projects in Blend.

By the way if your wondering about the codename of the project that drove this adventure (PONGO) – the answer is yes if you guessed that it’s something that revolves around IronPython and MongoDB and I will be blogging about that more in the coming weeks.

Share/Save/Bookmark

IronPython and the Entity Framework Part 5.0 – Concluded

February 6th, 2010 Simon Segal No comments

This is the final part of the series on using IronPython together with the Entity Framework.

Part 1.0

In this first instalment we setup the discussion on what we were trying achieve, how to use the __clrtype__ class in IronPython 2.6 to create PODO’s (Plain Old Dlr Objects) ala POCO, for integration with the Entity Framework. Our aim is to use the Entity Framework using the Beta 2 of Entity Framework 4.0.

Part 2.0

We dig in deep and explore the internals of what’s required to create a PODO (POCO for the DLR). We also build an IronPython ObjectContext and determine how to manage mapping between the Entities and Database. We achieve a successful test run and prove that we can in simple cases get IronPython and Entity Framework to worth together in this fashion. Finally in part 2.0 we take steps to verify lazy loading for an entity graph and we strike our first hurdle, leaving us to look forward to part 3 to see if the problems encountered might be overcome.

Part 3.0

We start to examine the issues with scalar / native types and see how proxies fit into the puzzle. We check out eager loading and whilst we do have some success we still face a problem in rounding out the whole story. Finally we prepare to journey into debugging a crash dump file for part 4.

Part 4.0

Using Visual Studio 2010 we successfully load up our crash dump file in the hope of jumping over the final hurdle. We discover that the specialized version of IronPython 2.6 does not have publicly available debug symbols but we can manage to see where the active thread was when the fatal crash occurred.

The State of Play

squarepegroundhole The current set of problems unfortunately leaves us short in terms of using the Entity Framework from IronPython, however I did previously demonstrate (in two parts here and here) how this can be achieved by way of introducing an interop layer with C# addressing the Entity Framework.

Part 1.0

Part 2.0

Part 3.0

Part 4.0

It’s my hope that this post will make the previous posts easier to find given my recent problems with a blog scraping site. Finding the original content has been difficult in light of my successfully having the scraping site in question remove my content.

Share/Save/Bookmark

Creative Commons Attribution-ShareAlike 2.5 Australia
Creative Commons Attribution-ShareAlike 2.5 Australia