AppWebReferencesCompiler.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // System.Web.Compilation.AppWebResourcesCompiler
  3. //
  4. // Authors:
  5. // Marek Habersack <[email protected]>
  6. //
  7. // Copyright (C) 2008 Novell, Inc (http://novell.com/)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if WEBSERVICES_DEP
  30. using System;
  31. using System.CodeDom;
  32. using System.CodeDom.Compiler;
  33. using System.IO;
  34. using System.Reflection;
  35. using System.Web;
  36. using System.Web.Configuration;
  37. namespace System.Web.Compilation
  38. {
  39. internal class AppWebReferencesCompiler
  40. {
  41. const string ResourcesDirName = "App_WebReferences";
  42. public void Compile ()
  43. {
  44. string refsPath = Path.Combine (HttpRuntime.AppDomainAppPath, ResourcesDirName);
  45. if (!Directory.Exists (refsPath))
  46. return;
  47. string[] files = Directory.GetFiles (refsPath, "*.wsdl", SearchOption.AllDirectories);
  48. if (files == null || files.Length == 0)
  49. return;
  50. CompilationSection cs = WebConfigurationManager.GetWebApplicationSection ("system.web/compilation") as CompilationSection;
  51. if (cs == null)
  52. throw new HttpException ("Unable to determine default compilation language.");
  53. CompilerType ct = BuildManager.GetDefaultCompilerTypeForLanguage (cs.DefaultLanguage, cs);
  54. CodeDomProvider codeDomProvider = null;
  55. Exception codeDomException = null;
  56. try {
  57. codeDomProvider = Activator.CreateInstance (ct.CodeDomProviderType) as CodeDomProvider;
  58. } catch (Exception e) {
  59. codeDomException = e;
  60. }
  61. if (codeDomProvider == null)
  62. throw new HttpException ("Unable to instantiate default compilation language provider.", codeDomException);
  63. AssemblyBuilder ab = new AssemblyBuilder (codeDomProvider, "App_WebReferences_");
  64. ab.CompilerOptions = ct.CompilerParameters;
  65. VirtualPath vp;
  66. WsdlBuildProvider wbp;
  67. foreach (string file in files) {
  68. vp = VirtualPath.PhysicalToVirtual (file);
  69. if (vp == null)
  70. continue;
  71. wbp = new WsdlBuildProvider ();
  72. wbp.SetVirtualPath (vp);
  73. wbp.GenerateCode (ab);
  74. }
  75. CompilerResults results;
  76. try {
  77. results = ab.BuildAssembly ();
  78. } catch (CompilationException ex) {
  79. throw new HttpException ("Failed to compile web references.", ex);
  80. }
  81. if (results == null)
  82. return;
  83. Assembly asm = results.CompiledAssembly;
  84. BuildManager.TopLevelAssemblies.Add (asm);
  85. }
  86. }
  87. }
  88. #endif