public object Any(RunScript request) { if (request.ScriptId == null) { throw new ArgumentException("ScriptId"); } AppData.AssertNoIllegalTokens(request.MainSource); AppData.AssertNoIllegalTokens(request.Sources.ToArray()); var result = new ScriptExecutionResult(); var existingUserScripts = GetExistingActiveUserScripts(); var runnerInfo = LocalCache.GetScriptRunnerInfo(request.ScriptId); //stop script if run if (runnerInfo?.ScriptDomain != null) { var scriptStatus = runnerInfo.DomainWrapper.GetScriptStatus(); if (request.ForceRun || scriptStatus != ScriptStatus.PrepareToRun && scriptStatus != ScriptStatus.Running) { Cancel(runnerInfo); } else { result.Status = ScriptStatus.AnotherScriptExecuting; return(new RunScriptResponse { Result = result }); } } List <AssemblyReference> normalizedReferences; var addedReferences = AddReferencesFromPackages(request.References, request.PackagesConfig, out normalizedReferences); var evidence = new Evidence(AppDomain.CurrentDomain.Evidence); var setup = new AppDomainSetup { PrivateBinPath = Path.Combine(VirtualFiles.RootDirectory.RealPath, "bin"), ApplicationBase = VirtualFiles.RootDirectory.RealPath }; var domain = AppDomain.CreateDomain(Guid.NewGuid().ToString(), evidence, setup); var asm = typeof(DomainWrapper).Assembly.FullName; var type = typeof(DomainWrapper).FullName; var wrapper = (DomainWrapper)domain.CreateInstanceAndUnwrap(asm, type); wrapper.ScriptId = request.ScriptId; var writerProxy = new NotifierProxy(ServerEvents, request.ScriptId); result = wrapper.RunAsync(request.MainSource, request.Sources, addedReferences.Select(r => r.Path).ToList(), writerProxy); LocalCache.SetScriptRunnerInfo(request.ScriptId, new ScriptRunnerInfo { ScriptId = request.ScriptId, SessionId = base.Request.GetPermanentSessionId(), CreatedDate = DateTime.UtcNow, ScriptDomain = domain, DomainWrapper = wrapper, }); var unloadOldUserScripts = existingUserScripts .Where(x => x.ScriptId != request.ScriptId && DateTime.UtcNow - x.CreatedDate > TimeSpan.FromMinutes(1)); var scriptsRemoved = UnloadExistingScripts(unloadOldUserScripts); return(new RunScriptResponse { Result = result, References = normalizedReferences, ScriptsRemoved = scriptsRemoved, }); }
public object Any(RunEmbedScript request) { if (request.GistHash == null) { throw new ArgumentException("GistHash"); } AppData.AssertNoIllegalTokens(request.MainSource); AppData.AssertNoIllegalTokens(request.Sources.ToArray()); var result = new EmbedScriptExecutionResult(); var codeHash = GetSourceCodeHash(request); if (!request.NoCache) { var mr = AppData.GetMemoizedResult(codeHash); if (mr != null) { result = mr.Result; return(new RunEmbedScriptResponse { Result = result }); } } List <AssemblyReference> normalizedReferences; var addedReferences = AddReferencesFromPackages(request.References, request.Packages, out normalizedReferences); //Create domain and run script var evidence = new Evidence(AppDomain.CurrentDomain.Evidence); var setup = new AppDomainSetup { PrivateBinPath = Path.Combine(VirtualFiles.RootDirectory.RealPath, "bin"), ApplicationBase = VirtualFiles.RootDirectory.RealPath }; var domain = AppDomain.CreateDomain(Guid.NewGuid().ToString(), evidence, setup); var asm = typeof(DomainWrapper).Assembly.FullName; var type = typeof(DomainWrapper).FullName; var wrapper = (DomainWrapper)domain.CreateInstanceAndUnwrap(asm, type); wrapper.ScriptId = request.ScriptId; var writerProxy = new NotifierProxy(ServerEvents, request.ScriptId); var info = new ScriptRunnerInfo { ScriptId = request.ScriptId, ScriptDomain = domain, DomainWrapper = wrapper }; Cache.Set(request.ScriptId, info); var lockEvt = new ManualResetEvent(false); ThreadPool.QueueUserWorkItem(_ => { try { var sr = wrapper.Run(request.MainSource, request.Sources, addedReferences.Select(r => r.Path).ToList(), writerProxy); result.Exception = sr.Exception; result.Errors = sr.Errors; //get json of last variable if (sr.Variables != null && sr.Variables.Count > 0) { var value = wrapper.GetVariableValue(sr.Variables[sr.Variables.Count - 1].Name); result.LastVariableJson = ScriptUtils.ToJson(value); } } finally { lockEvt.Set(); } }); lockEvt.WaitOne(); AppData.SetMemoizedResult(new MemoizedResult { CodeHash = codeHash, Result = result }); //Unload appdomain only in synchroneous version //AppDomain.Unload(domain); return(new RunEmbedScriptResponse { Result = result, References = normalizedReferences }); }