WebServiceCompiler.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 = Path.ChangeExtension (Path.GetTempFileName (), ".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. CheckCompilerErrors (results);
  50. return results.CompiledAssembly.GetType (wService.ClassName, true);
  51. }
  52. void CheckCompilerErrors (CompilerResults results)
  53. {
  54. if (results.NativeCompilerReturnValue == 0)
  55. return;
  56. throw new CompilationException (wService.PhysicalPath, results.Errors, wService.Program);
  57. }
  58. internal new SimpleWebHandlerParser Parser {
  59. get { return wService; }
  60. }
  61. internal override ICodeCompiler Compiler {
  62. get {
  63. if (compiler == null) {
  64. //TODO: get the compiler and default options from system.web/compileroptions
  65. compiler = new CSharpCodeProvider ().CreateCompiler ();
  66. }
  67. return compiler;
  68. }
  69. }
  70. }
  71. }