AnalyzeViews() public method

public AnalyzeViews ( System.Web.Hosting.VirtualPathProvider vpp, System.Web.HttpContextBase httpContext, IEnumerable sources ) : IEnumerable
vpp System.Web.Hosting.VirtualPathProvider
httpContext System.Web.HttpContextBase
sources IEnumerable
return IEnumerable
コード例 #1
0
        public IEnumerable <TemplateDefinition> GetTemplates(Type contentType)
        {
            var httpContext = httpContextProvider.Get();

            if (httpContext == null)
            {
                return(new TemplateDefinition[0]);
            }

            try
            {
                httpContext.Request.GetType();
            }
            catch (Exception)
            {
                return(new TemplateDefinition[0]);
            }

            const string cacheKey    = "RazorDefinitions";
            var          definitions = httpContext.Cache[cacheKey] as IEnumerable <ItemDefinition>;

            lock (this)
            {
                if (definitions == null || rebuild)
                {
                    DequeueRegistrations();

                    var vpp          = vppProvider.Get();
                    var descriptions = analyzer.AnalyzeViews(vpp, httpContext, sources).ToList();
                    definitions = BuildDefinitions(descriptions);

                    var files = descriptions.SelectMany(p => p.TouchedPaths).Distinct().ToList();
                    //var dirs = files.Select(f => f.Substring(0, f.LastIndexOf('/'))).Distinct();
                    var cacheDependency = vpp.GetCacheDependency(files.FirstOrDefault(), files, DateTime.UtcNow);

                    httpContext.Cache.Remove(cacheKey);
                    httpContext.Cache.Add(cacheKey, definitions, cacheDependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.AboveNormal, new CacheItemRemovedCallback(delegate
                    {
                        logger.Debug("Razor template changed");
                    }));
                    rebuild = false;
                }
            }

            var templates = definitions.Where(d => d.ItemType == contentType).Select(d =>
            {
                var td             = new TemplateDefinition();
                td.Definition      = d;
                td.Description     = d.Description;
                td.Name            = d.TemplateKey;
                td.OriginalFactory = () => null;
                td.TemplateFactory = () => activator.CreateInstance(d.ItemType, null, d.TemplateKey);
                td.TemplateUrl     = null;
                td.Title           = d.Title;
                td.ReplaceDefault  = "Index".Equals(d.TemplateKey, StringComparison.InvariantCultureIgnoreCase);
                return(td);
            }).ToArray();

            return(templates);
        }
コード例 #2
0
        public IEnumerable <TemplateDefinition> GetTemplates(Type contentType)
        {
            var httpContext = httpContextProvider.Get();

            if (httpContext == null)
            {
                logger.Warn("Trying to get templates with no context");
                return(Enumerable.Empty <TemplateDefinition>());
            }

            try
            {
                httpContext.Request.GetType();
            }
            catch (Exception ex)
            {
                logger.Warn("Trying to get templates with invalid context", ex);
                return(Enumerable.Empty <TemplateDefinition>());
            }

            const string cacheKey    = "RazorDefinitions";
            var          definitions = httpContext.Cache[cacheKey] as IEnumerable <ItemDefinition>;

            lock (this)
            {
                if (definitions == null || rebuild)
                {
                    if (registrator.QueuedRegistrations.Count > 0)
                    {
                        logger.DebugFormat("Dequeuing {0} registrations", registrator.QueuedRegistrations.Count);
                        DequeueRegistrations();
                    }
                    var vpp          = vppProvider.Get();
                    var descriptions = analyzer.AnalyzeViews(vpp, httpContext, sources).ToList();
                    if (descriptions.Count > 0)
                    {
                        logger.DebugFormat("Got {0} descriptions", descriptions.Count);
                        definitions = BuildDefinitions(descriptions);
                        logger.Debug("Built definitions");

                        var files = descriptions.SelectMany(p => p.Context.TouchedPaths).Distinct().ToList();
                        if (files.Count > 0)
                        {
                            logger.DebugFormat("Setting up cache dependency on {0} files", files.Count);
                            var cacheDependency = vpp.GetCacheDependency(files.FirstOrDefault(), files, DateTime.UtcNow);

                            httpContext.Cache.Remove(cacheKey);
                            httpContext.Cache.Add(cacheKey, definitions, cacheDependency, Cache.NoAbsoluteExpiration,
                                                  Cache.NoSlidingExpiration, CacheItemPriority.AboveNormal,
                                                  delegate { logger.Debug("Razor template changed"); });
                        }
                    }
                    rebuild = false;
                }
            }

            if (definitions == null)
            {
                return(Enumerable.Empty <TemplateDefinition>());
            }

            var templates = definitions.Where(d => d.ItemType == contentType).Select(d =>
            {
                var td = new TemplateDefinition
                {
                    Definition      = d,
                    Description     = d.Description,
                    Name            = d.TemplateKey,
                    OriginalFactory = () => null,
                    TemplateFactory = () => activator.CreateInstance(d.ItemType, null, d.TemplateKey),
                    TemplateUrl     = null,
                    Title           = d.Title,
                    ReplaceDefault  = "Index".Equals(d.TemplateKey, StringComparison.InvariantCultureIgnoreCase)
                };
                return(td);
            }).ToArray();

            return(templates);
        }