WebServiceCompiler.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // System.Web.Compilation.WebServiceCompiler
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System;
  10. using System.IO;
  11. using System.Reflection;
  12. using System.Web.UI;
  13. namespace System.Web.Compilation
  14. {
  15. class WebServiceCompiler : BaseCompiler
  16. {
  17. SimpleWebHandlerParser wService;
  18. string sourceFile;
  19. private WebServiceCompiler (SimpleWebHandlerParser wService)
  20. {
  21. this.wService = wService;
  22. }
  23. public static Type CompileIntoType (SimpleWebHandlerParser wService)
  24. {
  25. CompilationCacheItem item = CachingCompiler.GetCached (wService.PhysicalPath);
  26. if (item != null && item.Result != null) {
  27. if (item.Result != null)
  28. return item.Result.Data as Type;
  29. throw new CompilationException (item.Result);
  30. }
  31. WebServiceCompiler wsc = new WebServiceCompiler (wService);
  32. return wsc.GetCompiledType ();
  33. }
  34. static string GenerateSourceFile (SimpleWebHandlerParser wService)
  35. {
  36. //FIXME: should get Tmp dir for this application
  37. string csName = Path.GetTempFileName ();
  38. StreamWriter output = new StreamWriter (File.OpenWrite (csName));
  39. output.Write (wService.Program);
  40. output.Close ();
  41. return csName;
  42. }
  43. public override Type GetCompiledType ()
  44. {
  45. sourceFile = GenerateSourceFile (wService);
  46. CachingCompiler compiler = new CachingCompiler (this);
  47. CompilationResult result = new CompilationResult ();
  48. if (compiler.Compile (result) == false)
  49. throw new CompilationException (result);
  50. Assembly assembly = Assembly.LoadFrom (result.OutputFile);
  51. Type [] types = assembly.GetTypes ();
  52. Type type = types [0];
  53. if (type.FullName != wService.ClassName)
  54. throw new ApplicationException (String.Format (
  55. "Class={0}, but the class compiled is {1}",
  56. wService.ClassName,
  57. type.FullName));
  58. result.Data = type;
  59. return type;
  60. }
  61. public override string Key {
  62. get {
  63. return wService.PhysicalPath;
  64. }
  65. }
  66. public override string SourceFile {
  67. get {
  68. return sourceFile;
  69. }
  70. }
  71. }
  72. }