SvcHttpHandler.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // SvcHttpHandler.cs
  3. //
  4. // Author:
  5. // Ankit Jain <[email protected]>
  6. // Atsushi Enomoto <[email protected]>
  7. //
  8. // Copyright (C) 2006,2009 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.Linq;
  32. using System.Web;
  33. using System.Threading;
  34. using System.ServiceModel;
  35. using System.ServiceModel.Activation;
  36. using System.ServiceModel.Configuration;
  37. using System.ServiceModel.Description;
  38. namespace System.ServiceModel.Channels {
  39. internal class SvcHttpHandler : IHttpHandler
  40. {
  41. Type type;
  42. Type factory_type;
  43. string path;
  44. Uri request_url;
  45. ServiceHostBase host;
  46. Queue<HttpContext> pending = new Queue<HttpContext> ();
  47. bool closing;
  48. AutoResetEvent wait = new AutoResetEvent (false);
  49. AutoResetEvent listening = new AutoResetEvent (false);
  50. public SvcHttpHandler (Type type, Type factoryType, string path)
  51. {
  52. this.type = type;
  53. this.factory_type = factoryType;
  54. this.path = path;
  55. }
  56. public bool IsReusable
  57. {
  58. get { return true; }
  59. }
  60. public Uri Uri { get; private set; }
  61. public HttpContext WaitForRequest (TimeSpan timeout)
  62. {
  63. DateTime start = DateTime.Now;
  64. lock (pending) {
  65. if (pending.Count > 0) {
  66. var ctx = pending.Dequeue ();
  67. if (ctx.AllErrors != null && ctx.AllErrors.Length > 0)
  68. return WaitForRequest (timeout - (DateTime.Now - start));
  69. return ctx;
  70. }
  71. }
  72. return wait.WaitOne (timeout - (DateTime.Now - start), false) && !closing ?
  73. WaitForRequest (timeout - (DateTime.Now - start)) : null;
  74. }
  75. public void ProcessRequest (HttpContext context)
  76. {
  77. request_url = context.Request.Url;
  78. EnsureServiceHost ();
  79. pending.Enqueue (context);
  80. wait.Set ();
  81. listening.WaitOne ();
  82. }
  83. public void EndRequest (HttpContext context)
  84. {
  85. listening.Set ();
  86. }
  87. public void Close ()
  88. {
  89. closing = true;
  90. listening.Set ();
  91. wait.Set ();
  92. host.Close ();
  93. host = null;
  94. closing = false;
  95. }
  96. void ApplyConfiguration (ServiceHostBase host)
  97. {
  98. foreach (ServiceElement service in ConfigUtil.ServicesSection.Services) {
  99. foreach (ServiceEndpointElement endpoint in service.Endpoints) {
  100. // FIXME: consider BindingName as well
  101. host.AddServiceEndpoint (
  102. endpoint.Contract,
  103. ConfigUtil.CreateBinding (endpoint.Binding, endpoint.BindingConfiguration),
  104. new Uri (path, UriKind.Relative));
  105. }
  106. // behaviors
  107. ServiceBehaviorElement behavior = ConfigUtil.BehaviorsSection.ServiceBehaviors.Find (service.BehaviorConfiguration);
  108. if (behavior != null) {
  109. foreach (BehaviorExtensionElement bxel in behavior) {
  110. IServiceBehavior b = null;
  111. ServiceMetadataPublishingElement meta = bxel as ServiceMetadataPublishingElement;
  112. if (meta != null) {
  113. ServiceMetadataBehavior smb = meta.CreateBehavior () as ServiceMetadataBehavior;
  114. smb.HttpGetUrl = request_url;
  115. // FIXME: HTTPS as well
  116. b = smb;
  117. }
  118. if (b != null)
  119. host.Description.Behaviors.Add (b);
  120. }
  121. }
  122. }
  123. }
  124. void EnsureServiceHost ()
  125. {
  126. if (host != null)
  127. return;
  128. //ServiceHost for this not created yet
  129. var baseUri = new Uri (HttpContext.Current.Request.Url.GetLeftPart (UriPartial.Path));
  130. if (factory_type != null) {
  131. host = ((ServiceHostFactory) Activator.CreateInstance (factory_type)).CreateServiceHost (type, new Uri [] {baseUri});
  132. }
  133. else
  134. host = new ServiceHost (type, baseUri);
  135. ApplyConfiguration (host);
  136. if (host.Description.Endpoints.Count == 0)
  137. //FIXME: Binding: Get from web.config.
  138. host.AddServiceEndpoint (ContractDescription.GetContract (type).Name,
  139. new BasicHttpBinding (), new Uri (path, UriKind.Relative));
  140. var c = host.BaseAddresses;
  141. var ba = c.FirstOrDefault (u => u.Scheme == Uri.UriSchemeHttp || u.Scheme == Uri.UriSchemeHttps);
  142. if (ba != null)
  143. this.Uri = new Uri (ba, path);
  144. else
  145. this.Uri = host.Description.Endpoints [0].Address.Uri;
  146. host.Open ();
  147. //listening.WaitOne ();
  148. }
  149. }
  150. }