2 // Microsoft.Web.Services.Messaging.SoapChannel.cs
4 // Author: Todd Berman <tberman@gentoo.org>
6 // (C) 2003 Todd Berman
10 using Microsoft.Web.Services;
12 namespace Microsoft.Web.Services.Messaging
14 public abstract class SoapChannel
17 private ISoapFormatter _formatter;
18 private object _sync_obj = new object ();
20 public SoapChannel (ISoapFormatter format)
23 throw new ArgumentNullException ("formatter");
28 public abstract void Close ();
30 public SoapEnvelope DeserializeMessage (Stream stream)
32 return _formatter.Deserialize (stream);
35 public void SerializeMessage (SoapEnvelope envelope, Stream stream)
37 _formatter.Serialize (envelope, stream);
40 public abstract SoapEnvelope Receive ();
41 public abstract void Send (SoapEnvelope envelope);
43 public abstract bool Active {
47 public abstract string Scheme {
51 public object SyncRoot {
52 get { return _sync_obj; }
55 public IAsyncResult BeginReceive (AsyncCallback callback, object state)
57 return new ReceiveAsyncResult (this, callback, state);
60 public SoapEnvelope EndReceive (IAsyncResult result)
63 throw new ArgumentNullException ("result");
66 ReceiveAsyncResult res = result as ReceiveAsyncResult;
69 ReceiveAsyncResult.End (result);
73 throw new InvalidOperationException ("Invalid IAsyncResult type");
76 public IAsyncResult BeginSend (SoapEnvelope env, AsyncCallback callback, object state)
79 throw new ArgumentNullException ("envelope");
81 return new SendAsyncResult (this, env, callback, state);
84 public void EndSend (IAsyncResult result)
87 throw new ArgumentNullException ("result");
89 SendAsyncResult a_res = result as SendAsyncResult;
92 AsyncResult.End (result);
94 throw new InvalidOperationException ("Invalid IAsyncResult type");
97 protected class ReceiveAsyncResult : AsyncResult
99 private SoapEnvelope _envelope;
101 private delegate SoapEnvelope Receive ();
103 public ReceiveAsyncResult (SoapChannel channel,
104 AsyncCallback callback,
105 object state) : base (callback, state)
107 Receive rec = new Receive (channel.Receive);
109 rec.BeginInvoke (new AsyncCallback (OnReceiveComplete), rec);
112 private void OnReceiveComplete (IAsyncResult result)
114 Receive rec = (Receive) result.AsyncState;
117 _envelope = rec.EndInvoke (result);
118 Complete (result.CompletedSynchronously);
119 } catch (Exception e) {
120 Complete (result.CompletedSynchronously, e);
124 public SoapEnvelope Envelope {
125 get { return _envelope; }
129 protected class SendAsyncResult : AsyncResult
131 private delegate void Send (SoapEnvelope e);
133 public SendAsyncResult (SoapChannel channel,
135 AsyncCallback callback,
136 object state) : base (callback, state)
138 Send send_delegate = new Send (channel.Send);
139 send_delegate.BeginInvoke (env, new AsyncCallback (OnSendComplete), send_delegate);
142 private void OnSendComplete (IAsyncResult result)
144 Send s_del = result.AsyncState as Send;
147 s_del.EndInvoke (result);
148 this.Complete (result.CompletedSynchronously);
149 } catch (Exception e) {
150 Complete (result.CompletedSynchronously, e);