<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>The Problem Solver : IronPython</title><link>http://msmvps.com/blogs/theproblemsolver/archive/tags/IronPython/default.aspx</link><description>Tags: IronPython</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Want to download all MIX sessions?</title><link>http://msmvps.com/blogs/theproblemsolver/archive/2009/03/31/want-to-download-all-mix-sessions.aspx</link><pubDate>Tue, 31 Mar 2009 06:52:40 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1683235</guid><dc:creator>Maurice</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/theproblemsolver/rsscomments.aspx?PostID=1683235</wfw:commentRss><comments>http://msmvps.com/blogs/theproblemsolver/archive/2009/03/31/want-to-download-all-mix-sessions.aspx#comments</comments><description>&lt;p&gt;Then you should check Mike Swanson’s blog post &lt;a href="http://blogs.msdn.com/mswanson/archive/2009/03/26/mix09-keynote-and-session-videos.aspx"&gt;here&lt;/a&gt;. He has two batch files, the first downloads all MIX session recordings with in the format you like, the second renames them so it is easier to make sense of all the media file names.&lt;/p&gt;  &lt;p&gt;If you just want a list of all sessions with download links &lt;a href="http://videos.visitmix.com/MIX09/All"&gt;this&lt;/a&gt; page might be the thing for you.&lt;/p&gt;  &lt;p&gt;So much better than downloading them by hand. Not as good as going to the MIX itself but if you, like me, couldn’t go it is still a nice way to see all the content.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1683235" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/NetFx3/default.aspx">NetFx3</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/WCF/default.aspx">WCF</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/VB/default.aspx">VB</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/webcast/default.aspx">webcast</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/DevCenter/default.aspx">DevCenter</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/WPF/default.aspx">WPF</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/IronPython/default.aspx">IronPython</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/ASP.NET/default.aspx">ASP.NET</category></item><item><title>IronPython in Action</title><link>http://msmvps.com/blogs/theproblemsolver/archive/2009/02/03/ironpython-in-action.aspx</link><pubDate>Tue, 03 Feb 2009 14:54:37 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1668540</guid><dc:creator>Maurice</dc:creator><slash:comments>12</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/theproblemsolver/rsscomments.aspx?PostID=1668540</wfw:commentRss><comments>http://msmvps.com/blogs/theproblemsolver/archive/2009/02/03/ironpython-in-action.aspx#comments</comments><description>&lt;p&gt;I recently received a copy of IronPython in Action by Michael Foord and Christian Muirhead from Manning so I could review it so here it is &lt;img src="http://msmvps.com/emoticons/emotion-1.gif" alt="Smile" /&gt;.&lt;/p&gt; &lt;p&gt;So why am I interested in a book about IronPython? Well I used to do some Python programming a number of years ago and found the language kind of nice to work with. Working with dynamic languages like IronPython relieves you from a lot of the code you have to write just to keep the compiler happy. Admittedly the compiler complains for good reason, to make you think about your code, but often you just add casts and that is all. Now I am not suggesting we dump C# and VB and all start coding all our applications in IronPython but a mix of dynamic and static certainly makes sense to me.&lt;/p&gt; &lt;p align="center"&gt;&lt;a href="http://www.ironpythoninaction.com/"&gt;&lt;img src="http://www.ironpythoninaction.com/images/ironpythoninaction.jpg" alt="" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;But more about the book.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Part 1&lt;/strong&gt; is about getting started with IronPython&lt;/p&gt; &lt;p&gt;The first few chapters provide an overview of how IronPython and Python itself came to be and the basic syntax. The basic syntax of IronPython is not difficult to learn for C# or VB developers as Python is also an object oriented language. One things that tends to freak out people when they start is that the indentation of code matters. As Michael and Christian explain this is not a bad thing but quite brilliant as everyone does so anyway for readability purposes.&lt;/p&gt; &lt;p&gt;Chapter 3 is quite interesting as that explains the interoperability between IronPython and regular complied .NET objects. This is an important chapter as IronPython by itself would not be very interesting, it&amp;#39;s the combination with the full .NET framework and all the embedded functionality and the ease of working of IronPython where the sweet sport is.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Part 2&lt;/strong&gt; is about the code development techniques&lt;/p&gt; &lt;p&gt;Chapter 4 covers the basics of creating applications in IronPython. This covers design patterns like the Model View Controller (MVC) and how they apply to IronPython. Chapter 5 covers Python functions and how you can use some of the dynamic Python function features to read an XML document. In this chapter we get to see some of the using a dynamic language as compared to a static language like C#.&lt;/p&gt; &lt;p&gt;Chapter 6 is about working with properties. IronPython properties are somewhat different that C# or VB properties. Chapter 7 is about unit testing IronPython code. With a dynamic language like Python unit testing is even more important that with regular .NET code. Even though Python is a dynamic language it turns out that mock objects and dependency injection are still as much a part of IronPython unit testing as they are with regular .NET code. The chapter close of with a part about higher order testing and how to test the user interface.&lt;/p&gt; &lt;p&gt;Chapter 8 is about all the magic functions like __init__(self) that Python uses as a naming convention just like we use&amp;nbsp; interfaces in C#. Again some of the concepts might seem a little unusual but once you get used to them it is really quite easy. &lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Part 3&lt;/strong&gt; is about some of the more advanced concepts of using IronPython.&lt;/p&gt; &lt;p&gt;Chapter 9 covers programming Windows Presentation Foundation (WPF) with IronPython. With chapter 10 we are introduced into system administration using IronPython. This is an area where scripting languages are frequently used and IronPython is a perfect candidate. One popular choice for administration scripts is PowerShell and there is a whole section devoted to using PowerShell from within IronPython.&lt;/p&gt; &lt;p&gt;Chapter 11 is about ASP.NET and IronPython. Another interesting place to use IronPython is databases and web services, something covered in chapter 12.&lt;/p&gt; &lt;p&gt;Of course the DLR is part of Silverlight so IronPython is a prima candidate to use with Silverlight development as well. This subject is covered in chapter 13.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Part 4&lt;/strong&gt; is about about interoperability between IronPython by embedding the IronPython runtime inside our own .NET applications and extending IronPython with C# or Visual Basic code. &lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt; &lt;/p&gt; &lt;p&gt;This book is a good read for all .NET developers who want to know about the new trend towards the dynamic languages and IronPython in particular. You get a good explanation what IronPython can do for you and how to go about solving every day programming problems. With the current push towards dynamic behavior, even in languages like C#, every experienced .NET developer should read this book!&lt;/p&gt; &lt;p&gt;Enjoy.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1668540" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/IronPython/default.aspx">IronPython</category></item><item><title>PDC session download</title><link>http://msmvps.com/blogs/theproblemsolver/archive/2008/11/01/pdc-session-download.aspx</link><pubDate>Sat, 01 Nov 2008 10:17:52 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1652746</guid><dc:creator>Maurice</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/theproblemsolver/rsscomments.aspx?PostID=1652746</wfw:commentRss><comments>http://msmvps.com/blogs/theproblemsolver/archive/2008/11/01/pdc-session-download.aspx#comments</comments><description>&lt;p&gt;Want to download and watch all PDC content? Then there are a couple of ways to get at them. The official way if to go through the session agenda at the conference site. See &lt;a title="https://sessions.microsoftpdc.com/public/timeline.aspx" href="https://sessions.microsoftpdc.com/public/timeline.aspx"&gt;https://sessions.microsoftpdc.com/public/timeline.aspx&lt;/a&gt;. You see all sessions but it takes a bit of work. Another nice way is through Channel 9 by using the following feed: &lt;a title="http://channel9.msdn.com/posts/pdc2008/RSS/?tag=videos" href="http://channel9.msdn.com/posts/pdc2008/RSS/?tag=videos"&gt;http://channel9.msdn.com/posts/pdc2008/RSS/?tag=videos&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;But the best as far as I am concerned is using the list compiled by Greg Duncan. Check this list &lt;a title="http://coolthingoftheday.blogspot.com/2008/10/pdc2008-quick-video-link-list.html" href="http://coolthingoftheday.blogspot.com/2008/10/pdc2008-quick-video-link-list.html"&gt;http://coolthingoftheday.blogspot.com/2008/10/pdc2008-quick-video-link-list.html&lt;/a&gt;. And it even has the Visual Basic source code used to generate the list &lt;img src="http://msmvps.com/emoticons/emotion-1.gif" alt="Smile" /&gt;&lt;/p&gt; &lt;p&gt;Enjoy the videos!&lt;/p&gt; &lt;p&gt;&lt;a href="http://www.theproblemsolver.nl/"&gt;www.TheProblemSolver.nl &lt;/a&gt;&lt;br /&gt;&lt;a href="http://wiki.windowsworkflowfoundation.eu/"&gt;Wiki.WindowsWorkflowFoundation.eu&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1652746" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/NetFx3/default.aspx">NetFx3</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/Workflow/default.aspx">Workflow</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/VSTO/default.aspx">VSTO</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/WCF/default.aspx">WCF</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/VB/default.aspx">VB</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/webcast/default.aspx">webcast</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/DevCenter/default.aspx">DevCenter</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/ClickOnce/default.aspx">ClickOnce</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/WPF/default.aspx">WPF</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/SqlCe/default.aspx">SqlCe</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/IronPython/default.aspx">IronPython</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/PDC/default.aspx">PDC</category></item><item><title>Using an IronPython runtime service from Windows Workflow Foundation</title><link>http://msmvps.com/blogs/theproblemsolver/archive/2008/08/25/using-an-ironpython-runtime-service-from-windows-workflow-foundation.aspx</link><pubDate>Mon, 25 Aug 2008 10:15:18 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1645704</guid><dc:creator>Maurice</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/theproblemsolver/rsscomments.aspx?PostID=1645704</wfw:commentRss><comments>http://msmvps.com/blogs/theproblemsolver/archive/2008/08/25/using-an-ironpython-runtime-service-from-windows-workflow-foundation.aspx#comments</comments><description>&lt;p&gt;In &lt;a href="http://msmvps.com/blogs/theproblemsolver/archive/2008/08/14/calling-ironpython-functions-from-net.aspx"&gt;this post&lt;/a&gt; I demonstrated how to create &lt;a href="http://www.codeplex.com/IronPython"&gt;IronPython&lt;/a&gt; objects and call them from strongly typed .NET code. So suppose we want to do so with &lt;a href="http://wiki.windowsworkflowfoundation.eu/default.aspx/WF/Workflow%20Foundation.html"&gt;Windows Workflow Foundation&lt;/a&gt; where could we use this?&lt;/p&gt; &lt;p&gt;Well the most obvious place would be a &lt;a href="http://wiki.windowsworkflowfoundation.eu/default.aspx/WF/WorkflowRuntimeService.html"&gt;runtime service&lt;/a&gt;. The example below uses a very simple message that needs to be displayed but it is easy to see how to same concept could be applied in other places.&lt;/p&gt; &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/theproblemsolver.UsinganIronPythonruntimeservicefromWindo_5F00_A4C6/image_5F00_2.png"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="337" alt="image" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/theproblemsolver.UsinganIronPythonruntimeservicefromWindo_5F00_A4C6/image_5F00_thumb.png" width="541" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;But first the basics. I have created a very simple custom DisplayMessageActivity like this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public partial class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;DisplayMessageActivity &lt;/span&gt;: System.Workflow.ComponentModel.&lt;span style="color:#2b91af;"&gt;Activity
&lt;/span&gt;{
    &lt;span style="color:blue;"&gt;public &lt;/span&gt;DisplayMessageActivity()
    {
    }

    &lt;span style="color:blue;"&gt;public string &lt;/span&gt;Message { &lt;span style="color:blue;"&gt;get&lt;/span&gt;; &lt;span style="color:blue;"&gt;set&lt;/span&gt;; }

    &lt;span style="color:blue;"&gt;protected override &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ActivityExecutionStatus &lt;/span&gt;Execute(
        &lt;span style="color:#2b91af;"&gt;ActivityExecutionContext &lt;/span&gt;executionContext)
    {
        &lt;span style="color:#2b91af;"&gt;IDisplayMessageService &lt;/span&gt;service = executionContext.GetService&amp;lt;&lt;span style="color:#2b91af;"&gt;IDisplayMessageService&lt;/span&gt;&amp;gt;();
        service.Display(Message);

        &lt;span style="color:blue;"&gt;return base&lt;/span&gt;.Execute(executionContext);
    }
}
&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;Next I have created a very simple workflow with only the DisplayMessageActivity and set the Message property to: &amp;quot;This is the message to show.&amp;quot;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You might have noticed in the code above that I am looking for a service of type &lt;span style="color:#2b91af;"&gt;IDisplayMessageService&lt;/span&gt;. This is pretty simple as well and looks like this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public interface &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;IDisplayMessageService
&lt;/span&gt;{
    &lt;span style="color:blue;"&gt;void &lt;/span&gt;Display(&lt;span style="color:blue;"&gt;string &lt;/span&gt;message);
}
&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;In this case I am using an interface but you could just as well use a concrete baseclass with virtual methods.&lt;/p&gt;
&lt;p&gt;For the actual implementation I created two classes in IronPython, the first displays the message on the console while the second uses a MessageBox to display the same message. The IronPython source looks like this:&lt;/p&gt;&lt;pre class="code"&gt;import clr

