2
0

WebServiceCompiler.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.CodeDom.Compiler;
  32. using System.IO;
  33. using System.Web.Configuration;
  34. using System.Web.UI;
  35. using System.Reflection;
  36. namespace System.Web.Compilation
  37. {
  38. class WebServiceCompiler : BaseCompiler
  39. {
  40. SimpleWebHandlerParser parser;
  41. string inputFile;
  42. public WebServiceCompiler (SimpleWebHandlerParser wService)
  43. : base (null)
  44. {
  45. this.parser = wService;
  46. }
  47. public static Type CompileIntoType (SimpleWebHandlerParser wService)
  48. {
  49. WebServiceCompiler wsc = new WebServiceCompiler (wService);
  50. return wsc.GetCompiledType ();
  51. }
  52. public override Type GetCompiledType ()
  53. {
  54. Type type = CachingCompiler.GetTypeFromCache (parser.PhysicalPath);
  55. if (type != null)
  56. return type;
  57. if (parser.Program.Trim () == "") {
  58. type = Type.GetType (parser.ClassName, false);
  59. if (type == null)
  60. type = parser.GetTypeFromBin (parser.ClassName);
  61. CachingCompiler.InsertTypeFileDep (type, parser.PhysicalPath);
  62. return type;
  63. }
  64. string lang = parser.Language;
  65. string compilerOptions;
  66. string tempdir;
  67. int warningLevel;
  68. CodeDomProvider provider;
  69. Provider = provider = CreateProvider (parser.Context, lang, out compilerOptions, out warningLevel, out tempdir);
  70. if (Provider == null)
  71. throw new HttpException ("Configuration error. Language not supported: " +
  72. lang, 500);
  73. CompilerParameters compilerParameters;
  74. CompilerParameters = compilerParameters = CachingCompiler.GetOptions (parser.Assemblies);
  75. compilerParameters.IncludeDebugInformation = parser.Debug;
  76. compilerParameters.CompilerOptions = compilerOptions;
  77. compilerParameters.WarningLevel = warningLevel;
  78. bool keepFiles = (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") != null);
  79. TempFileCollection tempcoll;
  80. tempcoll = new TempFileCollection (tempdir, keepFiles);
  81. compilerParameters.TempFiles = tempcoll;
  82. inputFile = tempcoll.AddExtension (provider.FileExtension);
  83. Stream st = File.OpenWrite (inputFile);
  84. StreamWriter sw = new StreamWriter (st);
  85. sw.WriteLine (parser.Program);
  86. sw.Close ();
  87. string dllfilename = Path.GetFileName (tempcoll.AddExtension ("dll", true));
  88. compilerParameters.OutputAssembly = Path.Combine (DynamicDir (), dllfilename);
  89. CompilerResults results = CachingCompiler.Compile (this);
  90. CheckCompilerErrors (results);
  91. Assembly assembly = results.CompiledAssembly;
  92. if (assembly == null) {
  93. if (!File.Exists (compilerParameters.OutputAssembly))
  94. throw new CompilationException (inputFile, results.Errors,
  95. "No assembly returned after compilation!?");
  96. assembly = Assembly.LoadFrom (compilerParameters.OutputAssembly);
  97. }
  98. results.TempFiles.Delete ();
  99. type = assembly.GetType (parser.ClassName, true);
  100. CachingCompiler.InsertTypeFileDep (type, parser.PhysicalPath);
  101. return type;
  102. }
  103. void CheckCompilerErrors (CompilerResults results)
  104. {
  105. if (results.NativeCompilerReturnValue == 0)
  106. return;
  107. throw new CompilationException (parser.PhysicalPath, results.Errors, parser.Program);
  108. }
  109. internal new SimpleWebHandlerParser Parser {
  110. get { return parser; }
  111. }
  112. internal string InputFile {
  113. get { return inputFile; }
  114. }
  115. }
  116. }