Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute ScriptCS from c# application

Tags:

c#

scriptcs

Background

I'm creating a c# application that runs some status checks (think nagios style checks).

What I ideally want is this c# application to look at a specific directory, and just compile/execute any scriptcs scripts within it, and act on the results (send email alerts for failing status checks for example).

I would expect the script to return an integer or something (for example) and that integer would indicate weather the status check succeeded or failed.

Values returned back to C# application.

0 - Success
1 - Warning
2 - Error

When first tasked with this I thought this is a job for MEF, but it would be vastly more convenient to create these 'status checks' without having to create a project or compile anything, just plopping a scriptcs script within a folder seems way more attractive.

So my questions are

  1. Is there any documentation/samples on using a c# app and scriptcs together (google didn't net me much)?

  2. Is this a use case for scriptcs or was it never really intended to be used like this?

  3. Would I have an easier time just creating a custom solution using roslyn or some dynamic compilation/execution? (I have very little experience with scriptsc)

like image 913
Kyle Gobel Avatar asked Dec 05 '14 19:12

Kyle Gobel


People also ask

How do I run C sharp code in Visual Studio?

To do so, you can double-click or tap on the . csproj file in Windows File Explorer, or choose Open a project in Visual Studio, browse to find the . csproj file, and select the file.

How do I run a .CS file?

cs file is normally C# source code. You can open it in any text editor. If you want to actually run the program defined by the source code, you will need to compile it, for example by loading it into Visual Studio (the real Visual Studio, not Visual Studio Code) and building it and running it.

Can you make scripts with C#?

C# Scripting (hereafter, CS-Script) lets you write and execute individual lines of C# code without having to define classes or namespaces. This statement, all by itself, is a perfectly good CS-Script script (and, in the right environment, will even display "Hello, World"): Console.

Where is Scriptcs installed?

Installing scriptcs Chocolatey will install scriptcs to %LOCALAPPDATA%\scriptcs\ and update your PATH accordingly. Note: You may need to restart your command prompt after the installation completes.


1 Answers

I found some good easy examples on how to do this:

Loading a script from a file on disk and running it: https://github.com/glennblock/scriptcs-hosting-example

A web site where you can submit code, and it will return the result: https://github.com/filipw/sample-scriptcs-webhost

Here is an example of how to load a script file, run it and return the 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;
}
like image 140
jmc Avatar answered Oct 01 '22 10:10

jmc