#r "System.Collections" | |
#r "System.Runtime" | |
#r "System.Reflection" | |
#r "System.Threading.Tasks" | |
#r "System.Net.Http" | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Reflection; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json; | |
using Microsoft.CodeAnalysis.Scripting; | |
using Microsoft.CodeAnalysis.CSharp.Scripting; | |
using MyExtensions; | |
private static readonly string[] DefaultImports = | |
{ | |
"System", | |
"System.IO", | |
"System.Linq", | |
"System.Collections.Generic", | |
"System.Text", | |
"System.Text.RegularExpressions", | |
"System.Net", | |
"System.Net.Http", | |
"System.Threading", | |
"System.Threading.Tasks", | |
"MyExtensions", | |
}; | |
private static readonly Assembly[] DefaultReferences = | |
{ | |
typeof(Enumerable).Assembly, | |
typeof(List<string>).Assembly, | |
typeof(System.Net.Http.HttpClient).Assembly, | |
typeof(EnumerableExtensions).Assembly, | |
}; | |
public static async Task<string> EvaluateCSharpAsync(string code) | |
{ | |
object result = null; | |
try | |
{ | |
result = await CSharpScript.EvaluateAsync(code ?? "コードが空ですよ?", | |
ScriptOptions.Default | |
.WithImports(DefaultImports) | |
.WithReferences(new [] { | |
"System", | |
"System.Core", | |
"System.Xml", | |
"System.Xml.Linq", | |
}) | |
.WithReferences(DefaultReferences)); | |
} | |
catch (Exception ex) | |
{ | |
result = $"{ex.Message}{Environment.NewLine}{ex.StackTrace}"; | |
} | |
var resultText = result?.ToString() ?? ""; | |
return resultText; | |
} |
#load "..\CSharpScripting.csx" | |
#r "MyExtensions.dll" | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json; | |
using MyExtensions; | |
public static async Task<object> Run(HttpRequestMessage req, TraceWriter log) | |
{ | |
log.Verbose("Charp Compiler service Webhook was triggered!"); | |
string jsonContent = await req.Content.ReadAsStringAsync(); | |
dynamic data = JsonConvert.DeserializeObject(jsonContent); | |
if (data.code == null) | |
{ | |
return req.CreateResponse(HttpStatusCode.BadRequest, new { | |
error = "missing <code> property." | |
}); | |
} | |
// CSharp Code評価 | |
string code = data.code; | |
log.Verbose($"{nameof(code)} : {code}"); | |
var resultText = await EvaluateCSharpAsync(code); | |
log.Verbose(resultText); | |
return req.CreateResponse(HttpStatusCode.OK, new { | |
body = resultText, | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment