WebServiceCompiler.cs 4.3 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. ICodeCompiler compiler;
  42. CodeDomProvider provider;
  43. CompilerParameters compilerParameters;
  44. string inputFile;
  45. public WebServiceCompiler (SimpleWebHandlerParser wService)
  46. : base (null)
  47. {
  48. this.parser = wService;
  49. }
  50. public static Type CompileIntoType (SimpleWebHandlerParser wService)
  51. {
  52. WebServiceCompiler wsc = new WebServiceCompiler (wService);
  53. return wsc.GetCompiledType ();
  54. }
  55. public override Type GetCompiledType ()
  56. {
  57. Type type = CachingCompiler.GetTypeFromCache (parser.PhysicalPath, parser.ClassName);
  58. if (type != null)
  59. return type;
  60. if (parser.Program.Trim () == "")
  61. return parser.GetTypeFromBin (parser.ClassName);
  62. string lang = parser.Language;
  63. CompilationConfiguration config;
  64. config = CompilationConfiguration.GetInstance (parser.Context);
  65. provider = config.GetProvider (lang);
  66. if (provider == null)
  67. throw new HttpException ("Configuration error. Language not supported: " +
  68. lang, 500);
  69. compiler = provider.CreateCompiler ();
  70. compilerParameters = CachingCompiler.GetOptions (parser.Assemblies);
  71. compilerParameters.IncludeDebugInformation = parser.Debug;
  72. compilerParameters.CompilerOptions = config.GetCompilerOptions (lang);
  73. compilerParameters.WarningLevel = config.GetWarningLevel (lang);
  74. bool keepFiles = (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") != null);
  75. TempFileCollection tempcoll;
  76. tempcoll = new TempFileCollection (config.TempDirectory, keepFiles);
  77. compilerParameters.TempFiles = tempcoll;
  78. inputFile = tempcoll.AddExtension (provider.FileExtension);
  79. Stream st = File.OpenWrite (inputFile);
  80. StreamWriter sw = new StreamWriter (st);
  81. sw.WriteLine (parser.Program);
  82. sw.Close ();
  83. string dllfilename = Path.GetFileName (tempcoll.AddExtension ("dll", true));
  84. if (!Directory.Exists (dynamicBase))
  85. Directory.CreateDirectory (dynamicBase);
  86. compilerParameters.OutputAssembly = Path.Combine (dynamicBase, dllfilename);
  87. CompilerResults results = CachingCompiler.Compile (this);
  88. CheckCompilerErrors (results);
  89. if (results.CompiledAssembly == null)
  90. throw new CompilationException (inputFile, results.Errors,
  91. "No assembly returned after compilation!?");
  92. results.TempFiles.Delete ();
  93. return results.CompiledAssembly.GetType (parser.ClassName, true);
  94. }
  95. void CheckCompilerErrors (CompilerResults results)
  96. {
  97. if (results.NativeCompilerReturnValue == 0)
  98. return;
  99. throw new CompilationException (parser.PhysicalPath, results.Errors, parser.Program);
  100. }
  101. internal new SimpleWebHandlerParser Parser {
  102. get { return parser; }
  103. }
  104. internal override ICodeCompiler Compiler {
  105. get { return compiler; }
  106. }
  107. internal CompilerParameters CompilerOptions {
  108. get { return compilerParameters; }
  109. }
  110. internal string InputFile {
  111. get { return inputFile; }
  112. }
  113. }
  114. }