2 // System.Web.Services.Protocols.SoapHttpClientProtocol.cs
5 // Tim Coleman (tim@timcoleman.com)
6 // Miguel de Icaza (miguel@ximian.com)
7 // Lluis Sanchez Gual (lluis@ximian.com)
9 // Copyright (C) Tim Coleman, 2002
10 // Copyright (C) Ximian, Inc, 2003
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.ComponentModel;
40 using System.Reflection;
41 using System.Web.Services;
42 using System.Diagnostics;
43 using System.Runtime.CompilerServices;
44 using System.Web.Services.Description;
45 using System.Web.Services.Discovery;
46 using System.Xml.Serialization;
47 using System.Xml.Schema;
48 using System.Collections;
49 using System.Threading;
51 namespace System.Web.Services.Protocols
54 [System.Runtime.InteropServices.ComVisible (true)]
56 public class SoapHttpClientProtocol : HttpWebClientProtocol
58 SoapTypeStubInfo type_info;
60 SoapProtocolVersion soapVersion;
63 #region SoapWebClientAsyncResult class
65 internal class SoapWebClientAsyncResult: WebClientAsyncResult
67 public SoapWebClientAsyncResult (WebRequest request, AsyncCallback callback, object asyncState)
68 : base (request, callback, asyncState)
72 public SoapClientMessage Message;
73 public SoapExtension[] Extensions;
79 public SoapHttpClientProtocol ()
81 type_info = (SoapTypeStubInfo) TypeStubManager.GetTypeStub (this.GetType (), "Soap");
84 #endregion // Constructors
88 protected IAsyncResult BeginInvoke (string methodName, object[] parameters, AsyncCallback callback, object asyncState)
90 SoapMethodStubInfo msi = (SoapMethodStubInfo) type_info.GetMethod (methodName);
92 SoapWebClientAsyncResult ainfo = null;
95 SoapClientMessage message = new SoapClientMessage (this, msi, Url, parameters);
96 message.CollectHeaders (this, message.MethodStubInfo.Headers, SoapHeaderDirection.In);
98 WebRequest request = GetRequestForMessage (uri, message);
100 ainfo = new SoapWebClientAsyncResult (request, callback, asyncState);
101 ainfo.Message = message;
102 ainfo.Extensions = SoapExtension.CreateExtensionChain (type_info.SoapExtensions[0], msi.SoapExtensions, type_info.SoapExtensions[1]);
104 ainfo.Request.BeginGetRequestStream (new AsyncCallback (AsyncGetRequestStreamDone), ainfo);
109 ainfo.SetCompleted (null, ex, false);
115 void AsyncGetRequestStreamDone (IAsyncResult ar)
117 SoapWebClientAsyncResult ainfo = (SoapWebClientAsyncResult) ar.AsyncState;
120 SendRequest (ainfo.Request.EndGetRequestStream (ar), ainfo.Message, ainfo.Extensions);
121 ainfo.Request.BeginGetResponse (new AsyncCallback (AsyncGetResponseDone), ainfo);
125 ainfo.SetCompleted (null, ex, true);
129 void AsyncGetResponseDone (IAsyncResult ar)
131 SoapWebClientAsyncResult ainfo = (SoapWebClientAsyncResult) ar.AsyncState;
132 WebResponse response = null;
135 response = GetWebResponse (ainfo.Request, ar);
137 catch (WebException ex) {
138 response = ex.Response;
139 HttpWebResponse http_response = response as HttpWebResponse;
140 if (http_response == null || http_response.StatusCode != HttpStatusCode.InternalServerError) {
141 ainfo.SetCompleted (null, ex, true);
145 catch (Exception ex) {
146 ainfo.SetCompleted (null, ex, true);
151 object[] result = ReceiveResponse (response, ainfo.Message, ainfo.Extensions);
152 ainfo.SetCompleted (result, null, true);
154 catch (Exception ex) {
155 ainfo.SetCompleted (null, ex, true);
162 protected object[] EndInvoke (IAsyncResult asyncResult)
164 if (!(asyncResult is SoapWebClientAsyncResult)) throw new ArgumentException ("asyncResult is not the return value from BeginInvoke");
166 SoapWebClientAsyncResult ainfo = (SoapWebClientAsyncResult) asyncResult;
169 if (!ainfo.IsCompleted) ainfo.WaitForComplete ();
170 if (ainfo.Exception != null) throw ainfo.Exception;
171 else return (object[]) ainfo.Result;
175 public void Discover ()
177 BindingInfo bnd = (BindingInfo) type_info.Bindings [0];
179 DiscoveryClientProtocol discoverer = new DiscoveryClientProtocol ();
180 discoverer.Discover (Url);
182 foreach (object info in discoverer.AdditionalInformation)
184 System.Web.Services.Discovery.SoapBinding sb = info as System.Web.Services.Discovery.SoapBinding;
185 if (sb != null && sb.Binding.Name == bnd.Name && sb.Binding.Namespace == bnd.Namespace) {
191 string msg = string.Format ("The binding named '{0}' from namespace '{1}' was not found in the discovery document at '{2}'", bnd.Name, bnd.Namespace, Url);
192 throw new Exception (msg);
195 protected override WebRequest GetWebRequest (Uri uri)
197 return base.GetWebRequest (uri);
200 WebRequest GetRequestForMessage (Uri uri, SoapClientMessage message)
202 WebRequest request = GetWebRequest (uri);
203 request.Method = "POST";
204 WebHeaderCollection headers = request.Headers;
205 headers.Add ("SOAPAction", "\"" + message.Action + "\"");
206 request.ContentType = message.ContentType + "; charset=utf-8";
210 void SendRequest (Stream s, SoapClientMessage message, SoapExtension[] extensions)
214 if (extensions != null) {
215 s = SoapExtension.ExecuteChainStream (extensions, s);
216 message.SetStage (SoapMessageStage.BeforeSerialize);
217 SoapExtension.ExecuteProcessMessage (extensions, message, s, true);
220 XmlTextWriter xtw = WebServiceHelper.CreateXmlWriter (s);
222 WebServiceHelper.WriteSoapMessage (xtw, message.MethodStubInfo, SoapHeaderDirection.In, message.Parameters, message.Headers);
224 if (extensions != null) {
225 message.SetStage (SoapMessageStage.AfterSerialize);
226 SoapExtension.ExecuteProcessMessage (extensions, message, s, true);
237 // Handle other web responses (multi-output?)
239 object [] ReceiveResponse (WebResponse response, SoapClientMessage message, SoapExtension[] extensions)
241 SoapMethodStubInfo msi = message.MethodStubInfo;
242 HttpWebResponse http_response = response as HttpWebResponse;
244 if (http_response != null)
246 HttpStatusCode code = http_response.StatusCode;
248 if (!(code == HttpStatusCode.Accepted || code == HttpStatusCode.OK || code == HttpStatusCode.InternalServerError)) {
249 string msg = "The request failed with HTTP status {0}: {1}";
250 msg = String.Format (msg, (int) code, code);
251 throw new WebException (msg, null, WebExceptionStatus.ProtocolError, http_response);
253 if (message.OneWay && response.ContentLength <= 0 && (code == HttpStatusCode.Accepted || code == HttpStatusCode.OK)) {
254 return new object[0];
259 // Remove optional encoding
262 Encoding encoding = WebServiceHelper.GetContentEncoding (response.ContentType, out ctype);
263 if (ctype != "text/xml")
264 WebServiceHelper.InvalidOperation (
265 "Content is not 'text/xml' but '" + response.ContentType + "'",
268 message.ContentType = ctype;
269 message.ContentEncoding = encoding.WebName;
271 Stream stream = response.GetResponseStream ();
273 if (extensions != null) {
274 stream = SoapExtension.ExecuteChainStream (extensions, stream);
275 message.SetStage (SoapMessageStage.BeforeDeserialize);
276 SoapExtension.ExecuteProcessMessage (extensions, message, stream, false);
279 // Deserialize the response
281 SoapHeaderCollection headers;
284 using (StreamReader reader = new StreamReader (stream, encoding, false)) {
285 XmlTextReader xml_reader = new XmlTextReader (reader);
287 WebServiceHelper.ReadSoapMessage (xml_reader, msi, SoapHeaderDirection.Out, out content, out headers);
291 if (content is Fault)
293 Fault fault = (Fault) content;
294 SoapException ex = new SoapException (fault.faultstring, fault.faultcode, fault.faultactor, fault.detail);
295 message.SetException (ex);
298 message.OutParameters = (object[]) content;
300 message.SetHeaders (headers);
301 message.UpdateHeaderValues (this, message.MethodStubInfo.Headers);
303 if (extensions != null) {
304 message.SetStage (SoapMessageStage.AfterDeserialize);
305 SoapExtension.ExecuteProcessMessage (extensions, message, stream, false);
308 if (message.Exception == null)
309 return message.OutParameters;
311 throw message.Exception;
314 protected object[] Invoke (string method_name, object[] parameters)
316 SoapMethodStubInfo msi = (SoapMethodStubInfo) type_info.GetMethod (method_name);
318 SoapClientMessage message = new SoapClientMessage (this, msi, Url, parameters);
319 message.CollectHeaders (this, message.MethodStubInfo.Headers, SoapHeaderDirection.In);
321 SoapExtension[] extensions = SoapExtension.CreateExtensionChain (type_info.SoapExtensions[0], msi.SoapExtensions, type_info.SoapExtensions[1]);
323 WebResponse response;
326 WebRequest request = GetRequestForMessage (uri, message);
327 SendRequest (request.GetRequestStream (), message, extensions);
328 response = GetWebResponse (request);
330 catch (WebException ex)
332 response = ex.Response;
333 HttpWebResponse http_response = response as HttpWebResponse;
334 if (http_response == null || http_response.StatusCode != HttpStatusCode.InternalServerError)
339 return ReceiveResponse (response, message, extensions);
348 [MonoTODO ("Do something with this")]
349 [System.Runtime.InteropServices.ComVisible(false)]
350 [DefaultValue (SoapProtocolVersion.Default)]
351 public SoapProtocolVersion SoapVersion {
352 get { return soapVersion; }
353 set { soapVersion = value; }
356 protected void InvokeAsync (string methodName, object[] parameters, SendOrPostCallback callback)
358 InvokeAsync (methodName, parameters, callback, null);
361 protected void InvokeAsync (string methodName, object[] parameters, SendOrPostCallback callback, object userState)
363 InvokeAsyncInfo info = new InvokeAsyncInfo (callback, userState);
364 BeginInvoke (methodName, parameters, new AsyncCallback (InvokeAsyncCallback), info);
367 void InvokeAsyncCallback (IAsyncResult ar)
369 InvokeAsyncInfo info = (InvokeAsyncInfo) ar.AsyncState;
370 SoapWebClientAsyncResult sar = (SoapWebClientAsyncResult) ar;
371 InvokeCompletedEventArgs args = new InvokeCompletedEventArgs (sar.Exception, false, info.UserState, (object[]) sar.Result);
372 if (info.Context != null)
373 info.Context.Send (info.Callback, args);
375 info.Callback (args);
380 #endregion // Methods