2 // AspNetReplyChannel.cs
5 // Ankit Jain <jankit@novell.com>
7 // Copyright (C) 2006 Novell, Inc. http://www.novell.com
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections.Generic;
32 using System.ServiceModel;
34 using System.Threading;
37 namespace System.ServiceModel.Channels
39 internal class AspNetReplyChannel : HttpReplyChannel
41 AspNetChannelListener<IReplyChannel> listener;
42 List<HttpContext> waiting = new List<HttpContext> ();
43 HttpContext http_context;
44 ManualResetEvent wait;
46 public AspNetReplyChannel (AspNetChannelListener<IReplyChannel> listener)
49 this.listener = listener;
52 internal void CloseContext ()
54 if (http_context == null)
57 ((AspNetListenerManager) listener.ListenerManager).HttpHandler.EndRequest (listener, http_context);
63 void ShutdownPendingRequests ()
66 foreach (HttpContext ctx in waiting)
68 ((AspNetListenerManager) listener.ListenerManager).HttpHandler.EndRequest (listener, ctx);
73 protected override void OnAbort ()
75 ShutdownPendingRequests ();
79 protected override void OnClose (TimeSpan timeout)
81 base.OnClose (timeout);
83 ShutdownPendingRequests ();
87 public override bool TryReceiveRequest (TimeSpan timeout, out RequestContext context)
90 return TryReceiveRequestCore (timeout, out context);
91 } catch (Exception ex) {
93 Console.WriteLine ("AspNetReplyChannel caught an error: " + ex);
98 bool TryReceiveRequestCore (TimeSpan timeout, out RequestContext context)
101 if (waiting.Count == 0 && !WaitForRequest (timeout))
104 if (waiting.Count > 0) {
105 http_context = waiting [0];
106 waiting.RemoveAt (0);
109 if (http_context == null)
110 // Though as long as this instance is used
111 // synchronously, it should not happen.
113 if (http_context.Response.StatusCode != 200) {
114 http_context.Response.Close ();
119 var req = http_context.Request;
120 if (req.HttpMethod == "GET") {
121 msg = Message.CreateMessage (Encoder.MessageVersion, null);
123 //FIXME: Do above stuff for HttpContext ?
124 int maxSizeOfHeaders = 0x10000;
126 msg = Encoder.ReadMessage (
127 req.InputStream, maxSizeOfHeaders);
129 if (Encoder.MessageVersion.Envelope == EnvelopeVersion.Soap11) {
130 string action = GetHeaderItem (req.Headers ["SOAPAction"]);
132 msg.Headers.Action = action;
136 // FIXME: prop.SuppressEntityBody
137 msg.Headers.To = req.Url;
138 msg.Properties.Add ("Via", LocalAddress.Uri);
139 msg.Properties.Add (HttpRequestMessageProperty.Name, CreateRequestProperty (req.HttpMethod, req.Url.Query, req.Headers));
141 context = new AspNetRequestContext (this, msg, http_context);
147 public override bool WaitForRequest (TimeSpan timeout)
149 if (http_context == null)
150 http_context = listener.HttpHandler.WaitForRequest (listener, timeout);
151 return http_context != null;
155 public override bool WaitForRequest (TimeSpan timeout)
158 throw new InvalidOperationException ("Another wait operation is in progress");
160 var wait_ = new ManualResetEvent (false);
162 listener.ListenerManager.GetHttpContextAsync (timeout, HttpContextAcquired);
163 return wait_.WaitOne (timeout, false) && waiting.Count > 0;
169 void HttpContextAcquired (HttpContextInfo ctx)
172 throw new InvalidOperationException ("WaitForRequest operation has not started");
173 var sctx = (AspNetHttpContextInfo) ctx;
174 if (State == CommunicationState.Opened && ctx != null)
175 waiting.Add (sctx.Source);