SvcHttpHandlerFactory.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // SvcHttpHandlerFactory.cs
  3. //
  4. // Author:
  5. // Ankit Jain <[email protected]>
  6. //
  7. // Copyright (C) 2006 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.Collections.Specialized;
  32. using System.IO;
  33. using System.Reflection;
  34. using System.ServiceModel;
  35. using System.Web;
  36. using System.Web.Caching;
  37. namespace System.ServiceModel.Channels {
  38. internal class SvcHttpHandlerFactory : IHttpHandlerFactory
  39. {
  40. static Dictionary<string, SvcHttpHandler> handlers = new Dictionary<string, SvcHttpHandler> ();
  41. string privateBinPath;
  42. Type service_type, factory_type;
  43. public SvcHttpHandlerFactory ()
  44. {
  45. ServiceHostingEnvironment.InAspNet = true;
  46. }
  47. public IHttpHandler GetHandler (HttpContext context, string requestType, string url, string pathTranslated)
  48. {
  49. if (handlers.ContainsKey (url))
  50. return handlers [url];
  51. LoadTypeFromSvc (pathTranslated, url, context);
  52. if (service_type == null)
  53. throw new Exception (String.Format (
  54. "Could not find service for url : '{0}'", url));
  55. SvcHttpHandler handler = new SvcHttpHandler (service_type, factory_type, url);
  56. handlers [url] = handler;
  57. return handler;
  58. }
  59. [MonoTODO]
  60. public void ReleaseHandler (IHttpHandler handler)
  61. {
  62. return;
  63. }
  64. internal static SvcHttpHandler GetHandler (string path)
  65. {
  66. return handlers [path];
  67. }
  68. void LoadTypeFromSvc (string path, string url, HttpContext context)
  69. {
  70. if (CachingCompiler.GetTypeFromCache (path) != null)
  71. return;
  72. ServiceHostParser parser = new ServiceHostParser (path, url, context);
  73. parser.Parse ();
  74. if (parser.Program == null) {
  75. //FIXME: Not caching, as parser.TypeName could be
  76. //just typename or fully qualified name
  77. service_type = GetTypeFromBin (parser.TypeName);
  78. /*CachingCompiler.InsertType (
  79. service_type, service_type.Assembly.Location, url,
  80. new CacheItemRemovedCallback (RemovedCallback));*/
  81. } else {
  82. service_type = CachingCompiler.CompileAndGetType (
  83. parser, url,
  84. new CacheItemRemovedCallback (RemovedCallback));
  85. }
  86. if (parser.Factory != null) {
  87. factory_type = GetTypeFromBin (parser.Factory);
  88. /*CachingCompiler.InsertType (
  89. factory_type, factory_type.Assembly.Location, url,
  90. new CacheItemRemovedCallback (RemovedCallback));*/
  91. }
  92. }
  93. string PrivateBinPath {
  94. get {
  95. if (privateBinPath != null)
  96. return privateBinPath;
  97. AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
  98. privateBinPath = setup.PrivateBinPath;
  99. if (!Path.IsPathRooted (privateBinPath)) {
  100. string appbase = setup.ApplicationBase;
  101. if (appbase.StartsWith ("file://")) {
  102. appbase = appbase.Substring (7);
  103. if (Path.DirectorySeparatorChar != '/')
  104. appbase = appbase.Replace ('/', Path.DirectorySeparatorChar);
  105. }
  106. privateBinPath = Path.Combine (appbase, privateBinPath);
  107. }
  108. return privateBinPath;
  109. }
  110. }
  111. //FIXME: Service="TypeName,TypeNamespace" not handled
  112. Type GetTypeFromBin (string typeName)
  113. {
  114. if (!Directory.Exists (PrivateBinPath))
  115. throw new HttpException (String.Format ("Type {0} not found.", typeName));
  116. string [] binDlls = Directory.GetFiles (PrivateBinPath, "*.dll");
  117. Type result = null;
  118. foreach (string dll in binDlls) {
  119. Assembly assembly = Assembly.LoadFrom (dll);
  120. Type type = assembly.GetType (typeName, false);
  121. if (type != null) {
  122. if (result != null)
  123. throw new HttpException (String.Format ("Type {0} is not unique.", typeName));
  124. result = type;
  125. }
  126. }
  127. if (result == null)
  128. throw new HttpException (String.Format ("Type {0} not found.", typeName));
  129. return result;
  130. }
  131. public static void RemovedCallback (string key, object value, CacheItemRemovedReason reason)
  132. {
  133. if (key.StartsWith (CachingCompiler.cacheTypePrefix)) {
  134. string path = key.Remove (0, CachingCompiler.cacheTypePrefix.Length);
  135. SvcHttpHandler handler;
  136. if (!handlers.TryGetValue (path, out handler))
  137. return;
  138. handler.Close ();
  139. handlers.Remove (path);
  140. }
  141. }
  142. }
  143. }