Exemplo n.º 1
0
        private string GetSourceCodeHash(RunEmbedScript request)
        {
            string hash;
            var    code = (request.MainSource ?? string.Empty)
                          + (request.Packages ?? string.Empty) + string.Concat(request.Sources);

            using (var md5 = MD5.Create())
            {
                var hashBytes = md5.ComputeHash(Encoding.Unicode.GetBytes(code));
                var sb        = new StringBuilder();
                foreach (var b in hashBytes)
                {
                    sb.Append(b.ToString("x2"));
                }
                hash = sb.ToString();
            }

            return(hash);
        }
        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
            });
        }