New tests.
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / HttpRequestChannel.cs
index 42fb54e2279f47da959c8462af0a39b6dca2c77a..4bae6026237252bad1bf95b9c5ccf720bea05541 100644 (file)
@@ -146,6 +146,13 @@ namespace System.ServiceModel.Channels
                        }
 #endif
 
+/*
+// FIXME: this causes invalid message security.
+var mb = message.CreateBufferedCopy (0x10000);
+message = mb.CreateMessage ();
+Console.WriteLine (mb.CreateMessage ());
+*/
+
                        if (!suppressEntityBody && String.Compare (web_request.Method, "GET", StringComparison.OrdinalIgnoreCase) != 0) {
                                MemoryStream buffer = new MemoryStream ();
                                Encoder.WriteMessage (message, buffer);
@@ -317,16 +324,15 @@ w.Close ();
                        AsyncCallback callback;
                        ManualResetEvent wait;
                        Exception error;
+                       object locker = new object ();
+                       bool is_completed;
 
                        public HttpChannelRequestAsyncResult (Message message, TimeSpan timeout, AsyncCallback callback, object state)
                        {
-                               CompletedSynchronously = true;
                                Message = message;
                                Timeout = timeout;
                                this.callback = callback;
                                AsyncState = state;
-
-                               wait = new ManualResetEvent (false);
                        }
 
                        public Message Response {
@@ -334,7 +340,13 @@ w.Close ();
                        }
 
                        public WaitHandle AsyncWaitHandle {
-                               get { return wait; }
+                               get {
+                                       lock (locker) {
+                                               if (wait == null)
+                                                       wait = new ManualResetEvent (is_completed);
+                                       }
+                                       return wait;
+                               }
                        }
 
                        public object AsyncState {
@@ -355,7 +367,6 @@ w.Close ();
                                error = error ?? ex;
 
                                IsCompleted = true;
-                               wait.Set ();
                                if (callback != null)
                                        callback (this);
                        }
@@ -365,7 +376,14 @@ w.Close ();
                        }
 
                        public bool IsCompleted {
-                               get; private set;
+                               get { return is_completed; }
+                               set {
+                                       is_completed = value;
+                                       lock (locker) {
+                                               if (is_completed && wait != null)
+                                                       wait.Set ();
+                                       }
+                               }
                        }
 
                        public void WaitEnd ()
@@ -376,9 +394,9 @@ w.Close ();
                                        // exception to the Complete () method and allow the result to complete 'normally'.
 #if NET_2_1
                                        // neither Moonlight nor MonoTouch supports contexts (WaitOne default to false)
-                                       bool result = wait.WaitOne (Timeout);
+                                       bool result = AsyncWaitHandle.WaitOne (Timeout);
 #else
-                                       bool result = wait.WaitOne (Timeout, true);
+                                       bool result = AsyncWaitHandle.WaitOne (Timeout, true);
 #endif
                                        if (!result)
                                                throw new TimeoutException ();