same code work well in Console App but not in Silverlight App

Apr 29, 2010 at 3:36 PM
Edited Apr 29, 2010 at 3:41 PM
I write the same code:

ScriptRuntime runtime = Python.CreateRuntime();
dynamic script = runtime.UseFile("script.py");
int p = script.factorial(5);
Console.WriteLine(p.ToString()); // In Console App
// tbkResult.Text = p.ToString(); // In Silverlight App

While the file "script.py" is:

def factorial(n):
"factorial(n) -> returns factorial of n"
if n <= 1: return 1
return n * factorial(n-1)

When running in Silverlight, it thows "NotImplementedException was unhandled by user code" error.

I use the VS2010 and Silverlight 4.0.
May 6, 2010 at 2:58 AM

We don't yet have a build of IronPython out that works against SL4; but it will be released shortly. In the meantime, you can build IronPython against SL4 if you want to use the "dynamic" keyword with Python code.

If you have a SL4 build of IPy, then there's another issue, where is script.py in your Silverlight app? If it's in the XAP file, you'll need to tell the DLR hosting API host to look in the XAP; the Microsoft.Scripting.Silverlight.dll lets you do that: 

using Silverlight = Microsoft.Scripting.Silverlight;
//...
var runtime = Silverlight.DynamicEngine.CreateRuntime();
var python = runtime.GetEngine("python");  
 

Then the rest of your script will work.

May 7, 2010 at 2:36 AM
Hi, jimmysch, thank you for your reply. Two questions:

1. How can I get a SL4 build of IPy? I add script.py throung "right click the solution--->Add---->Existing Item...." in the Silverlight app.

2. When I try do that:

using Silverlgith = Microsoft.Scripting.Silverlight;
//...
var runtime = Silverlight.DynamicEngine.CreateRuntime();

The IntelliSense does not appear the "DynamicEngine", that is to say the "DynamicEngine" is not in the namespace Microsoft.Scripting.Silverlight.

Thanks,
-zeyoo
May 7, 2010 at 10:30 PM

You can just build the "Silverlight 4" build configurations of the IronPython source code. But, i've also just made a build myself and put it online for you at http://jimmy.schementi.com/downloads/ipy-sl4-20100507.zip. It contains the IronPython binaries built against SL4, as well as an example.sln which shows your original example working.

Let me know if you have any other questions,

~Jimmy

May 15, 2010 at 2:51 AM
Edited May 15, 2010 at 2:52 AM
Jimmy, thanks for your example. It can works well.

However, when I re-new a SLApp project and copy your code in my project, run it and it appears a error:

System.IO.FileNotFoundException was unhandled by user code
Message=File 'script.py' not found in language's search path:
StackTrace:
位于 Microsoft.Scripting.Hosting.ScriptRuntime.UseFile(String path)
位于 SilvIronpyhotn.MainPage..ctor()
位于 SilvIronpyhotn.App.Application_Startup(Object sender, StartupEventArgs e)
位于 MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
位于 MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
InnerException:

I want to know:

1) Where I should put the file "script.py"?
2) The search path of method UseFile() and ExecuteFile()

Sorry, my English is poor. I'm a Chinese.

Thanks,
-zeyoo
May 15, 2010 at 3:36 PM
Hi, Jimmy. There is a question:

I try the example you provided at http://jimmy.schementi.com/downloads/ipy-sl4-20100507.zip and I find that the source code run in a Stand-Alone Silverlight Project work well, but in Hosted Silverlight Project does not work well. I want to know how it can work in the Hosted Silverlight Project?

Thanks,
-zeyoo
May 15, 2010 at 7:53 PM

What do you mean by stand-alone SL project and hosted SL project?

May 15, 2010 at 7:55 PM

“script.py” needs to be in the generated XAP file, so you need to right-click->Properties and set its type to “Content” … that will put it in the XAP.

From: zeyoo [mailto:notifications@codeplex.com]
Sent: Friday, May 14, 2010 7:51 PM
To: Jimmy Schementi
Subject: Re: same code work well in Console App but not in Silverlight App [dlr:211030]

From: zeyoo

Jimmy, thanks for your example. It can works well.
However, when I re-new a SLApp project and copy your code in my project, run it and it appears a error:

System.IO.FileNotFoundException was unhandled by user code
Message=File 'script.py' not found in language's search path:
StackTrace:
位于 Microsoft.Scripting.Hosting.ScriptRuntime.UseFile(String path)
位于 SilvIronpyhotn.MainPage..ctor()
位于 SilvIronpyhotn.App.Application_Startup(Object sender, StartupEventArgs e)
位于 MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
位于 MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
InnerException:

I want to know:

1) Where I should put the file "script.py"?
2) The search path of method UseFile() and ExecuteFile()

Sorry, my English is poor. I'm a Chinese.

Thanks, -zeyoo

Read the full discussion online.

To add a post to this discussion, reply to this email (dlr@discussions.codeplex.com)

To start a new discussion for this project, email dlr@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

May 16, 2010 at 11:52 AM
Sorry, I don't how to express them in English. The stand-alone SL project and hosted SL project I refered to means:

Hosted SL project: check the Host the Silverlight application in a new Web site check box to add a separate ASP.NET Web site or ASP.NET Web Application Project to your solution to host the Silverlight application.

Stand-alone SL project: Uncheck the Host the Silverlight application in a new Web site check box if you do not want to use a Web site to host your Silverlight application.

I don't know how do you express them in English.

PS: I tried it today again, then both of them worked well. But I don't know the reason.

Thanks,
-zeyoo
jimmysch wrote:

What do you mean by stand-alone SL project and hosted SL project?