SvcHttpHandler.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // SvcHttpHandler.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.Web;
  30. using System.Threading;
  31. using System.ServiceModel;
  32. using System.ServiceModel.Activation;
  33. using System.ServiceModel.Configuration;
  34. using System.ServiceModel.Description;
  35. namespace System.ServiceModel.Channels {
  36. internal class SvcHttpHandler : IHttpHandler
  37. {
  38. Type type;
  39. Type factory_type;
  40. internal string path;
  41. Uri request_url;
  42. ServiceHostBase host;
  43. AspNetReplyChannel reply_channel;
  44. AutoResetEvent wait = new AutoResetEvent (false);
  45. AutoResetEvent listening = new AutoResetEvent (false);
  46. public SvcHttpHandler (Type type, Type factoryType, string path)
  47. {
  48. this.type = type;
  49. this.factory_type = factoryType;
  50. this.path = path;
  51. }
  52. public bool IsReusable
  53. {
  54. get { return true; }
  55. }
  56. public bool WaitForRequest (AspNetReplyChannel reply_channel, TimeSpan timeout)
  57. {
  58. this.reply_channel = reply_channel;
  59. listening.Set ();
  60. return wait.WaitOne (timeout, false);
  61. }
  62. public void ProcessRequest (HttpContext context)
  63. {
  64. request_url = context.Request.Url;
  65. EnsureServiceHost ();
  66. reply_channel.Context = context;
  67. wait.Set ();
  68. listening.WaitOne ();
  69. reply_channel.Context = null;
  70. }
  71. public void Close ()
  72. {
  73. host.Close ();
  74. host = null;
  75. }
  76. void ApplyConfiguration (ServiceHost host)
  77. {
  78. foreach (ServiceElement service in ConfigUtil.ServicesSection.Services) {
  79. foreach (ServiceEndpointElement endpoint in service.Endpoints) {
  80. // FIXME: consider BindingName as well
  81. ServiceEndpoint se = host.AddServiceEndpoint (
  82. endpoint.Contract,
  83. ConfigUtil.CreateBinding (endpoint.Binding, endpoint.BindingConfiguration),
  84. new Uri (path));
  85. }
  86. // behaviors
  87. ServiceBehaviorElement behavior = ConfigUtil.BehaviorsSection.ServiceBehaviors.Find (service.BehaviorConfiguration);
  88. if (behavior != null) {
  89. foreach (BehaviorExtensionElement bxel in behavior) {
  90. IServiceBehavior b = null;
  91. ServiceMetadataPublishingElement meta = bxel as ServiceMetadataPublishingElement;
  92. if (meta != null) {
  93. ServiceMetadataBehavior smb = meta.CreateBehavior () as ServiceMetadataBehavior;
  94. smb.HttpGetUrl = request_url;
  95. // FIXME: HTTPS as well
  96. b = smb;
  97. }
  98. if (b != null)
  99. host.Description.Behaviors.Add (b);
  100. }
  101. }
  102. }
  103. }
  104. void EnsureServiceHost ()
  105. {
  106. if (reply_channel != null)
  107. return;
  108. //ServiceHost for this not created yet
  109. if (factory_type != null)
  110. host = ((ServiceHostFactory) Activator.CreateInstance (factory_type)).CreateServiceHost (type, new Uri [0]);
  111. else
  112. host = new ServiceHost (type);
  113. #if true
  114. //FIXME: Binding: Get from web.config.
  115. host.AddServiceEndpoint (ContractDescription.GetContract (type).Name,
  116. new BasicHttpBinding (), new Uri (path));
  117. #else
  118. ApplyConfiguration (host);
  119. #endif
  120. host.Open ();
  121. listening.WaitOne ();
  122. }
  123. }
  124. }