示例#1
0
        internal override TextWriter CreateCodeFile(System.Web.Compilation.BuildProvider buildProvider, out string filename)
        {
            string cacheKeyFromVirtualPath = BuildManager.GetCacheKeyFromVirtualPath(buildProvider.VirtualPathObject);

            cacheKeyFromVirtualPath = FileUtil.TruncatePathIfNeeded(Path.Combine(this._generatedFilesDir, cacheKeyFromVirtualPath), 10) + "." + base._codeProvider.FileExtension;
            filename = cacheKeyFromVirtualPath;
            BuildManager.GenerateFileTable[buildProvider.VirtualPathObject.VirtualPathStringNoTrailingSlash] = cacheKeyFromVirtualPath;
            return(new StreamWriter(new FileStream(cacheKeyFromVirtualPath, FileMode.Create, FileAccess.Write, FileShare.Read), Encoding.UTF8));
        }
        internal CodeCompileUnit GenerateCodeCompileUnit(
            VirtualPath virtualPath, string virtualFileString, out Type codeDomProviderType,
            out CompilerParameters compilerParameters, out IDictionary linePragmasTable)
        {
            // Add a pending call to make sure our thread doesn't get killed
            AddPendingCall();

            try {
                BuildManager.SkipTopLevelCompilationExceptions = true;
                _buildManager.EnsureTopLevelFilesCompiled();

                // Get the virtual file content so that we can use the correct hash code.
                if (virtualFileString == null)
                {
                    using (Stream stream = virtualPath.OpenFile()) {
                        TextReader reader = Util.ReaderFromStream(stream, virtualPath);
                        virtualFileString = reader.ReadToEnd();
                    }
                }

                _virtualPathProvider.RegisterVirtualFile(virtualPath, virtualFileString);

                string cacheKey = BuildManager.GetCacheKeyFromVirtualPath(virtualPath) + "_CBMResult";
                BuildResultCodeCompileUnit result = (BuildResultCodeCompileUnit)BuildManager.GetBuildResultFromCache(cacheKey, virtualPath);

                if (result == null)
                {
                    lock (_lock) {
                        // Don't need to check the result again since it's very unlikely in CBM scenarios.
                        DateTime utcStart = DateTime.UtcNow;

                        BuildProvider internalBuildProvider = GetCompilerParamsAndBuildProvider(
                            virtualPath, out codeDomProviderType, out compilerParameters);

                        // This is the no-compile case
                        if (internalBuildProvider == null)
                        {
                            linePragmasTable = null;
                            return(null);
                        }

                        CodeCompileUnit ccu = internalBuildProvider.GetCodeCompileUnit(out linePragmasTable);

                        result             = new BuildResultCodeCompileUnit(codeDomProviderType, ccu, compilerParameters, linePragmasTable);
                        result.VirtualPath = virtualPath;
                        result.SetCacheKey(cacheKey);

                        FixupReferencedAssemblies(virtualPath, compilerParameters);

                        // CodeCompileUnit could be null, do not try to fix referenced assemblies.
                        // This happens for example when an .asmx file does not contain any code.
                        if (ccu != null)
                        {
                            // VSWhidbey 501260 Add all the referenced assemblies to the CodeCompileUnit
                            // in case the CodeDom provider needs them for code generation
                            foreach (String assemblyString in compilerParameters.ReferencedAssemblies)
                            {
                                ccu.ReferencedAssemblies.Add(assemblyString);
                            }
                        }

                        // Add all the dependencies, so that the ccu gets cached correctly (VSWhidbey 275091)
                        ICollection dependencies = internalBuildProvider.VirtualPathDependencies;
                        if (dependencies != null)
                        {
                            result.AddVirtualPathDependencies(dependencies);
                        }

                        BuildManager.CacheBuildResult(cacheKey, result, utcStart);

                        return(ccu);
                    }
                }

                codeDomProviderType = result.CodeDomProviderType;
                compilerParameters  = result.CompilerParameters;
                linePragmasTable    = result.LinePragmasTable;

                FixupReferencedAssemblies(virtualPath, compilerParameters);

                return(result.CodeCompileUnit);
            }
            finally {
                if (virtualFileString != null)
                {
                    _virtualPathProvider.RevertVirtualFile(virtualPath);
                }
                BuildManager.SkipTopLevelCompilationExceptions = false;

                RemovePendingCall();
            }
        }
        internal CodeCompileUnit GenerateCodeCompileUnit(VirtualPath virtualPath, string virtualFileString, out Type codeDomProviderType, out CompilerParameters compilerParameters, out IDictionary linePragmasTable)
        {
            CodeCompileUnit codeCompileUnit;

            this.AddPendingCall();
            try
            {
                BuildManager.SkipTopLevelCompilationExceptions = true;
                this._buildManager.EnsureTopLevelFilesCompiled();
                if (virtualFileString == null)
                {
                    using (Stream stream = virtualPath.OpenFile())
                    {
                        virtualFileString = Util.ReaderFromStream(stream, virtualPath).ReadToEnd();
                    }
                }
                this._virtualPathProvider.RegisterVirtualFile(virtualPath, virtualFileString);
                string cacheKey = BuildManager.GetCacheKeyFromVirtualPath(virtualPath) + "_CBMResult";
                BuildResultCodeCompileUnit buildResultFromCache = (BuildResultCodeCompileUnit)BuildManager.GetBuildResultFromCache(cacheKey, virtualPath);
                if (buildResultFromCache == null)
                {
                    lock (this._lock)
                    {
                        DateTime utcNow = DateTime.UtcNow;
                        System.Web.Compilation.BuildProvider provider = this.GetCompilerParamsAndBuildProvider(virtualPath, out codeDomProviderType, out compilerParameters);
                        if (provider == null)
                        {
                            linePragmasTable = null;
                            return(null);
                        }
                        CodeCompileUnit unit2 = provider.GetCodeCompileUnit(out linePragmasTable);
                        buildResultFromCache = new BuildResultCodeCompileUnit(codeDomProviderType, unit2, compilerParameters, linePragmasTable)
                        {
                            VirtualPath = virtualPath
                        };
                        buildResultFromCache.SetCacheKey(cacheKey);
                        this.FixupReferencedAssemblies(virtualPath, compilerParameters);
                        if (unit2 != null)
                        {
                            foreach (string str2 in compilerParameters.ReferencedAssemblies)
                            {
                                unit2.ReferencedAssemblies.Add(str2);
                            }
                        }
                        ICollection virtualPathDependencies = provider.VirtualPathDependencies;
                        if (virtualPathDependencies != null)
                        {
                            buildResultFromCache.AddVirtualPathDependencies(virtualPathDependencies);
                        }
                        BuildManager.CacheBuildResult(cacheKey, buildResultFromCache, utcNow);
                        return(unit2);
                    }
                }
                codeDomProviderType = buildResultFromCache.CodeDomProviderType;
                compilerParameters  = buildResultFromCache.CompilerParameters;
                linePragmasTable    = buildResultFromCache.LinePragmasTable;
                this.FixupReferencedAssemblies(virtualPath, compilerParameters);
                codeCompileUnit = buildResultFromCache.CodeCompileUnit;
            }
            finally
            {
                if (virtualFileString != null)
                {
                    this._virtualPathProvider.RevertVirtualFile(virtualPath);
                }
                BuildManager.SkipTopLevelCompilationExceptions = false;
                this.RemovePendingCall();
            }
            return(codeCompileUnit);
        }