void BuildDefaultAssembly(List <string> files, CodeCompileUnit unit) { AssemblyBuilder abuilder = new AssemblyBuilder(Provider); if (unit != null) { abuilder.AddCodeCompileUnit(unit); } CompilerParameters cp = ci.CreateDefaultCompilerParameters(); cp.OutputAssembly = baseAssemblyPath; cp.GenerateExecutable = false; cp.TreatWarningsAsErrors = true; cp.IncludeDebugInformation = config.Debug; foreach (string f in files) { cp.EmbeddedResources.Add(f); } CompilerResults results = abuilder.BuildAssembly(cp); if (results == null) { return; } if (results.NativeCompilerReturnValue == 0) { mainAssembly = results.CompiledAssembly; BuildManager.TopLevelAssemblies.Add(mainAssembly); } else { if (HttpContext.Current.IsCustomErrorEnabled) { throw new ApplicationException("An error occurred while compiling global resources."); } throw new CompilationException(null, results.Errors, null); } HttpRuntime.WritePreservationFile(mainAssembly, canonicAssemblyName); HttpRuntime.EnableAssemblyMapping(true); }
// Build and add the assembly to the BuildManager's // CodeAssemblies collection public void Build(string[] binAssemblies) { Type compilerProvider = null; CompilerInfo compilerInfo = null, cit; string extension, language, cpfile = null; List <string> knownfiles = new List <string>(); List <string> unknownfiles = new List <string>(); // First make sure all the files are in the same // language bool known = false; foreach (string f in files) { known = true; language = null; extension = Path.GetExtension(f); if (String.IsNullOrEmpty(extension) || !CodeDomProvider.IsDefinedExtension(extension)) { known = false; } if (known) { language = CodeDomProvider.GetLanguageFromExtension(extension); if (!CodeDomProvider.IsDefinedLanguage(language)) { known = false; } } if (!known || language == null) { unknownfiles.Add(f); continue; } cit = CodeDomProvider.GetCompilerInfo(language); if (cit == null || !cit.IsCodeDomProviderTypeValid) { continue; } if (compilerProvider == null) { cpfile = f; compilerProvider = cit.CodeDomProviderType; compilerInfo = cit; } else if (compilerProvider != cit.CodeDomProviderType) { throw new HttpException( String.Format( "Files {0} and {1} are in different languages - they cannot be compiled into the same assembly", Path.GetFileName(cpfile), Path.GetFileName(f))); } knownfiles.Add(f); } CodeDomProvider provider = null; CompilationSection compilationSection = WebConfigurationManager.GetWebApplicationSection("system.web/compilation") as CompilationSection; if (compilerInfo == null) { if (!CodeDomProvider.IsDefinedLanguage(compilationSection.DefaultLanguage)) { throw new HttpException("Failed to retrieve default source language"); } compilerInfo = CodeDomProvider.GetCompilerInfo(compilationSection.DefaultLanguage); if (compilerInfo == null || !compilerInfo.IsCodeDomProviderTypeValid) { throw new HttpException("Internal error while initializing application"); } } provider = compilerInfo.CreateProvider(); if (provider == null) { throw new HttpException("A code provider error occurred while initializing application."); } AssemblyBuilder abuilder = new AssemblyBuilder(provider); foreach (string file in knownfiles) { abuilder.AddCodeFile(file); } foreach (CodeCompileUnit unit in units) { abuilder.AddCodeCompileUnit(unit); } BuildProvider bprovider; CompilerParameters parameters = compilerInfo.CreateDefaultCompilerParameters(); parameters.IncludeDebugInformation = compilationSection.Debug; if (binAssemblies != null && binAssemblies.Length > 0) { StringCollection parmRefAsm = parameters.ReferencedAssemblies; foreach (string binAsm in binAssemblies) { if (parmRefAsm.Contains(binAsm)) { continue; } parmRefAsm.Add(binAsm); } } if (compilationSection != null) { foreach (AssemblyInfo ai in compilationSection.Assemblies) { if (ai.Assembly != "*") { try { parameters.ReferencedAssemblies.Add( AssemblyPathResolver.GetAssemblyPath(ai.Assembly)); } catch (Exception ex) { throw new HttpException( String.Format("Could not find assembly {0}.", ai.Assembly), ex); } } } BuildProviderCollection buildProviders = compilationSection.BuildProviders; foreach (string file in unknownfiles) { bprovider = GetBuildProviderFor(file, buildProviders); if (bprovider == null) { continue; } bprovider.GenerateCode(abuilder); } } if (knownfiles.Count == 0 && unknownfiles.Count == 0 && units.Count == 0) { return; } outputAssemblyName = (string)FileUtils.CreateTemporaryFile( AppDomain.CurrentDomain.SetupInformation.DynamicBase, name, "dll", OnCreateTemporaryAssemblyFile); parameters.OutputAssembly = outputAssemblyName; foreach (Assembly a in BuildManager.TopLevelAssemblies) { parameters.ReferencedAssemblies.Add(a.Location); } CompilerResults results = abuilder.BuildAssembly(parameters); if (results == null) { return; } if (results.NativeCompilerReturnValue == 0) { BuildManager.CodeAssemblies.Add(results.CompiledAssembly); BuildManager.TopLevelAssemblies.Add(results.CompiledAssembly); HttpRuntime.WritePreservationFile(results.CompiledAssembly, name); } else { if (HttpContext.Current.IsCustomErrorEnabled) { throw new HttpException("An error occurred while initializing application."); } throw new CompilationException(null, results.Errors, null); } }
// Build and add the assembly to the BuildManager's // CodeAssemblies collection public void Build (string[] binAssemblies) { Type compilerProvider = null; CompilerInfo compilerInfo = null, cit; string extension, language, cpfile = null; List<string> knownfiles = new List<string>(); List<string> unknownfiles = new List<string>(); // First make sure all the files are in the same // language bool known = false; foreach (string f in files) { known = true; language = null; extension = Path.GetExtension (f); if (String.IsNullOrEmpty (extension) || !CodeDomProvider.IsDefinedExtension (extension)) known = false; if (known) { language = CodeDomProvider.GetLanguageFromExtension(extension); if (!CodeDomProvider.IsDefinedLanguage (language)) known = false; } if (!known || language == null) { unknownfiles.Add (f); continue; } cit = CodeDomProvider.GetCompilerInfo (language); if (cit == null || !cit.IsCodeDomProviderTypeValid) continue; if (compilerProvider == null) { cpfile = f; compilerProvider = cit.CodeDomProviderType; compilerInfo = cit; } else if (compilerProvider != cit.CodeDomProviderType) throw new HttpException ( String.Format ( "Files {0} and {1} are in different languages - they cannot be compiled into the same assembly", Path.GetFileName (cpfile), Path.GetFileName (f))); knownfiles.Add (f); } CodeDomProvider provider = null; CompilationSection compilationSection = WebConfigurationManager.GetWebApplicationSection ("system.web/compilation") as CompilationSection; if (compilerInfo == null) { if (!CodeDomProvider.IsDefinedLanguage (compilationSection.DefaultLanguage)) throw new HttpException ("Failed to retrieve default source language"); compilerInfo = CodeDomProvider.GetCompilerInfo (compilationSection.DefaultLanguage); if (compilerInfo == null || !compilerInfo.IsCodeDomProviderTypeValid) throw new HttpException ("Internal error while initializing application"); } provider = compilerInfo.CreateProvider (); if (provider == null) throw new HttpException ("A code provider error occurred while initializing application."); AssemblyBuilder abuilder = new AssemblyBuilder (provider); foreach (string file in knownfiles) abuilder.AddCodeFile (file); foreach (CodeCompileUnit unit in units) abuilder.AddCodeCompileUnit (unit); BuildProvider bprovider; CompilerParameters parameters = compilerInfo.CreateDefaultCompilerParameters (); parameters.IncludeDebugInformation = compilationSection.Debug; if (binAssemblies != null && binAssemblies.Length > 0) parameters.ReferencedAssemblies.AddRange (binAssemblies); if (compilationSection != null) { foreach (AssemblyInfo ai in compilationSection.Assemblies) if (ai.Assembly != "*") { try { parameters.ReferencedAssemblies.Add ( AssemblyPathResolver.GetAssemblyPath (ai.Assembly)); } catch (Exception ex) { throw new HttpException ( String.Format ("Could not find assembly {0}.", ai.Assembly), ex); } } BuildProviderCollection buildProviders = compilationSection.BuildProviders; foreach (string file in unknownfiles) { bprovider = GetBuildProviderFor (file, buildProviders); if (bprovider == null) continue; bprovider.GenerateCode (abuilder); } } if (knownfiles.Count == 0 && unknownfiles.Count == 0 && units.Count == 0) return; outputAssemblyName = (string)FileUtils.CreateTemporaryFile ( AppDomain.CurrentDomain.SetupInformation.DynamicBase, name, "dll", OnCreateTemporaryAssemblyFile); parameters.OutputAssembly = outputAssemblyName; foreach (Assembly a in BuildManager.TopLevelAssemblies) parameters.ReferencedAssemblies.Add (a.Location); CompilerResults results = abuilder.BuildAssembly (parameters); if (results == null) return; if (results.NativeCompilerReturnValue == 0) { BuildManager.CodeAssemblies.Add (results.CompiledAssembly); BuildManager.TopLevelAssemblies.Add (results.CompiledAssembly); HttpRuntime.WritePreservationFile (results.CompiledAssembly, name); } else { if (HttpContext.Current.IsCustomErrorEnabled) throw new HttpException ("An error occurred while initializing application."); throw new CompilationException (null, results.Errors, null); } }
void BuildDefaultAssembly (List <string> files, CodeCompileUnit unit) { AssemblyBuilder abuilder = new AssemblyBuilder (Provider); if (unit != null) abuilder.AddCodeCompileUnit (unit); CompilerParameters cp = ci.CreateDefaultCompilerParameters (); cp.OutputAssembly = baseAssemblyPath; cp.GenerateExecutable = false; cp.TreatWarningsAsErrors = true; cp.IncludeDebugInformation = config.Debug; foreach (string f in files) cp.EmbeddedResources.Add (f); CompilerResults results = abuilder.BuildAssembly (cp); if (results == null) return; if (results.NativeCompilerReturnValue == 0) { mainAssembly = results.CompiledAssembly; BuildManager.TopLevelAssemblies.Add (mainAssembly); } else { if (HttpContext.Current.IsCustomErrorEnabled) throw new ApplicationException ("An error occurred while compiling global resources."); throw new CompilationException (null, results.Errors, null); } HttpRuntime.WritePreservationFile (mainAssembly, canonicAssemblyName); HttpRuntime.EnableAssemblyMapping (true); }
public void Compile() { string refsPath = Path.Combine(HttpRuntime.AppDomainAppPath, ResourcesDirName); if (!Directory.Exists(refsPath)) { return; } string[] files = Directory.GetFiles(refsPath, "*.wsdl", SearchOption.AllDirectories); if (files == null || files.Length == 0) { return; } CompilationSection cs = WebConfigurationManager.GetWebApplicationSection("system.web/compilation") as CompilationSection; if (cs == null) { throw new HttpException("Unable to determine default compilation language."); } CompilerType ct = BuildManager.GetDefaultCompilerTypeForLanguage(cs.DefaultLanguage, cs); CodeDomProvider codeDomProvider = null; Exception codeDomException = null; try { codeDomProvider = Activator.CreateInstance(ct.CodeDomProviderType) as CodeDomProvider; } catch (Exception e) { codeDomException = e; } if (codeDomProvider == null) { throw new HttpException("Unable to instantiate default compilation language provider.", codeDomException); } AssemblyBuilder ab = new AssemblyBuilder(codeDomProvider, "App_WebReferences_"); ab.CompilerOptions = ct.CompilerParameters; VirtualPath vp; WsdlBuildProvider wbp; foreach (string file in files) { vp = VirtualPath.PhysicalToVirtual(file); if (vp == null) { continue; } wbp = new WsdlBuildProvider(); wbp.SetVirtualPath(vp); wbp.GenerateCode(ab); } CompilerResults results; try { results = ab.BuildAssembly(); } catch (CompilationException ex) { throw new HttpException("Failed to compile web references.", ex); } if (results == null) { return; } Assembly asm = results.CompiledAssembly; BuildManager.TopLevelAssemblies.Add(asm); }
static void GenerateAssembly (AssemblyBuilder abuilder, BuildProviderGroup group, VirtualPath vp, bool debug) { IDictionary <string, bool> deps; BuildManagerCacheItem bmci; string bvp, vpabsolute = vp.Absolute; StringBuilder sb; string newline; int failedCount = 0; if (debug) { newline = Environment.NewLine; sb = new StringBuilder ("Code generation for certain virtual paths in a batch failed. Those files have been removed from the batch." + newline); sb.Append ("Since you're running in debug mode, here's some more information about the error:" + newline); } else { newline = null; sb = null; } List <BuildProvider> failedBuildProviders = null; StringComparison stringComparison = RuntimeHelpers.StringComparison; foreach (BuildProvider bp in group) { bvp = bp.VirtualPath; if (HasCachedItemNoLock (bvp)) continue; try { bp.GenerateCode (abuilder); } catch (Exception ex) { if (String.Compare (bvp, vpabsolute, stringComparison) == 0) { if (ex is CompilationException || ex is ParseException) throw; throw new HttpException ("Code generation failed.", ex); } if (failedBuildProviders == null) failedBuildProviders = new List <BuildProvider> (); failedBuildProviders.Add (bp); failedCount++; if (sb != null) { if (failedCount > 1) sb.Append (newline); sb.AppendFormat ("Failed file virtual path: {0}; Exception: {1}{2}{1}", bp.VirtualPath, newline, ex); } continue; } deps = bp.ExtractDependencies (); if (deps != null) { foreach (var dep in deps) { bmci = GetCachedItemNoLock (dep.Key); if (bmci == null || bmci.BuiltAssembly == null) continue; abuilder.AddAssemblyReference (bmci.BuiltAssembly); } } } if (sb != null && failedCount > 0) ShowDebugModeMessage (sb.ToString ()); if (failedBuildProviders != null) { foreach (BuildProvider bp in failedBuildProviders) group.Remove (bp); } foreach (Assembly asm in referencedAssemblies) { if (asm == null) continue; abuilder.AddAssemblyReference (asm); } CompilerResults results = abuilder.BuildAssembly (vp); // No results is not an error - it is possible that the assembly builder contained only .asmx and // .ashx files which had no body, just the directive. In such case, no code unit or code file is added // to the assembly builder and, in effect, no assembly is produced but there are STILL types that need // to be added to the cache. Assembly compiledAssembly = results != null ? results.CompiledAssembly : null; try { buildCacheLock.EnterWriteLock (); if (compiledAssembly != null) referencedAssemblies.Add (compiledAssembly); foreach (BuildProvider bp in group) { if (HasCachedItemNoLock (bp.VirtualPath)) continue; StoreInCache (bp, compiledAssembly, results); } } finally { buildCacheLock.ExitWriteLock (); } }
Assembly GetAssemblyFromSource (string vpath) { vpath = UrlUtils.Combine (BaseVirtualDir, vpath); string realPath = MapPath (vpath, false); if (!File.Exists (realPath)) ThrowParseException ("File " + vpath + " not found"); AddSourceDependency (vpath); CompilerResults result; string tmp; CompilerParameters parameters; CodeDomProvider provider = BaseCompiler.CreateProvider (HttpContext.Current, language, out parameters, out tmp); if (provider == null) throw new HttpException ("Cannot find provider for language '" + language + "'."); AssemblyBuilder abuilder = new AssemblyBuilder (provider); abuilder.CompilerOptions = parameters; abuilder.AddAssemblyReference (BuildManager.GetReferencedAssemblies () as List <Assembly>); abuilder.AddCodeFile (realPath); result = abuilder.BuildAssembly (new VirtualPath (vpath)); if (result.NativeCompilerReturnValue != 0) { using (StreamReader reader = new StreamReader (realPath)) { throw new CompilationException (realPath, result.Errors, reader.ReadToEnd ()); } } AddAssembly (result.CompiledAssembly, true); return result.CompiledAssembly; }
public void Compile () { string refsPath = Path.Combine (HttpRuntime.AppDomainAppPath, ResourcesDirName); if (!Directory.Exists (refsPath)) return; string[] files = Directory.GetFiles (refsPath, "*.wsdl", SearchOption.AllDirectories); if (files == null || files.Length == 0) return; CompilationSection cs = WebConfigurationManager.GetWebApplicationSection ("system.web/compilation") as CompilationSection; if (cs == null) throw new HttpException ("Unable to determine default compilation language."); CompilerType ct = BuildManager.GetDefaultCompilerTypeForLanguage (cs.DefaultLanguage, cs); CodeDomProvider codeDomProvider = null; Exception codeDomException = null; try { codeDomProvider = Activator.CreateInstance (ct.CodeDomProviderType) as CodeDomProvider; } catch (Exception e) { codeDomException = e; } if (codeDomProvider == null) throw new HttpException ("Unable to instantiate default compilation language provider.", codeDomException); AssemblyBuilder ab = new AssemblyBuilder (codeDomProvider, "App_WebReferences_"); ab.CompilerOptions = ct.CompilerParameters; VirtualPath vp; WsdlBuildProvider wbp; foreach (string file in files) { vp = VirtualPath.PhysicalToVirtual (file); if (vp == null) continue; wbp = new WsdlBuildProvider (); wbp.SetVirtualPath (vp); wbp.GenerateCode (ab); } CompilerResults results; try { results = ab.BuildAssembly (); } catch (CompilationException ex) { throw new HttpException ("Failed to compile web references.", ex); } if (results == null) return; Assembly asm = results.CompiledAssembly; BuildManager.TopLevelAssemblies.Add (asm); }