Set first unity configuration to instantiate the scripting engine:
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="IScriptCache" type="ScriptEngine.Interface.IScriptCache, ScriptEngine.Interface"/>
<alias alias="ScriptCache" type="ScriptEngine.ScriptCache, ScriptEngine"/>
<alias alias="AppFabricScriptCache" type="ScriptEngine.AppFabricScriptCache, ScriptEngine"/>
<alias alias="PythonEngineOptions" type="ScriptEngine.PythonEngineOptions, ScriptEngine"/>
<namespace name="ScriptEngine" />
<namespace name="ScriptEngine.Interface" />
<assembly name="ScriptEngine" />
<assembly name="ScriptEngine.Interface" />
<container>
<register type="IScriptCache" mapTo="ScriptCache"></register>
<register type="PythonEngineOptions">
<property name="Debug" value="true"></property>
<property name="SourceCodePath" value="C:\Codeplex TFS\PythonEngine\ScriptEngine.Python\Code.py"></property>
</register>
</container>
</unity>
don't forget the connection string:
<connectionStrings>
<add name="ScriptEngineModelContainer" connectionString="metadata=res://*/ScriptEngineModel.csdl|res://*/ScriptEngineModel.ssdl|res://*/ScriptEngineModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.;Initial Catalog=PythonEngine;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
then is it possible to save a script (after a syntax check) :
if (PythonEngine.Instance.CheckSyntax(txtCode.Text, new ScriptContext { }, new string[] { "System" }, ref message))
{
ScriptDataSource sds = new ScriptDataSource();
sds.Create(new Script { ScriptCode = txtCode.Text });
}
this is a sample testing script:
class person:
name = ''
surname = ''
p = person()
p.name = 'John'
p.surname = 'Doe'
return 'Hello ' + p.name + ' ' + p.surname + '!'
and then execute the pre-compiled script:
Script script = new Script{Id=IdScript};
script = new ScriptDataSource().Read(script);
try
{
object result = PythonEngine.Instance.Evaluate(script.ScriptCode, new ScriptContext { }, new string[] { "System" });
lblResult.Text = result.ToString();
}
catch (Exception x)
{
...
}