Ok, I believe I've gotten this to work. I know very little about ASP.NET so
I'm just going to walk through the steps to setup a new Web Site which can use
the DLR. The key limitation here is that if you're referring to a type out of
Microsoft.Scripting.Core.dll you'll need to do it from a .CS file. If you like
inline <script> tags you can still use them but you'll need to add helpers to
your .cs file for getting whatever you'd want from the alternate namespace.
Let's get started. First you need to install the DLR & IronPython (or other
DLR languages) into the GAC.
Next in VS create your web site - File->New->Web Site... Save it somewhere.
In the new project you need to make 2 updates to web.config:
First add the compiler options reference to
Microsoft.Scripting.Core.dll for whatever language you're using. Luckily you
don't need this very often so having the extra verbose syntax isn't too bad.
I'm using C# so this ends up looking like:
<compiler language="c#;cs;csharp" extension=".cs" warningLevel="4"
type="Microsoft.CSharp.CSharpCodeProvider, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
compilerOptions=""/reference:DLR=C:\Program
Files\IronPython 2.0Beta4\Microsoft.Scripting.Core.dll"">
Second add a reference to Microsoft.Scripting.dll so you have
access to the hosting APIs:
<assemblies>
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral,
PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Extensions, Version=3.5.0.0,
Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0,
Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral,
PublicKeyToken=B77A5C561934E089"/>
<add assembly="Microsoft.Scripting, Version=1.0.0.4000,
Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"/>
</assemblies>
Next let's drop in our code. The code needs to go into the .cs file associated
with the page. In the new site you have a Default.aspx.cs page which is
pre-created for you. At the top of that file you need to add "extern alias
DLR;" - or whatever alias you used when adding the Microsoft.Scripting.Core.dll
reference. Next add a using statement to bring in Microsoft.Scripting.Hosting.
Now let's write some code in this .cs file!
protected string RunSomeCode(string code) {
var sr = ScriptRuntime.Create();
var engine = sr.GetEngine("py");
var scope = engine.CreateScope();
var source = engine.CreateScriptSourceFromString(code,
DLR.System.Scripting.SourceCodeKind.Expression);
return source.Execute(scope).ToString();
}
protected void OnClickHandler(object sender, EventArgs args) {
result.Text = RunSomeCode(foo.Value);
}
Note the DLR.System.Scripting... when accessing code from
Microsoft.Scripting.Core. Most of the hosting APIs are in the
Microsoft.Scripting.dll so we usually don't need to use that.
Finally edit the default page so it has some space for us to experiment with
scripting. Mine ends up looking like:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="Form1" runat="server">
<textarea runat="server" id = "foo" cols = "40" rows="1"/>
<br />
<asp:Button ID="Button1" runat="server" text="Submit!"
onclick="OnClickHandler"/>
</form>
Results: <asp:Label runat="server" id = "result"/>
</body>
</html>
Press F5, type 2+2 into the text box and hit submit. It's 4!
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Dody Gunawinata
Sent: Tuesday, August 19, 2008 12:01 PM
To: Discussion of IronPython
Subject: Re: [IronPython] defined in multiple assemblies (framework 3.5)
OK. Thanks a lot. In the meantime, I'll proceed try to modifying the namespaces
in Microsoft.Scripting.Core (if it's actually possible. Hopefully there's no
internal classes being used)
Dody G.
On Tue, Aug 19, 2008 at 9:38 PM, Dino Viehland <[EMAIL PROTECTED]<mailto:[EMAIL
PROTECTED]>> wrote:
I'm continuing to look into it... We're going to have conflicting names
because Microsoft.Scripting.Core includes a superset of the functionality in
the v3.5 System.Core - and changing that would complicate our internal builds
quite a bit. But hopefully we can find a way to get the aliases working in web
site mode.
-----Original Message-----
From: [EMAIL PROTECTED]<mailto:[EMAIL PROTECTED]> [mailto:[EMAIL
PROTECTED]<mailto:[EMAIL PROTECTED]>] On Behalf Of Fernando Correia
Sent: Tuesday, August 19, 2008 11:33 AM
To: Discussion of IronPython
Subject: Re: [IronPython] defined in multiple assemblies (framework 3.5)
This issue is very bad for my project too. Do you think there is a way
we can make IronPython compatible with the Framework 3.5?
I find it appalling that we even have to consider that...
_______________________________________________
Users mailing list
[email protected]<mailto:[email protected]>
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
_______________________________________________
Users mailing list
[email protected]<mailto:[email protected]>
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
--
nomadlife.org<http://nomadlife.org>
_______________________________________________
Users mailing list
[email protected]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com