clr.AddReference(&amp;#39;PythonWorkflowConsoleApplication1&amp;#39;)
from PythonWorkflowConsoleApplication1 import IDisplayMessageService

clr.AddReference(&amp;quot;System.Windows.Forms&amp;quot;)
from System.Windows.Forms import MessageBox

# Display all messages in the console window
class ConsoleDisplayMessageService(IDisplayMessageService):
    def Display(self, message):
        print message

# Display all messages using a Windows.Forms.MessageBox
class MessageBoxDisplayMessageService(IDisplayMessageService):
    def Display(self, message):
        MessageBox.Show(message, &amp;#39;IronPython&amp;#39;)
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Like in the previous example I first import the clr so I can set references to additional assemblies. Next add a reference to the PythonWorkflowConsoleApplication1 assembly. This is my sample application that contains the runtime service interface definition. With this reference set I can import the IDisplayMessageService interface so I can derive from it. &lt;/p&gt;
&lt;p&gt;I included two implementations, lets first look at the ConsoleDisplayMessageService as this is the simpler of the two. All this does is use the Python print function to display the message on the console. The first parameter, self, is the current object, so this in C# or me in VB. We make sure this class derives from IDisplayMessageService by specifying this after the class name. BTW the lines starting with # are comments in Python.&lt;/p&gt;
&lt;p&gt;The second class is MessageBoxDisplayMessageService and this actually uses a MessageBox to display the message from the workflow. First we need to add a reference to the System.Windows.Forms assembly and once that is done we need to import the MessageBox type. Once this is done we can use it like this: &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MessageBox.Show(message, &amp;#39;IronPython&amp;#39;)&lt;/p&gt;
&lt;p&gt;Simple enough right.&lt;/p&gt;
&lt;p&gt;So now for the trick part, adding the IronPython runtime service to the workflow runtime.&lt;/p&gt;
&lt;p&gt;The console main function is as follows:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;static void &lt;/span&gt;Main(&lt;span style="color:blue;"&gt;string&lt;/span&gt;[] args)
{
    &lt;span style="color:blue;"&gt;using &lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;WorkflowRuntime &lt;/span&gt;workflowRuntime = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;WorkflowRuntime&lt;/span&gt;())
    {
        AddMessageService(workflowRuntime, &lt;span style="color:#2b91af;"&gt;DisplayMode&lt;/span&gt;.Windows);

        &lt;span style="color:#2b91af;"&gt;WorkflowInstance &lt;/span&gt;instance = workflowRuntime.CreateWorkflow(
            &lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(PythonWorkflowConsoleApplication1.&lt;span style="color:#2b91af;"&gt;Workflow1&lt;/span&gt;));
        instance.Start();

        &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.ReadLine();
    }
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;With the AddMessageService() doing the actual work. Note the second parameter specifies which IronPython class to load.&lt;/p&gt;
&lt;p&gt;So the AddMessageService looks like this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;static void &lt;/span&gt;AddMessageService(&lt;span style="color:#2b91af;"&gt;WorkflowRuntime &lt;/span&gt;workflowRuntime, &lt;span style="color:#2b91af;"&gt;DisplayMode &lt;/span&gt;mode)
{
    &lt;span style="color:blue;"&gt;string &lt;/span&gt;className;
    &lt;span style="color:blue;"&gt;if &lt;/span&gt;(mode == &lt;span style="color:#2b91af;"&gt;DisplayMode&lt;/span&gt;.Console)
        className = &lt;span style="color:#a31515;"&gt;&amp;quot;ConsoleDisplayMessageService&amp;quot;&lt;/span&gt;;
    &lt;span style="color:blue;"&gt;else
        &lt;/span&gt;className = &lt;span style="color:#a31515;"&gt;&amp;quot;MessageBoxDisplayMessageService&amp;quot;&lt;/span&gt;;

    &lt;span style="color:#2b91af;"&gt;ScriptRuntime &lt;/span&gt;runtime = &lt;span style="color:#2b91af;"&gt;PythonEngine&lt;/span&gt;.CurrentEngine.Runtime;
    &lt;span style="color:#2b91af;"&gt;ScriptScope &lt;/span&gt;scope = runtime.ExecuteFile(&lt;span style="color:#a31515;"&gt;&amp;quot;DisplayMessageService.py&amp;quot;&lt;/span&gt;);

    &lt;span style="color:#2b91af;"&gt;PythonType &lt;/span&gt;pythonType = scope.GetVariable&amp;lt;&lt;span style="color:#2b91af;"&gt;PythonType&lt;/span&gt;&amp;gt;(className);
    &lt;span style="color:blue;"&gt;object &lt;/span&gt;service = runtime.Operations.Call(pythonType);

    workflowRuntime.AddService(service);
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;The basics are the same as in my &lt;a href="http://msmvps.com/blogs/theproblemsolver/archive/2008/08/14/calling-ironpython-functions-from-net.aspx"&gt;previous&lt;/a&gt; IronPython post. Depending on the display mode I make a choice of class to load and I use the same code as previous time to create the object. Finally I add it to the &lt;a href="http://wiki.windowsworkflowfoundation.eu/default.aspx/WF/WorkflowRuntime.html"&gt;WorkflowRuntime&lt;/a&gt; as a &lt;a href="http://wiki.windowsworkflowfoundation.eu/default.aspx/WF/WorkflowRuntimeService.html"&gt;runtime service&lt;/a&gt;. This last bit is easy as this accepts every object type as a valid service and only when we call the GetService do we check the actual type.&lt;/p&gt;
&lt;p&gt;I used IronPython 2.0 Beta 4 for this example. More info about IronPython can be found &lt;a href="http://www.codeplex.com/IronPython"&gt;here&lt;/a&gt;, the download of version 2 &lt;a href="http://www.codeplex.com/IronPython/Release/ProjectReleases.aspx?ReleaseId=14353"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Enjoy!&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.theproblemsolver.nl/"&gt;www.TheProblemSolver.nl &lt;/a&gt;&lt;br /&gt;&lt;a href="http://wiki.windowsworkflowfoundation.eu/"&gt;Wiki.WindowsWorkflowFoundation.eu&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1645704" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/NetFx3/default.aspx">NetFx3</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/Workflow/default.aspx">Workflow</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/DevCenter/default.aspx">DevCenter</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/IronPython/default.aspx">IronPython</category></item><item><title>Calling IronPython functions from .NET</title><link>http://msmvps.com/blogs/theproblemsolver/archive/2008/08/14/calling-ironpython-functions-from-net.aspx</link><pubDate>Thu, 14 Aug 2008 18:17:19 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1644675</guid><dc:creator>Maurice</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/theproblemsolver/rsscomments.aspx?PostID=1644675</wfw:commentRss><comments>http://msmvps.com/blogs/theproblemsolver/archive/2008/08/14/calling-ironpython-functions-from-net.aspx#comments</comments><description>&lt;p&gt;Having worked with Python in the past I find IronPython an interesting language to work with. However the story becomes really interesting if you can combine IronPython with regular strong typed code into one.&lt;/p&gt; &lt;p&gt;So I decided to take a look at what it would take to do so with the current IronPython version. I used IronPython 2 Beta 3, based upon the current version of the Dynamic Language Runtime, which is still evolving so the classes might have changed, take care! You can download the latest IronPython release &lt;a href="http://www.codeplex.com/IronPython"&gt;here&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;h3&gt;Lets take a look at what it takes to use an IronPython class from a Visual Basic Project.&lt;/h3&gt; &lt;p&gt; The first thing we need is a base class we are actually going to derive from. This is because IronPython doesn&amp;#39;t compile its code into a static type but VB requires its variables to be typed. This is the base class I am using:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Public Class &lt;/span&gt;HelloWorldVB
    &lt;span style="color:blue;"&gt;Public Overridable Function &lt;/span&gt;HelloWorld(&lt;span style="color:blue;"&gt;ByVal &lt;/span&gt;name &lt;span style="color:blue;"&gt;As String&lt;/span&gt;) &lt;span style="color:blue;"&gt;As String
        Return String&lt;/span&gt;.Format(&lt;span style="color:#a31515;"&gt;&amp;quot;Hello &amp;#39;{0}&amp;#39; from Visual Basic&amp;quot;&lt;/span&gt;, name)
    &lt;span style="color:blue;"&gt;End Function
End Class
&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;And a main module to start thing off:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Sub &lt;/span&gt;Main()

    &lt;span style="color:blue;"&gt;Dim &lt;/span&gt;helloWorld &lt;span style="color:blue;"&gt;As New &lt;/span&gt;HelloWorldVB()
    Console.WriteLine(helloWorld.HelloWorld(&lt;span style="color:#a31515;"&gt;&amp;quot;Maurice&amp;quot;&lt;/span&gt;))

    Console.ReadLine()
&lt;span style="color:blue;"&gt;End Sub
&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;No big surprise here &lt;img src="http://msmvps.com/emoticons/emotion-1.gif" alt="Smile" /&gt;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h3&gt;So how do we derive an IronPython class from our VB type?&lt;/h3&gt;
&lt;p&gt;The following code is the IronPython code I added to the file HelloWorld.py.&lt;/p&gt;&lt;pre class="code"&gt;import clr
clr.AddReference(&amp;#39;IronPythonTest2&amp;#39;)
from IronPythonTest2 import HelloWorldVB

class HelloWorldIronPython(HelloWorldVB):
    def HelloWorld(self, name):
        return &amp;quot;Hello &amp;#39;&amp;quot; + name + &amp;quot;&amp;#39; from IronPython&amp;quot;
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Basically this code adds a reference to the IronPythonTest2 assembly, this is my console application containing the previous VB code. Next it imports the HelloWorldVB type from the assembly. Now it know about the type we can go ahead and derive from that type and create a new one. This exactly what the line class HelloWorldIronPython(HelloWorldVB) does, it creates a new type called HelloWorldIronPython. Next we override the HelloWorld function and return a different welcome string. A few things to note here: Python uses indentation to specify blocks, so the function being indented from the class is a part of that and the line of code with the return is indented from the def that defines the function so it is part of the function body. Another interesting thing is the self parameter. Every function in Python is passed a reference to itself as the first parameter. Sort of like the implicit Me in VB except a bit more obvious.&lt;/p&gt;
&lt;p&gt;If you want to know more about IronPython I suggest you get the book &lt;a href="http://www.manning.com/foord/"&gt;IronPython in Action&lt;/a&gt; from &lt;a href="http://www.voidspace.org.uk/python/weblog/index.shtml"&gt;Michael Foord&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src="http://www.manning.com/foord/foord_cover150.jpg" alt="" /&gt; &lt;/p&gt;
&lt;p&gt;To make sure the HelloWorld.py file is available to the console application I have set the&amp;nbsp; Copy to Output Directory to Copy always.&lt;/p&gt;
&lt;p&gt;OK so far so good. We have an IronPython class with the code we need. Now we still need to create an object from it. This isn&amp;#39;t hard it just takes a bit more doing than just using the New operator.&lt;/p&gt;
&lt;h3&gt;Instantiating the IronPython object&lt;/h3&gt;
&lt;p&gt;The first step is adding the required references. We need to add the following assemblies:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;IronPython.dll&lt;/li&gt;
&lt;li&gt;IronPython.Modules.dll&lt;/li&gt;
&lt;li&gt;Microsoft.Scripting.dll&lt;/li&gt;
&lt;li&gt;Microsoft.Scripting.Core.dll&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;We will only be using the 1st and 3rd ourselves but the others are needed as well.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Once we have the references set we can add the required Imports:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Imports &lt;/span&gt;Microsoft.Scripting.Hosting
&lt;span style="color:blue;"&gt;Imports &lt;/span&gt;IronPython.Hosting
&lt;span style="color:blue;"&gt;Imports &lt;/span&gt;IronPython.Runtime.Types
&lt;/pre&gt;
&lt;p&gt;And now we can get really going with the code &lt;img src="http://msmvps.com/emoticons/emotion-1.gif" alt="Smile" /&gt;&lt;/p&gt;
&lt;p&gt;We need to load the file with the Python source code using an IronPython ScriptRuntime. Once done we can get a reference to the IronPython type object using the GetVariable() function by passing in the class name. Once we have that we call the ScriptRuntime again to actually create the object. See the code below:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;runtime &lt;span style="color:blue;"&gt;As &lt;/span&gt;ScriptRuntime = PythonEngine.CurrentEngine.Runtime
&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;scope &lt;span style="color:blue;"&gt;As &lt;/span&gt;ScriptScope = runtime.ExecuteFile(&lt;span style="color:#a31515;"&gt;&amp;quot;HelloWorld.py&amp;quot;&lt;/span&gt;)
&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;pythonType &lt;span style="color:blue;"&gt;As &lt;/span&gt;PythonType = scope.GetVariable(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;PythonType)(&lt;span style="color:#a31515;"&gt;&amp;quot;HelloWorldIronPython&amp;quot;&lt;/span&gt;)
helloWorld = &lt;span style="color:blue;"&gt;CType&lt;/span&gt;(runtime.Operations.Call(pythonType), HelloWorldVB)
Console.WriteLine(helloWorld.HelloWorld(&lt;span style="color:#a31515;"&gt;&amp;quot;Maurice&amp;quot;&lt;/span&gt;))
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;And with this code we are good to go. Running the complete code will show the output below:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/theproblemsolver.CallingIronPythonfunctionsfrom.NET_5F00_113A8/image_5F00_2.png"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="146" alt="image" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/theproblemsolver.CallingIronPythonfunctionsfrom.NET_5F00_113A8/image_5F00_thumb.png" width="335" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;The complete code in the main module:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Option Explicit On
Option Strict On

Imports &lt;/span&gt;Microsoft.Scripting.Hosting
&lt;span style="color:blue;"&gt;Imports &lt;/span&gt;IronPython.Hosting
&lt;span style="color:blue;"&gt;Imports &lt;/span&gt;IronPython.Runtime.Types

&lt;span style="color:blue;"&gt;Module &lt;/span&gt;Module1
    &lt;span style="color:blue;"&gt;Sub &lt;/span&gt;Main()
        &lt;span style="color:blue;"&gt;Dim &lt;/span&gt;helloWorld &lt;span style="color:blue;"&gt;As New &lt;/span&gt;HelloWorldVB()
        Console.WriteLine(helloWorld.HelloWorld(&lt;span style="color:#a31515;"&gt;&amp;quot;Maurice&amp;quot;&lt;/span&gt;))

        &lt;span style="color:blue;"&gt;Dim &lt;/span&gt;runtime &lt;span style="color:blue;"&gt;As &lt;/span&gt;ScriptRuntime = PythonEngine.CurrentEngine.Runtime
        &lt;span style="color:blue;"&gt;Dim &lt;/span&gt;scope &lt;span style="color:blue;"&gt;As &lt;/span&gt;ScriptScope = runtime.ExecuteFile(&lt;span style="color:#a31515;"&gt;&amp;quot;HelloWorld.py&amp;quot;&lt;/span&gt;)
        &lt;span style="color:blue;"&gt;Dim &lt;/span&gt;pythonType &lt;span style="color:blue;"&gt;As &lt;/span&gt;PythonType = scope.GetVariable(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;PythonType)(&lt;span style="color:#a31515;"&gt;&amp;quot;HelloWorldIronPython&amp;quot;&lt;/span&gt;)
        helloWorld = &lt;span style="color:blue;"&gt;CType&lt;/span&gt;(runtime.Operations.Call(pythonType), HelloWorldVB)
        Console.WriteLine(helloWorld.HelloWorld(&lt;span style="color:#a31515;"&gt;&amp;quot;Maurice&amp;quot;&lt;/span&gt;))

        Console.ReadLine()
    &lt;span style="color:blue;"&gt;End Sub
End Module
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Enjoy!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1644675" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/VB/default.aspx">VB</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/DevCenter/default.aspx">DevCenter</category><category domain="http://msmvps.com/blogs/theproblemsolver/archive/tags/IronPython/default.aspx">IronPython</category></item></channel></rss>