WebServiceCompiler.cs 4.5 KB

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