private string ConvertToReplyMessage(ReplFiddleExecuteResponse replFiddleExecuteResponse) { string replyMesage = String.Empty; bool hasResultValue = (replFiddleExecuteResponse.ReturnValue != null); bool hasConsoleOutput = (replFiddleExecuteResponse.ConsoleOutput != null); bool hasExceptionErrorMessage = (replFiddleExecuteResponse.ExceptionErrorMessage != null); bool hasDebugInfo = (replFiddleExecuteResponse.DebugInfo != null); if (hasResultValue && !hasConsoleOutput) { replyMesage = replFiddleExecuteResponse.ReturnValue; } if (hasResultValue && hasConsoleOutput) { replyMesage = replFiddleExecuteResponse.ReturnValue; replyMesage += Environment.NewLine + Environment.NewLine; replyMesage += "[Console] " + replFiddleExecuteResponse.ConsoleOutput; } if (!hasResultValue && hasConsoleOutput) { replyMesage = replFiddleExecuteResponse.ConsoleOutput; } if (hasExceptionErrorMessage) { replyMesage = "[Exception] " + replFiddleExecuteResponse.ExceptionErrorMessage; } if (!hasResultValue && !hasConsoleOutput && !hasExceptionErrorMessage) { replyMesage = GetEmptyReplyPlaceholder(); } if (hasDebugInfo) { replyMesage += Environment.NewLine + Environment.NewLine; replyMesage += "[Debug] "; replyMesage += Environment.NewLine + Environment.NewLine; replyMesage += replFiddleExecuteResponse.DebugInfo; } return(replyMesage); }
public async Task <ReplFiddleExecuteResponse> ExecuteFiddleAsync(ReplFiddleExecuteRequest request) { var response = new ReplFiddleExecuteResponse(); string previousCode = String.Join(Environment.NewLine, request.PreviousCodeBlocks); var code = previousCode + Environment.NewLine + request.CodeBlock; //Execute all code var apiRequest = new FiddleExecuteRequest() { Compiler = Compiler.Roslyn, Language = Language.CSharp, ProjectType = ProjectType.Script, CodeBlock = code }; DotNetFiddleApiClient apiCLient = new DotNetFiddleApiClient(); var apiResponse = await apiCLient.ExecuteFiddleAsync(apiRequest); string apiError = null; if (apiResponse.StatusCode != System.Net.HttpStatusCode.OK) { response.ExceptionErrorMessage = "There was an error calling .NET Fiddle API."; //Send error to errorlog or apiError += "Failed to execute API request. Here is an answer from API" + Environment.NewLine; apiError += "Response Code: " + apiResponse.StatusCode + Environment.NewLine; apiError += "Response Body: " + apiResponse.Content + Environment.NewLine; } else { if (apiResponse.Data.HasErrors || apiResponse.Data.HasCompilationErrors) { response.ExceptionErrorMessage = apiResponse.Data.ConsoleOutput; } else { var consoleOutput = apiResponse.Data.ConsoleOutput; var codeEntryConsoleOutput = consoleOutput.Substring(request.LastConsoleOutputLength); //Account for line break response.LastConsoleOutputLength = consoleOutput.Length; //Remove line break and other white spaces if in the beginning. Since DNF trims output. //Can fix it more later if it becomes an issue response.ConsoleOutput = codeEntryConsoleOutput.TrimStart(); } } //If worked, compare output to previous output. Only show new lines, changes from last output if (request.IsDebugEnabled) { response.DebugInfo = "Input: " + code + Environment.NewLine + Environment.NewLine + "Output: " + Environment.NewLine + Environment.NewLine + JsonConvert.SerializeObject(apiResponse); if (apiError != null) { response.DebugInfo += Environment.NewLine + Environment.NewLine + "API Error: " + Environment.NewLine + apiError; } } return(response); }
public async Task <ReplFiddleExecuteResponse> ExecuteFiddleAsync(ReplFiddleExecuteRequest request) { var response = new ReplFiddleExecuteResponse(); _scriptState = null; List <string> codeBlocks = new List <string>(); codeBlocks.AddRange(request.PreviousCodeBlocks); codeBlocks.Add(request.CodeBlock); for (int i = 0; i < codeBlocks.Count; i++) { bool isLastCodeEntry = i == codeBlocks.Count - 1; TextWriter oldConsoleOutput = null; TextWriter newConsoleOutput = null; if (isLastCodeEntry) { lock (_lockLastEntryExecution) { oldConsoleOutput = Console.Out; newConsoleOutput = new StringWriter(); Console.SetOut(newConsoleOutput); try { try { response.ReturnValue = RunCodeEntry(codeBlocks[i]).Result; } catch (Exception ex) { //User inner exception because async method throws AggregateException response.ExceptionErrorMessage = ex.InnerException.Message; } string consoleOutputText = newConsoleOutput.ToString(); if (!String.IsNullOrEmpty(consoleOutputText)) { response.ConsoleOutput = consoleOutputText.Trim(); } } finally { Console.SetOut(oldConsoleOutput); } } } else { try { //The return values for all but last code entry are not used string someReturnValue = await RunCodeEntry(codeBlocks[i]); } catch (Exception e) { Debug.WriteLine(e.Message); } } } if (request.IsDebugEnabled) { response.DebugInfo = "Input: " + request.CodeBlock + Environment.NewLine + Environment.NewLine + "Output: " + Environment.NewLine + Environment.NewLine + JsonConvert.SerializeObject(response); } return(response); }