[Ironpython-users] Debugging with CLR in Web App

John Davidson jwdavidson at gmail.com
Wed Feb 13 15:37:59 CET 2013


I am in the process of creating an MVC 4 based wiki with scripting
support for dynamic creation of content. I have chosen IronPython as
the embedded scripting host and am developing a DSL for the scripting
portion. This DSL is created in C# and exposed to the CLR for
scripting using IronPython.

The code I have works correctly when run without debugging. The
process for normal execution is w3wp.exe run from VS2012. It also
works correctly, with or without debugging, when run as a unit test.

The code for script engine creation and execution is shown below:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using IronPython.Hosting;
using IronPython.Runtime;
using Microsoft.Scripting.Hosting;

namespace LynxWikiScripting
{
    public class WikiScriptEngine
    {
        private static readonly Lazy<WikiScriptEngine> scriptEngine =
new Lazy<WikiScriptEngine>(() => { return new WikiScriptEngine(); },
true);

        private WikiScriptEngine() {}

        public static ScriptEngine Engine = Python.CreateEngine(new
Dictionary<string, object>() {{ "Debug", true }} );
        public static ScriptRuntime Runtime = Engine.Runtime;
        public static ScriptScope scope = Engine.CreateScope();


        public static void Initialize()
        {
            string fullPath = Assembly.GetExecutingAssembly().Location;
            string rootDir = Directory.GetParent(fullPath).FullName;

        }

        public static string ExecuteBehavior(string scriptInput)
        {
            StringBuilder scriptSource = new StringBuilder();
            scriptSource.AppendLine("import clr");
            scriptSource.AppendLine(@"clr.AddReferenceToFileAndPath(r'C:\Users\John\Documents\Visual
Studio 2012\Projects\LynxWiki\LynxWiki\App_Code\IronWiki.dll')");
            scriptSource.AppendLine("import IronWiki");
            scriptSource.AppendLine("from IronWiki import UtilityProperties");
            scriptSource.AppendLine("from IronWiki import IronWikiSyntax");
            scriptSource.Append(scriptInput);
            try {
                ScriptSource source =
Engine.CreateScriptSourceFromString(scriptSource.ToString(),
Microsoft.Scripting.SourceCodeKind.Statements);
                source.Execute(scope);
                var varPyNow = String.Empty;
                scope.TryGetVariable<string>("ScriptOutput", out varPyNow);
                return varPyNow;
            }
            catch (Exception ex) {
                Debug.Print(ex.Message);
                return ex.Message;
            }
        }
    }
}

The unit test code is:

using System;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using LynxWikiScripting;

namespace LynxWikiScripting.Tests
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestIronWikiSyntax()
        {
            WikiScriptEngine.Initialize();
            StringBuilder scriptSource = new StringBuilder();
            scriptSource.AppendLine();
            scriptSource.AppendLine("WikiSyntax = IronWikiSyntax()");
            scriptSource.AppendLine("so = ''");
            scriptSource.AppendLine("for iwc in WikiSyntax.IronWikiClasses :");
            scriptSource.AppendLine("    so = so + '||' + iwc.Name +
'||' + iwc.Description + '''||\\r\\n'''");
            scriptSource.AppendLine("ScriptOutput = so");
            //scriptSource.AppendLine("");
            //scriptSource.AppendLine("");

            string result =
WikiScriptEngine.ExecuteBehavior(scriptSource.ToString());

            Assert.IsNotNull(result);
        }
    }
}

The string returned by the processing of the IronPython script is:

||IronWikiSyntax||A class containing information about the IronWiki syntax||
||IronWikiClass||The class describing an IronWiki class||
||IronWikiMember||The class describing an IronWiki member||
||UtilityProperties||A class that provides a number of utility properties||

This is a wiki table text showing the result of reflecting the
contents of the DSL at its inception.

The application code has identical inputs to ExecuteBehavior as the
unit test has. I have tried Python.CreateEngine with or without the
"debug" option and also with and without the "trace" option. There is
no difference in execution behavior based on having or not having the
options included.

Is there something else I am missing, or can I give more information
to help resolve my problem.

Thanks

John Davidson


More information about the Ironpython-users mailing list