[IronPython] [python] Re: Hosting IronPython in a Silverlight App

Michael Foord fuzzyman at voidspace.org.uk
Wed Sep 5 00:19:25 CEST 2007


Hello Jimmy,

Thanks a lot for this. I'll have a play with it tonight. :-)

Michael
http://www.ironpython.info


Jimmy Schementi wrote:
> 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 (PAL) 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 override the default PAL the Silverlight PAL, 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 by the host, 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
>>     
>> ------------------------------------------------------------------------




More information about the Ironpython-users mailing list