private State Initialize()
        {
            var files = new C1DirectoryInfo(_templatesDirectory)
                           .GetFiles(LayoutFileMask, SearchOption.AllDirectories)
                           .Where(f => !f.Name.StartsWith(TempFilePrefix, StringComparison.Ordinal));

            var templates = new List<PageTemplateDescriptor>();
            var templateRenderingData = new Hashtable<Guid, TemplateRenderingInfo>();
            var loadingExceptions = new Hashtable<Guid, Exception>();
            var sharedFiles = new List<SharedFile>();

            // Loading and compiling layout controls
            foreach (var fileInfo in files)
            {
                string filePath = fileInfo.FullName;
                string virtualPath = ConvertToVirtualPath(filePath);

                CachedTemplateInformation cachedTemplateInformation;

                if (_templateCache.Get(virtualPath, filePath, out cachedTemplateInformation))
                {
                    if (cachedTemplateInformation == null)
                    {
                        sharedFiles.Add(new SharedRazorFile(virtualPath));
                        continue;
                    }

                    Guid templateId = cachedTemplateInformation.TemplateId;

                    templates.Add(new LazyInitializedRazorPageTemplateDescriptor(
                        virtualPath,
                        templateId,
                        cachedTemplateInformation.Title,
                        this));

                    Verify.That(!templateRenderingData.ContainsKey(templateId), "Multiple master page templates defined with the same ID '{0}'", templateId);

                    templateRenderingData.Add(templateId, new LazyInitializedTemplateRenderingInfo(virtualPath, this));
                    continue;
                }

                WebPageBase webPage;
                PageTemplateDescriptor parsedTemplate;
                IDictionary<string, PropertyInfo> placeholderProperties;
                Exception loadingException;

                if (!LoadRazorTemplate(virtualPath, out webPage, out parsedTemplate, out placeholderProperties, out loadingException))
                {
                    var brokenTemplate = GetIncorrectlyLoadedPageTemplate(virtualPath, loadingException);

                    loadingExceptions.Add(brokenTemplate.Id, brokenTemplate.LoadingException);
                    templates.Add(brokenTemplate);
                    continue;
                }

                if (!(webPage is RazorPageTemplate))
                {
                    sharedFiles.Add(new SharedRazorFile(virtualPath));

                    if (!HostingEnvironment.ApplicationHost.ShutdownInitiated())
                    {
                        _templateCache.Add(virtualPath, filePath, null);
                    }
                    continue;
                }

                templates.Add(parsedTemplate);

                templateRenderingData.Add(parsedTemplate.Id, new TemplateRenderingInfo(virtualPath, placeholderProperties));

                _templateCache.Add(virtualPath, filePath, new CachedTemplateInformation(parsedTemplate));
            }

            return new State
                       {
                           Templates = templates,
                           RenderingInfo = templateRenderingData,
                           SharedFiles = sharedFiles,
                           LoadingExceptions = loadingExceptions
                       };
        }
        private State Initialize()
        {
            var files = new C1DirectoryInfo(_templatesDirectory)
                           .GetFiles(MasterPageFileMask, SearchOption.AllDirectories)
                           .Where(f => !f.Name.StartsWith(TempFilePrefix, StringComparison.Ordinal));

            var templates = new List<PageTemplateDescriptor>();
            var templateRenderingData = new Hashtable<Guid, MasterPageRenderingInfo>();
            var loadingExceptions = new Hashtable<Guid, Exception>();
            var sharedSourceFiles = new List<SharedFile>();

            // Loading and compiling layout controls
            foreach (var fileInfo in files)
            {
                string filePath = fileInfo.FullName;
                string virtualPath = ConvertToVirtualPath(filePath);
                string codeBehindFilePath = GetCodebehindFilePath(filePath);

                string[] cacheRelatedFiles = { filePath, codeBehindFilePath };

                CachedTemplateInformation cachedTemplateInformation;

                if (_templateCache.Get(virtualPath, cacheRelatedFiles, out cachedTemplateInformation))
                {
                    if (cachedTemplateInformation == null)
                    {
                        sharedSourceFiles.Add(new SharedMasterPage(virtualPath));
                        continue;
                    }

                    Guid templateId = cachedTemplateInformation.TemplateId;

                    templates.Add(new LazyInitializedMasterPagePageTemplateDescriptor(
                        filePath,
                        codeBehindFilePath,
                        templateId,
                        cachedTemplateInformation.Title,
                        this));

                    Verify.That(!templateRenderingData.ContainsKey(templateId), "Multiple master page templates defined with the same ID '{0}'", templateId);

                    templateRenderingData.Add(templateId, new LazyInitializedMasterPageRenderingInfo(filePath, virtualPath, this));
                    continue;
                }

                MasterPage masterPage;
                MasterPagePageTemplateDescriptor parsedPageTemplateDescriptor;
                MasterPageRenderingInfo renderingInfo;
                Exception loadingException;

                if (!LoadMasterPage(filePath, out masterPage, out parsedPageTemplateDescriptor, out renderingInfo, out loadingException))
                {
                    var brokenTemplate = GetIncorrectlyLoadedPageTemplate(filePath, loadingException);

                    loadingExceptions.Add(brokenTemplate.Id, brokenTemplate.LoadingException);
                    templates.Add(brokenTemplate);
                    continue;
                }

                if (masterPage == null)
                {
                    continue;
                }

                if (!(masterPage is MasterPagePageTemplate))
                {
                    sharedSourceFiles.Add(new SharedMasterPage(virtualPath));

                    if (!HostingEnvironment.ApplicationHost.ShutdownInitiated())
                    {
                        _templateCache.Add(virtualPath, cacheRelatedFiles, null);
                    }

                    continue;
                }

                templates.Add(parsedPageTemplateDescriptor);

                Verify.That(!templateRenderingData.ContainsKey(parsedPageTemplateDescriptor.Id),
                    "Multiple master page templates defined with the same ID '{0}'", parsedPageTemplateDescriptor.Id);

                templateRenderingData.Add(parsedPageTemplateDescriptor.Id, renderingInfo);

                _templateCache.Add(virtualPath, cacheRelatedFiles, new CachedTemplateInformation(parsedPageTemplateDescriptor));
            }

            return new State {
                Templates = templates,
                RenderingInfo = templateRenderingData,
                LoadingExceptions = loadingExceptions,
                SharedSourceFiles = sharedSourceFiles
            };
        }