Exemplo n.º 1
0
        private static void ReloadChangedScriptFiles()
        {
            // Nothing to do if no files changed
            if (!s_appScriptDirChanged)
            {
                return;
            }

            // TODO: a new ScriptRuntime should be created instead
            foreach (string path in Directory.GetFiles(s_scriptFolder))
            {
                string ext = Path.GetExtension(path);
                if (IsDLRLanguageExtension(ext))
                {
                    try {
                        s_scriptEnvironment.ExecuteFile(path);
                    } catch (SyntaxErrorException e) {
                        EngineHelper.ThrowSyntaxErrorException(e);
                    }
                }
            }

            // Clear out the flag to mark that all the changes were processed
            s_appScriptDirChanged = false;
        }
Exemplo n.º 2
0
        internal static BuildResult GetBuildResult(string virtualPath, IBuildProvider buildProvider)
        {
            // If any script files in App_Scripts changed, they need to be reloaded
            ReloadChangedScriptFiles();

            virtualPath = VirtualPathUtility.ToAbsolute(virtualPath);
            string cacheKey = virtualPath.ToLowerInvariant();

            // Look up the cache before and after taking the lock
            BuildResult result = (BuildResult)HttpRuntime.Cache[cacheKey];

            if (result != null)
            {
                return(result);
            }

            ScriptEngine scriptEngine = buildProvider.GetScriptEngine();

            lock (typeof(EngineHelper)) {
                result = (BuildResult)HttpRuntime.Cache[cacheKey];
                if (result != null)
                {
                    return(result);
                }

                DateTime utcStart = DateTime.UtcNow;

                CompiledCode compiledCode = null;
                string       scriptCode   = buildProvider.GetScriptCode();

                if (scriptCode != null)
                {
                    // We pass the physical path for debugging purpose
                    string       physicalPath = HostingEnvironment.MapPath(virtualPath);
                    ScriptSource source       = scriptEngine.CreateScriptSourceFromString(scriptCode, physicalPath, SourceCodeKind.File);

                    try {
                        compiledCode = source.Compile();
                    } catch (SyntaxErrorException e) {
                        EngineHelper.ThrowSyntaxErrorException(e);
                    }
                }

                // Note that we cache the result even if there is no script, to avoid having to check
                // again later.

                result = buildProvider.CreateBuildResult(compiledCode, virtualPath);

                CacheDependency cacheDependency = HostingEnvironment.VirtualPathProvider.GetCacheDependency(
                    virtualPath, new Util.SingleObjectCollection(virtualPath), utcStart);

                // Cache the result with a 5 minute sliding expiration
                HttpRuntime.Cache.Insert(cacheKey, result, cacheDependency,
                                         Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(5));
            }

            return(result);
        }
Exemplo n.º 3
0
 internal static void ExecuteCode(ScriptScope scope, CompiledCode compiledCode, string virtualPath)
 {
     try {
         compiledCode.Execute(scope);
     } catch (SyntaxErrorException e) {
         EngineHelper.ThrowSyntaxErrorException(e);
     } catch (Exception e) {
         if (!EngineHelper.ProcessRuntimeException(compiledCode.Engine, e, virtualPath))
         {
             throw;
         }
     }
 }