Hosting

From $1

Table of contents
No headers

Here is some code that we put together for the ASP.NET MVC team. They used it to prototype their IronRuby integration that we showed at Tech Ed 2008. Here's how you can execute a simple file:

using Microsoft.Scripting.Hosting;

var runtime = ScriptRuntime.Create();      
runtime.ExecuteFile("MyController.rb")

Where MyController.rb contains:

class MyController
  def do_foo a, b
    puts a, b
  end
end

This will define the MyController class and the do_foo method. Here's some code that instantiates the controller and retrieves the action method:

var engine = runtime.GetEngine("Ruby");
// TODO: should check that the values are identifiers
var code = String.Format("{0}.new.method :{1}", "MyController", "do_foo");
var action = engine.CreateScriptSourceFromString(code).Execute();

The action variable now holds on do_foo method bound to the controller instance. You can invoke it by:

var result = engine.Operations.Call(action, 1, 2);

The definitive reference is the DLR hosting specification.

Tags:
FileSizeDateAttached by 
 dlr-spec-hosting.pdf
Draft DLR Hosting Specification
1280.84 kB17:50, 23 Jun 2008jflamActions
Images (0)
 
Comments (7)
Viewing 7 of 7 comments: view all
This doesn't work (why am I not suprised!
Posted 13:31, 23 Jun 2008
Updated the sample as per StephenDenisEdwards' comment. Thanks!
Posted 16:02, 23 Jun 2008
It's worthwhile noting this does not work off the shelf in some situations. As long as the IronRuby libraries are not in the GAC by default (perhaps through installation process) you might need to explicitly drag them into your processes address space.
In my latest project, I had to issue code like

var runtime = IronRuby.Ruby.CreateRuntime();
try {
var foo = Ruby.Runtime.Protocols.Normalize(10);
} catch {}

otherwise I got really bad problems
Posted 19:55, 13 Jul 2008
This code worked fine for me, but the real gem here is the DLR Hosting Specification... Answered so many questions I had.
Posted 19:31, 12 Aug 2008
The Microsoft.Scipting.dll distributed with the 0.5 release doesn't appear to have a ScriptRuntime.Create() method. I can only find a CreateFromConfiguration() and CreateRemote() methods. Where is the Create() method?
Posted 15:24, 26 Jun 2009
I also found the above code confusing, but I've used IronPython script hosting before and IronRuby hosting works the same way, I discovered (at least in v 0.9) Here's how I got a valid script engine... I have all the Microsoft.Scripting and IronRuby assemblies referenced in a VS2008 Console app project. Then add these usings: using IronRuby; using Microsoft.Scripting.Hosting; then my Main() method does this: ScriptEngine scriptEngine = Ruby.CreateEngine(); One line of code to get a working Ruby script engine.
Posted 05:27, 13 Sep 2009
I'm experimenting with both IronRuby and IronPython in my .NET 3.5 app. IronPython loads fine. With IronRuby, I get:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Failed to load language 'IronRuby': Could not load file or assembly 'IronRuby.Libraries, Version=0.9.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. ---> IronRuby.Builtins.LoadError: Could not load file or assembly 'IronRuby.Libraries, Version=0.9.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. ---> System.IO.FileNotFoundException: Could not load file or assembly 'IronRuby.Libraries, Version=0.9.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. File name: 'IronRuby.Libraries, Version=0.9.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' at System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection) at System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection) at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) at System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) at System.Reflection.Assembly.Load(String assemblyString) at Microsoft.Scripting.PlatformAdaptationLayer.LoadAssembly(String name) at IronRuby.Runtime.Loader.LoadBuiltins()

I'm referencing all of DLLs from the IronRuby distribution:
IronRuby
IronRuby.Libraries
IronRuby.Libraries.YAML
Microsoft.Dynamic
Microsoft.Scripting
Microsoft.Scripting.Core
Microsoft.Debugging
Microsoft.Scripting.ExtensionAttribute

Am I missing some other 3rd party library? Or is there some other kind of set up such as an environment variable I need to set up? edited 16:12, 11 Nov 2009

Posted 16:11, 11 Nov 2009
Viewing 7 of 7 comments: view all
You must login to post a comment.

 
SourceForge.net