Execute ScriptCS from c# application -
background
i'm creating c# application runs status checks (think nagios style checks).
what ideally want c# application @ specific directory, , compile/execute scriptcs scripts within it, , act on results (send email alerts failing status checks example).
i expect script return integer or (for example) , integer indicate weather status check succeeded or failed.
values returned c# application.
0 - success 1 - warning 2 - error
when first tasked thought job mef, vastly more convenient create these 'status checks' without having create project or compile anything, plopping scriptcs script within folder seems way more attractive.
so questions are
is there documentation/samples on using c# app , scriptcs (google didn't net me much)?
is use case scriptcs or never intended used this?
would have easier time creating custom solution using roslyn or dynamic compilation/execution? (i have little experience scriptsc)
i found easy examples on how this:
loading script file on disk , running it: https://github.com/glennblock/scriptcs-hosting-example
a web site can submit code, , return result: https://github.com/filipw/sample-scriptcs-webhost
here example of how load script file, run , return result:
public dynamic runscript() { var logger = logmanager.getlogger(system.reflection.methodbase.getcurrentmethod().declaringtype); var scriptservicesbuilder = new scriptservicesbuilder(new scriptconsole(), logger). loglevel(loglevel.info).cache(false).repl(false).scriptengine<roslynscriptengine>(); var scriptcs = scriptservicesbuilder.build(); scriptcs.executor.initialize(new[] { "system.web" }, enumerable.empty<iscriptpack>()); scriptcs.executor.addreferences(assembly.getexecutingassembly()); var result = scriptcs.executor.execute("helloworld.csx"); scriptcs.executor.terminate(); if (result.compileexceptioninfo != null) return new { error = result.compileexceptioninfo.sourceexception.message }; if (result.executeexceptioninfo != null) return new { error = result.executeexceptioninfo.sourceexception.message }; return result.returnvalue; }
Comments
Post a Comment