WebServiceCompiler.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.InsertTypeFileDep (type, parser.PhysicalPath);
  63. return type;
  64. }
  65. string lang = parser.Language;
  66. #if NET_2_0
  67. CompilationSection config = (CompilationSection)WebConfigurationManager.GetSection ("system.web/compilation");
  68. Compiler c = config.Compilers[lang];
  69. Type t = Type.GetType (c.Type, true);
  70. provider = Activator.CreateInstance (t) as CodeDomProvider;
  71. #else
  72. CompilationConfiguration config;
  73. config = CompilationConfiguration.GetInstance (parser.Context);
  74. provider = config.GetProvider (lang);
  75. #endif
  76. if (provider == null)
  77. throw new HttpException ("Configuration error. Language not supported: " +
  78. lang, 500);
  79. compiler = provider.CreateCompiler ();
  80. compilerParameters = CachingCompiler.GetOptions (parser.Assemblies);
  81. compilerParameters.IncludeDebugInformation = parser.Debug;
  82. #if NET_2_0
  83. compilerParameters.CompilerOptions = c.CompilerOptions;
  84. compilerParameters.WarningLevel = c.WarningLevel;
  85. #else
  86. compilerParameters.CompilerOptions = config.GetCompilerOptions (lang);
  87. compilerParameters.WarningLevel = config.GetWarningLevel (lang);
  88. #endif
  89. bool keepFiles = (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") != null);
  90. TempFileCollection tempcoll;
  91. tempcoll = new TempFileCollection (config.TempDirectory, keepFiles);
  92. compilerParameters.TempFiles = tempcoll;
  93. inputFile = tempcoll.AddExtension (provider.FileExtension);
  94. Stream st = File.OpenWrite (inputFile);
  95. StreamWriter sw = new StreamWriter (st);
  96. sw.WriteLine (parser.Program);
  97. sw.Close ();
  98. string dllfilename = Path.GetFileName (tempcoll.AddExtension ("dll", true));
  99. compilerParameters.OutputAssembly = Path.Combine (DynamicDir (), dllfilename);
  100. CompilerResults results = CachingCompiler.Compile (this);
  101. CheckCompilerErrors (results);
  102. Assembly assembly = results.CompiledAssembly;
  103. if (assembly == null) {
  104. if (!File.Exists (compilerParameters.OutputAssembly))
  105. throw new CompilationException (inputFile, results.Errors,
  106. "No assembly returned after compilation!?");
  107. assembly = Assembly.LoadFrom (compilerParameters.OutputAssembly);
  108. }
  109. results.TempFiles.Delete ();
  110. type = assembly.GetType (parser.ClassName, true);
  111. return type;
  112. }
  113. void CheckCompilerErrors (CompilerResults results)
  114. {
  115. if (results.NativeCompilerReturnValue == 0)
  116. return;
  117. throw new CompilationException (parser.PhysicalPath, results.Errors, parser.Program);
  118. }
  119. internal new SimpleWebHandlerParser Parser {
  120. get { return parser; }
  121. }
  122. internal override ICodeCompiler Compiler {
  123. get { return compiler; }
  124. }
  125. internal CompilerParameters CompilerOptions {
  126. get { return compilerParameters; }
  127. }
  128. internal string InputFile {
  129. get { return inputFile; }
  130. }
  131. }
  132. }