public static bool RunCs(string[] args) { var precompilationArgs = PrecompilationCommandLineParser.Parse(args, Directory.GetCurrentDirectory()); CompilationProxy proxy = null; AppDomain compilationDomain = null; try { var currentSetup = AppDomain.CurrentDomain.SetupInformation; var setup = new AppDomainSetup() { ApplicationName = currentSetup.ApplicationName, ApplicationBase = currentSetup.ApplicationBase, ConfigurationFile = precompilationArgs.AppConfig, }; if (setup.ConfigurationFile == null) { setup.ConfigurationFile = new[] { "app.config", "web.config" }.Select(x => Path.Combine(precompilationArgs.BaseDirectory, x)).FirstOrDefault(File.Exists); if (!string.IsNullOrWhiteSpace(setup.ConfigurationFile)) { Console.WriteLine("WARNING: '" + setup.ConfigurationFile + "' used as fallback config file"); } } compilationDomain = AppDomain.CreateDomain( AppDomainHelper.CsCompilationAppDomainName, AppDomain.CurrentDomain.Evidence, setup); compilationDomain.UnhandledException += (s, e) => { Console.WriteLine("error: " + e); }; var assemblyExt = new HashSet <string> { ".dll", ".exe" }; var references = precompilationArgs.References; //.Concat(Directory.EnumerateFiles(AppDomain.CurrentDomain.BaseDirectory).Where(r => assemblyExt.Contains(Path.GetExtension(r)))).ToArray() CompilationAssemblyResolver.Register(compilationDomain, references); proxy = (CompilationProxy)compilationDomain.CreateInstanceAndUnwrap( typeof(CompilationProxy).Assembly.FullName, typeof(CompilationProxy).FullName); Console.CancelKeyPress += (s, e) => proxy?.ForceStop(); return(proxy.RunCs(precompilationArgs)); } finally { proxy?.ForceStop(); // runtime has exited, finish off by unloading the runtime appdomain if (compilationDomain != null) { AppDomain.Unload(compilationDomain); } } }
public static bool RunCs(string[] args) { var precompilationArgs = PrecompilationCommandLineParser.Parse(args, Directory.GetCurrentDirectory()); AppDomain compilationDomain = null; try { var currentSetup = AppDomain.CurrentDomain.SetupInformation; var setup = new AppDomainSetup { ApplicationName = currentSetup.ApplicationName, ApplicationBase = currentSetup.ApplicationBase, ConfigurationFile = precompilationArgs.AppConfig, }; if (setup.ConfigurationFile == null) { setup.ConfigurationFile = new[] { "app.config", "web.config" }.Select(x => Path.Combine(precompilationArgs.BaseDirectory, x)).FirstOrDefault(File.Exists); if (!string.IsNullOrWhiteSpace(setup.ConfigurationFile)) { Console.WriteLine("WARNING: '" + setup.ConfigurationFile + "' used as fallback config file"); } } compilationDomain = AppDomain.CreateDomain( AppDomainHelper.CsCompilationAppDomainName, AppDomain.CurrentDomain.Evidence, setup); var proxy = (CompilationProxy)compilationDomain.CreateInstanceAndUnwrap( typeof(CompilationProxy).Assembly.FullName, typeof(CompilationProxy).FullName); proxy.SetConsoleIo(Console.In, Console.Out, Console.Error); // we need to make sure referenced assemblies get loaded before we touch any roslyn or asp.net mvc types proxy.HookAssemblyReslove(precompilationArgs.References); return(proxy.RunCs(precompilationArgs)); } finally { // runtime has exited, finish off by unloading the runtime appdomain if (compilationDomain != null) { AppDomain.Unload(compilationDomain); } } }