WebServiceCompiler.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. //
  9. using System;
  10. using System.CodeDom.Compiler;
  11. using System.IO;
  12. using System.Web.UI;
  13. using System.Reflection;
  14. //temp:
  15. using Microsoft.CSharp;
  16. namespace System.Web.Compilation
  17. {
  18. class WebServiceCompiler : BaseCompiler
  19. {
  20. SimpleWebHandlerParser wService;
  21. ICodeCompiler compiler;
  22. public WebServiceCompiler (SimpleWebHandlerParser wService)
  23. : base (null)
  24. {
  25. this.wService = wService;
  26. }
  27. public static Type CompileIntoType (SimpleWebHandlerParser wService)
  28. {
  29. WebServiceCompiler wsc = new WebServiceCompiler (wService);
  30. return wsc.GetCompiledType ();
  31. }
  32. public override Type GetCompiledType ()
  33. {
  34. if (wService.Program.Trim () == "")
  35. return wService.GetTypeFromBin (wService.ClassName);
  36. CompilationCacheItem item = CachingCompiler.GetCached (wService.PhysicalPath);
  37. if (item != null) {
  38. Assembly a = item.Result.CompiledAssembly;
  39. if (a != null)
  40. return a.GetType (wService.ClassName, true);
  41. }
  42. //FIXME: update when we support other languages
  43. string fname = GetTempFileNameWithExtension ("cs");
  44. StreamWriter sw = new StreamWriter (File.OpenWrite (fname));
  45. sw.WriteLine (wService.Program);
  46. sw.Close ();
  47. //TODO: get the compiler and default options from system.web/compileroptions
  48. CompilerResults results = CachingCompiler.Compile (wService.PhysicalPath, fname, this);
  49. FileInfo finfo = new FileInfo (fname);
  50. finfo.Delete ();
  51. CheckCompilerErrors (results);
  52. return results.CompiledAssembly.GetType (wService.ClassName, true);
  53. }
  54. static string GetTempFileNameWithExtension (string extension)
  55. {
  56. Exception exc;
  57. string extFile;
  58. do {
  59. string tmpFile = Path.GetTempFileName ();
  60. FileInfo fileInfo = new FileInfo (tmpFile);
  61. extFile = Path.ChangeExtension (tmpFile, extension);
  62. try {
  63. fileInfo.MoveTo (extFile);
  64. exc = null;
  65. } catch (Exception e) {
  66. exc = e;
  67. }
  68. } while (exc != null);
  69. return extFile;
  70. }
  71. void CheckCompilerErrors (CompilerResults results)
  72. {
  73. if (results.NativeCompilerReturnValue == 0)
  74. return;
  75. throw new CompilationException (wService.PhysicalPath, results.Errors, wService.Program);
  76. }
  77. internal new SimpleWebHandlerParser Parser {
  78. get { return wService; }
  79. }
  80. internal override ICodeCompiler Compiler {
  81. get {
  82. if (compiler == null) {
  83. //TODO: get the compiler and default options from system.web/compileroptions
  84. compiler = new CSharpCodeProvider ().CreateCompiler ();
  85. }
  86. return compiler;
  87. }
  88. }
  89. }
  90. }