Beispiel #1
0
        protected override void RunInteractive(RunOptsBase opts, RunResult result)
        {
            new PermissionSet(PermissionState.Unrestricted).Assert();

            CommonScriptEngine scriptEngine = this.CreateSciptEngine();
            Session            session      = scriptEngine.CreateSession();
            var codeBlock      = (ConsoleOrScriptCodeBlock)opts.CodeBlock;
            var referencedDlls = this.GetGacDlls(codeBlock.CodeBlock);

            foreach (var referenceDll in referencedDlls)
            {
                session.AddReference(referenceDll);
            }

            var libs = GetNonGacDlls();

            foreach (var path in libs)
            {
                session.AddReference(path);
            }

            Submission <object> submission;

            try
            {
                // we compile code there
                submission = session.CompileSubmission <object>(codeBlock.CodeBlock);
            }
            catch (ThreadAbortException)
            {
                throw;
            }
            catch (Exception ex)
            {
                if (!string.IsNullOrEmpty(ex.Message) && ex.Message.Contains(ErrorWhenCompileConsoleProjectAsScript))
                {                /*Case 4067: DotNetFiddle throws exception on VbNet Script
                                  * https://entech.fogbugz.com/default.asp?4067#31607
                                  * This issue occurs, when user is trying to compile VB.Net Console project as VB.Net Script project.
                                  * So, there is main entry point 'Module Sub Main' in snippet.
                                  * Then Roslyn throws following exception "(3) : error BC35000: Requested operation is not available because the runtime library function 'Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute..ctor' is not defined."
                                  * In same case for C#, Roslyn just ignores 'console' code.
                                  * So for VB.Net case we just return 'success' and empty string.
                                  */
                    result.IsSuccess     = true;
                    result.ConsoleOutput = "";
                }
                else
                {
                    ValidateCodeResult validateCodeResult = ValidateCode(codeBlock.CodeBlock);

                    result.IsSuccess      = false;
                    result.FailureType    = RunResultFailureType.CompilerErrors;
                    result.CompilerErrors = validateCodeResult.Errors;
                }

                if (result.CompilerErrors == null)
                {
                    result.CompilerErrors = new List <ValidationError> {
                        ValidationError.CreateFromException(ex)
                    };
                }

                TryCleanRoslynCacheHack();
                PermissionSet.RevertAssert();
                return;
            }


            object execResult = null;

            try
            {
                this.OnStartingExecution();

                PermissionSet.RevertAssert();

                execResult = submission.Execute();

                this.OnFinishedExecution();
            }
            catch (ThreadAbortException)
            {
                throw;
            }
            catch (Exception ex)
            {
                result.IsSuccess        = false;
                result.FailureType      = RunResultFailureType.RunTimeException;
                result.RunTimeException = new ExceptionInfo(ex);
            }
            finally
            {
                result.ConsoleOutput = _consoleWriter.ToString().TrimEnd();

                if (execResult != null)
                {
                    result.ConsoleOutput += Environment.NewLine;
                    result.ConsoleOutput += "[Return value]: " + execResult;
                }

                // don't need it as we modified Roslyn assemblies and made fix in them
                // TryCleanRoslynCacheHack();
            }
        }