2 // System.Web.Compilation.WebServiceCompiler
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
8 // Copyright (c) 2040 Novell, Inc. (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.CodeDom.Compiler;
34 using System.Web.Configuration;
36 using System.Reflection;
38 namespace System.Web.Compilation
40 class WebServiceCompiler : BaseCompiler
42 SimpleWebHandlerParser parser;
43 ICodeCompiler compiler;
44 CodeDomProvider provider;
45 CompilerParameters compilerParameters;
48 public WebServiceCompiler (SimpleWebHandlerParser wService)
51 this.parser = wService;
54 public static Type CompileIntoType (SimpleWebHandlerParser wService)
56 WebServiceCompiler wsc = new WebServiceCompiler (wService);
57 return wsc.GetCompiledType ();
60 public override Type GetCompiledType ()
62 Type type = CachingCompiler.GetTypeFromCache (parser.PhysicalPath);
66 if (parser.Program.Trim () == "") {
67 type = parser.GetTypeFromBin (parser.ClassName);
68 CachingCompiler.InsertTypeFileDep (type, parser.PhysicalPath);
72 string lang = parser.Language;
74 CompilationSection config = (CompilationSection)WebConfigurationManager.GetSection ("system.web/compilation");
75 Compiler c = config.Compilers[lang];
76 Type t = Type.GetType (c.Type, true);
77 provider = Activator.CreateInstance (t) as CodeDomProvider;
79 CompilationConfiguration config;
80 config = CompilationConfiguration.GetInstance (parser.Context);
81 provider = config.GetProvider (lang);
84 throw new HttpException ("Configuration error. Language not supported: " +
87 compiler = provider.CreateCompiler ();
89 compilerParameters = CachingCompiler.GetOptions (parser.Assemblies);
90 compilerParameters.IncludeDebugInformation = parser.Debug;
92 compilerParameters.CompilerOptions = c.CompilerOptions;
93 compilerParameters.WarningLevel = c.WarningLevel;
95 compilerParameters.CompilerOptions = config.GetCompilerOptions (lang);
96 compilerParameters.WarningLevel = config.GetWarningLevel (lang);
99 bool keepFiles = (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") != null);
101 TempFileCollection tempcoll;
102 tempcoll = new TempFileCollection (config.TempDirectory, keepFiles);
103 compilerParameters.TempFiles = tempcoll;
105 inputFile = tempcoll.AddExtension (provider.FileExtension);
106 Stream st = File.OpenWrite (inputFile);
107 StreamWriter sw = new StreamWriter (st);
108 sw.WriteLine (parser.Program);
111 string dllfilename = Path.GetFileName (tempcoll.AddExtension ("dll", true));
113 compilerParameters.OutputAssembly = Path.Combine (DynamicDir (), dllfilename);
115 CompilerResults results = CachingCompiler.Compile (this);
116 CheckCompilerErrors (results);
117 Assembly assembly = results.CompiledAssembly;
118 if (assembly == null) {
119 if (!File.Exists (compilerParameters.OutputAssembly))
120 throw new CompilationException (inputFile, results.Errors,
121 "No assembly returned after compilation!?");
122 assembly = Assembly.LoadFrom (compilerParameters.OutputAssembly);
125 results.TempFiles.Delete ();
126 type = assembly.GetType (parser.ClassName, true);
130 void CheckCompilerErrors (CompilerResults results)
132 if (results.NativeCompilerReturnValue == 0)
135 throw new CompilationException (parser.PhysicalPath, results.Errors, parser.Program);
138 internal new SimpleWebHandlerParser Parser {
139 get { return parser; }
142 internal override ICodeCompiler Compiler {
143 get { return compiler; }
146 internal CompilerParameters CompilerOptions {
147 get { return compilerParameters; }
150 internal string InputFile {
151 get { return inputFile; }