2
0

WebServiceCompiler.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // System.Web.Compilation.WebServiceCompiler
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
  8. // Copyright (c) 2040 Novell, Inc. (http://www.novell.com)
  9. //
  10. using System;
  11. using System.CodeDom.Compiler;
  12. using System.IO;
  13. using System.Web.Configuration;
  14. using System.Web.UI;
  15. using System.Reflection;
  16. namespace System.Web.Compilation
  17. {
  18. class WebServiceCompiler : BaseCompiler
  19. {
  20. SimpleWebHandlerParser parser;
  21. ICodeCompiler compiler;
  22. CodeDomProvider provider;
  23. CompilerParameters compilerParameters;
  24. string inputFile;
  25. public WebServiceCompiler (SimpleWebHandlerParser wService)
  26. : base (null)
  27. {
  28. this.parser = wService;
  29. }
  30. public static Type CompileIntoType (SimpleWebHandlerParser wService)
  31. {
  32. WebServiceCompiler wsc = new WebServiceCompiler (wService);
  33. return wsc.GetCompiledType ();
  34. }
  35. public override Type GetCompiledType ()
  36. {
  37. if (parser.Program.Trim () == "")
  38. return parser.GetTypeFromBin (parser.ClassName);
  39. CompilerResults results = (CompilerResults) HttpRuntime.Cache [parser.PhysicalPath];
  40. if (results != null) {
  41. Assembly a = results.CompiledAssembly;
  42. if (a != null)
  43. return a.GetType (parser.ClassName, true);
  44. }
  45. string lang = parser.Language;
  46. CompilationConfiguration config;
  47. config = CompilationConfiguration.GetInstance (parser.Context);
  48. provider = config.GetProvider (lang);
  49. if (provider == null)
  50. throw new HttpException ("Configuration error. Language not supported: " +
  51. lang, 500);
  52. compiler = provider.CreateCompiler ();
  53. compilerParameters = CachingCompiler.GetOptions (parser.Assemblies);
  54. compilerParameters.IncludeDebugInformation = parser.Debug;
  55. compilerParameters.CompilerOptions = config.GetCompilerOptions (lang);
  56. compilerParameters.WarningLevel = config.GetWarningLevel (lang);
  57. bool keepFiles = (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") != null);
  58. TempFileCollection tempcoll;
  59. tempcoll = new TempFileCollection (config.TempDirectory, keepFiles);
  60. compilerParameters.TempFiles = tempcoll;
  61. inputFile = tempcoll.AddExtension (provider.FileExtension);
  62. Stream st = File.OpenWrite (inputFile);
  63. StreamWriter sw = new StreamWriter (st);
  64. sw.WriteLine (parser.Program);
  65. sw.Close ();
  66. string dllfilename = tempcoll.AddExtension ("dll", true);
  67. if (!Directory.Exists (dynamicBase))
  68. Directory.CreateDirectory (dynamicBase);
  69. compilerParameters.OutputAssembly = Path.Combine (dynamicBase, dllfilename);
  70. results = CachingCompiler.Compile (this);
  71. CheckCompilerErrors (results);
  72. if (results.CompiledAssembly == null)
  73. throw new CompilationException (inputFile, results.Errors,
  74. "No assembly returned after compilation!?");
  75. return results.CompiledAssembly.GetType (parser.ClassName, true);
  76. }
  77. void CheckCompilerErrors (CompilerResults results)
  78. {
  79. if (results.NativeCompilerReturnValue == 0)
  80. return;
  81. throw new CompilationException (parser.PhysicalPath, results.Errors, parser.Program);
  82. }
  83. internal new SimpleWebHandlerParser Parser {
  84. get { return parser; }
  85. }
  86. internal override ICodeCompiler Compiler {
  87. get { return compiler; }
  88. }
  89. internal CompilerParameters CompilerOptions {
  90. get { return compilerParameters; }
  91. }
  92. internal string InputFile {
  93. get { return inputFile; }
  94. }
  95. }
  96. }