SvcHttpHandler.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // SvcHttpHandler.cs
  3. //
  4. // Author:
  5. // Ankit Jain <[email protected]>
  6. // Atsushi Enomoto <[email protected]>
  7. //
  8. // Copyright (C) 2006,2009-2010 Novell, Inc. http://www.novell.com
  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. using System;
  30. using System.Collections.Generic;
  31. using System.Collections.ObjectModel;
  32. using System.Linq;
  33. using System.Web;
  34. using System.Threading;
  35. using System.ServiceModel;
  36. using System.ServiceModel.Activation;
  37. using System.ServiceModel.Channels.Http;
  38. using System.ServiceModel.Configuration;
  39. using System.ServiceModel.Description;
  40. namespace System.ServiceModel.Channels
  41. {
  42. internal class SvcHttpHandler : IHttpHandler
  43. {
  44. internal static SvcHttpHandler Current;
  45. static object type_lock = new object ();
  46. Type type;
  47. Type factory_type;
  48. string path;
  49. ServiceHostBase host;
  50. Dictionary<HttpContext,ManualResetEvent> wcf_wait_handles = new Dictionary<HttpContext,ManualResetEvent> ();
  51. int close_state;
  52. public SvcHttpHandler (Type type, Type factoryType, string path)
  53. {
  54. this.type = type;
  55. this.factory_type = factoryType;
  56. this.path = path;
  57. }
  58. public bool IsReusable
  59. {
  60. get { return true; }
  61. }
  62. public ServiceHostBase Host {
  63. get { return host; }
  64. }
  65. public void ProcessRequest (HttpContext context)
  66. {
  67. EnsureServiceHost ();
  68. var table = HttpListenerManagerTable.GetOrCreate (host);
  69. var manager = table.GetOrCreateManager (context.Request.Url);
  70. if (manager == null)
  71. manager = table.GetOrCreateManager (host.BaseAddresses [0]);
  72. var wait = new ManualResetEvent (false);
  73. wcf_wait_handles [context] = wait;
  74. manager.ProcessNewContext (new System.ServiceModel.Channels.Http.AspNetHttpContextInfo (this, context));
  75. // This method must not return until the RequestContext
  76. // explicitly finishes replying. Otherwise xsp will
  77. // close the connection after this method call.
  78. wait.WaitOne ();
  79. }
  80. public void EndHttpRequest (HttpContext context)
  81. {
  82. ManualResetEvent wait;
  83. if (!wcf_wait_handles.TryGetValue (context, out wait))
  84. return;
  85. wcf_wait_handles.Remove (context);
  86. if (wait != null)
  87. wait.Set ();
  88. }
  89. // called from SvcHttpHandlerFactory's remove callback (i.e.
  90. // unloading asp.net). It closes ServiceHost, then the host
  91. // in turn closes the listener and the channels it opened.
  92. // The channel listener calls CloseServiceChannel() to stop
  93. // accepting further requests on its shutdown.
  94. public void Close ()
  95. {
  96. host.Close ();
  97. host = null;
  98. }
  99. void EnsureServiceHost ()
  100. {
  101. lock (type_lock) {
  102. Current = this;
  103. try {
  104. EnsureServiceHostCore ();
  105. } finally {
  106. Current = null;
  107. }
  108. }
  109. }
  110. void EnsureServiceHostCore ()
  111. {
  112. if (host != null)
  113. return;
  114. //ServiceHost for this not created yet
  115. var baseUri = new Uri (new Uri (HttpContext.Current.Request.Url.GetLeftPart (UriPartial.Authority)), path);
  116. if (factory_type != null) {
  117. host = ((ServiceHostFactory) Activator.CreateInstance (factory_type)).CreateServiceHost (type, new Uri [] {baseUri});
  118. }
  119. else
  120. host = new ServiceHost (type, baseUri);
  121. host.Extensions.Add (new VirtualPathExtension (baseUri.AbsolutePath));
  122. host.Open ();
  123. }
  124. }
  125. }