[IronPython] Hosting IronPython in a Silverlight App

Jimmy Schementi Jimmy.Schementi at microsoft.com
Tue Sep 4 23:02:37 CEST 2007


Michael,

Thanks for bringing this to our attention!

When hosting IronPython in a normal C# app, the Hosting API makes some assumptions for that scenario and makes the hosting work "out of the box". Those assumptions are not all true in Silverlight, so a custom Platform Adaptation Layer must be used.

I've attached a working version of your project to this email, and here's what it does different:

Before you create the Python Engine:

    ScriptEnvironmentSetup setup = new ScriptEnvironmentSetup(true);
    setup.PALType = typeof(SilverlightPAL);
    ScriptEnvironment.Create(setup);

This will create the Silverlight Platform Adoption Layer, which is defined here:

    // Definition of Silverlight Platform Adoption Layer:
    public class SilverlightPAL : PlatformAdaptationLayer
    {
        public override Assembly LoadAssembly(string name)
        {
            return Assembly.Load(LookupFullName(name));
        }

        private Dictionary<string, string> _assemblyFullNames = new Dictionary<string, string>();

        public SilverlightPAL()
        {
            LoadSilverlightAssemblyNameMapping();
        }

        // TODO: This will not be necessary as it will eventually move down into the host
        private void LoadSilverlightAssemblyNameMapping()
        {
            AssemblyName clrAssembly = new AssemblyName(typeof(object).Assembly.FullName);
            foreach (string asm in new string[] { "mscorlib", "System", "System.Core", "System.Xml.Core" })
            {
                clrAssembly.Name = asm;
                _assemblyFullNames.Add(asm.ToLower(), clrAssembly.FullName);
            }

            _assemblyFullNames.Add("system.silverlight", "System.SilverLight, Version=1.0.0.0, PublicKeyToken=b03f5f7f11d50a3a");
            _assemblyFullNames.Add("agclr", "agclr, Version=0.0.0.0, PublicKeyToken=b03f5f7f11d50a3a");
            _assemblyFullNames.Add("microsoft.visualbasic", "Microsoft.VisualBasic, Version=8.1.0.0, PublicKeyToken=b03f5f7f11d50a3a");

            AssemblyName dlrAssembly = new AssemblyName(typeof(PlatformAdaptationLayer).Assembly.FullName);
            foreach (string asm in new string[] {
                "Microsoft.Scripting",
                "Microsoft.Scripting.Silverlight",
                "IronPython",
                "IronPython.Modules",
                "Microsoft.JScript.Compiler",
                "Microsoft.JScript.Runtime",
                "Microsoft.VisualBasic.Compiler",
                "Microsoft.VisualBasic.Scripting",
                "Ruby"})
            {
                dlrAssembly.Name = asm;
                _assemblyFullNames.Add(asm.ToLower(), dlrAssembly.FullName);
            }
        }

        protected string LookupFullName(string name)
        {
            AssemblyName asm = new AssemblyName(name);
            if (asm.Version != null || asm.GetPublicKeyToken() != null || asm.GetPublicKey() != null)
            {
                return name;
            }
            return _assemblyFullNames.ContainsKey(name.ToLower()) ? _assemblyFullNames[name.ToLower()] : name;
        }

    }

This allows for IronPython.Modules to be loaded, which is the exception you were getting.

This is *a lot* to get IronPython hosted in Silverlight! This work was done for the DLR/Silverlight integration effort, however simple hosting in Silverlight shouldn't be this hard. I wouldn't be surprised if we decided to move this bit into the DLR host. =)

Let me know if you have any other questions,

~Jimmy

> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-
> bounces at lists.ironpython.com] On Behalf Of Michael Foord
> Sent: Monday, September 03, 2007 2:24 PM
> To: Discussion of IronPython
> Subject: [IronPython] Hosting IronPython in a Silverlight App
>
> Hello all,
>
> I'm experimenting with hosting IronPython in a silverlight (1.1 alpha
> refresh) app.
>
> When I write C# code that uses the PythonEngine it works fine when
> access *from IronPython code*.
>
> If I use the same code as 'code behind', then it throws an exception
> "The method or operation is not implemented."
>
> (At the line "PythonEngine pe = PythonEngine.CurrentEngine;")
>
> The code is:
>
> using System;
> using System.Windows;
> using System.Windows.Controls;
> using System.Windows.Documents;
> using System.Windows.Ink;
> using System.Windows.Input;
> using System.Windows.Media;
> using System.Windows.Media.Animation;
> using System.Windows.Shapes;
> using IronPython.Hosting;
> using Microsoft.Scripting;
>
> namespace EmbeddedWithCodeBehind
> {
>     public partial class Page : Canvas
>     {
>         public void Page_Loaded(object o, EventArgs e)
>         {
>             // Required to initialize variables
>             InitializeComponent();
>
>             try
>             {
>                 PythonEngine pe = PythonEngine.CurrentEngine;
>                 string code = "lambda x: x.upper()";
>                 Function<string, string> func =
> pe.EvaluateAs<Function<string, string>>(code);
>
>                 string x = "i'm talking loudly";
>                 string result = func(x);
>                 textBlock.Text = result;
>             }
>             catch (Exception ex)
>             {
>                 textBlock.Text = ex.Message;
>             }
>
>         }
>     }
> }
>
>
>
> The XAML is:
>
> <Canvas x:Name="parentCanvas"
>         xmlns="http://schemas.microsoft.com/client/2007"
>         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>         Loaded="Page_Loaded"
>
> x:Class="EmbeddedWithCodeBehind.Page;assembly=ClientBin/EmbeddedWithCod
> eBehind.dll"
>         Width="640"
>         Height="480"
>         Background="White"
>         >
>
>   <TextBlock x:Name="textBlock"></TextBlock>
>
> </Canvas>
>
>
> Everything else is the default from an Orcas beta 2 new Silverlight
> project.
>
> Anyone got any clues as to what is happening?
>
> Thanks
>
> Michael Foord
> http://www.ironpython.info/
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: EmbeddedWithCodeBehind.zip
Type: application/x-zip-compressed
Size: 27231 bytes
Desc: EmbeddedWithCodeBehind.zip
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20070904/4f0eacc6/attachment.bin>


More information about the Ironpython-users mailing list