Esempio n. 1
0
 private bool Cancel(ScriptRunnerInfo runner)
 {
     if (runner != null && runner.ScriptDomain != null)
     {
         var domain = runner.ScriptDomain;
         runner.ScriptDomain = null;
         LocalCache.RemoveScriptRunnerInfo(runner.ScriptId);
         AppDomain.Unload(domain);
         return(true);
     }
     return(false);
 }
Esempio n. 2
0
        public static void SetScriptRunnerInfo(this ICacheClient cache, string scriptId, ScriptRunnerInfo info)
        {
            var cacheKey = GetScriptCacheKey(scriptId);

            cache.Set(cacheKey, info);
        }
        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
            });
        }