public static CompilerResults Compile(string language, string key, string file, ArrayList assemblies, bool debug)
{
Cache cache = HttpRuntime.InternalCache;
CompilerResults results = (CompilerResults)cache [cachePrefix + key];
if (results != null)
{
return(results);
}
if (!Directory.Exists(dynamicBase))
{
Directory.CreateDirectory(dynamicBase);
}
object ticket;
bool acquired = AcquireCompilationTicket(cachePrefix + key, out ticket);
try
{
Monitor.Enter(ticket);
results = (CompilerResults)cache [cachePrefix + key];
if (results != null)
{
return(results);
}
CodeDomProvider provider = null;
int warningLevel;
string compilerOptions;
string tempdir;
provider = BaseCompiler.CreateProvider(language, out compilerOptions, out warningLevel, out tempdir);
if (provider == null)
{
throw new HttpException("Configuration error. Language not supported: " +
language, 500);
}
CodeDomProvider compiler = provider;
CompilerParameters options = GetOptions(assemblies);
options.IncludeDebugInformation = debug;
options.WarningLevel = warningLevel;
options.CompilerOptions = compilerOptions;
TempFileCollection tempcoll = new TempFileCollection(tempdir, true);
string dllfilename = Path.GetFileName(tempcoll.AddExtension("dll", true));
options.OutputAssembly = Path.Combine(dynamicBase, dllfilename);
results = compiler.CompileAssemblyFromFile(options, file);
ArrayList realdeps = new ArrayList(assemblies.Count + 1);
realdeps.Add(file);
for (int i = assemblies.Count - 1; i >= 0; i--)
{
string current = (string)assemblies [i];
if (Path.IsPathRooted(current))
{
realdeps.Add(current);
}
}
string [] deps = (string [])realdeps.ToArray(typeof(string));
cache.Insert(cachePrefix + key, results, new CacheDependency(deps));
}
finally
{
Monitor.Exit(ticket);
if (acquired)
{
ReleaseCompilationTicket(cachePrefix + key);
}
}
return(results);
}