예제 #1
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);
        }
        public void Handler(object sender, object eventArgs)
        {
            // No function: nothing to do
            if (_f == null)
            {
                return;
            }

            EngineHelper.CallMethod(_provider.GetScriptEngine(), _f, _virtualPath, new object[] { sender, eventArgs });
        }
예제 #3
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;
        }