} // DispatchExceptionOrRetry // called from StartRequest private static void ProcessGetRequestStreamCompletion(IAsyncResult iar) { // We've just received a request stream. AsyncHttpClientRequestState asyncRequestState = (AsyncHttpClientRequestState)iar.AsyncState; try { HttpWebRequest httpWebRequest = asyncRequestState.WebRequest; Stream sourceRequestStream = asyncRequestState.RequestStream; Stream webRequestStream = httpWebRequest.EndGetRequestStream(iar); StreamHelper.BeginAsyncCopyStream( sourceRequestStream, webRequestStream, false, true, // [....] read, async write false, true, // leave source open, close target s_processAsyncCopyRequestStreamCompletionCallback, asyncRequestState); } catch (Exception e) { asyncRequestState.RetryOrDispatchException(e); } } // ProcessGetRequestStreamCompletion
} // ProcessGetResponseCompletion // called from ProcessGetResponseCompletion private static void ProcessAsyncCopyResponseStreamCompletion(IAsyncResult iar) { // We've just finished copying the network response stream into a memory stream. AsyncHttpClientRequestState asyncRequestState = (AsyncHttpClientRequestState)iar.AsyncState; try { StreamHelper.EndAsyncCopyStream(iar); HttpWebResponse webResponse = asyncRequestState.WebResponse; Stream responseStream = asyncRequestState.ActualResponseStream; ITransportHeaders responseHeaders = CollectResponseHeaders(webResponse); // call down the sink chain asyncRequestState.SinkStack.AsyncProcessResponse(responseHeaders, responseStream); } catch (Exception e) { asyncRequestState.SinkStack.DispatchException(e); } catch { asyncRequestState.SinkStack.DispatchException(new Exception(CoreChannel.GetResourceString("Remoting_nonClsCompliantException"))); } } // ProcessAsyncResponseStreamCompletion
} // ProcessMessage public void AsyncProcessRequest(IClientChannelSinkStack sinkStack, IMessage msg, ITransportHeaders headers, Stream stream) { // Send the webrequest, headers, request stream, and retry count. AsyncHttpClientRequestState asyncRequestState = new AsyncHttpClientRequestState(this, sinkStack, msg, headers, stream, 1); asyncRequestState.StartRequest(); } // AsyncProcessRequest
} // ProcessGetRequestStreamCompletion // called from ProcessGetRequestStreamCompletion private static void ProcessAsyncCopyRequestStreamCompletion(IAsyncResult iar) { // We've just finished copying the original request stream into the network stream. AsyncHttpClientRequestState asyncRequestState = (AsyncHttpClientRequestState)iar.AsyncState; try { StreamHelper.EndAsyncCopyStream(iar); asyncRequestState.WebRequest.BeginGetResponse( s_processGetResponseCompletionCallback, asyncRequestState); } catch (Exception e) { // This is the last point where we should retry. asyncRequestState.RetryOrDispatchException(e); } } // ProcessAsyncCopyRequestStreamCompletion
} // ProcessAsyncCopyRequestStreamCompletion // called from ProcessAsyncCopyRequestStreamCompletion private static void ProcessGetResponseCompletion(IAsyncResult iar) { // We've just received a response. AsyncHttpClientRequestState asyncRequestState = (AsyncHttpClientRequestState)iar.AsyncState; try { // close the request stream since we are done with it. asyncRequestState.RequestStream.Close(); HttpWebResponse httpWebResponse = null; HttpWebRequest httpWebRequest = asyncRequestState.WebRequest; try { httpWebResponse = (HttpWebResponse)httpWebRequest.EndGetResponse(iar); } catch (WebException webException) { ProcessResponseException(webException, out httpWebResponse); } asyncRequestState.WebResponse = httpWebResponse; // Asynchronously pump the web response stream into a memory stream. ChunkedMemoryStream responseStream = new ChunkedMemoryStream(CoreChannel.BufferPool); asyncRequestState.ActualResponseStream = responseStream; StreamHelper.BeginAsyncCopyStream( httpWebResponse.GetResponseStream(), responseStream, true, false, // async read, sync write true, false, // close source, leave target open s_processAsyncCopyRequestStreamCompletion, asyncRequestState); } catch (Exception e) { asyncRequestState.SinkStack.DispatchException(e); } catch { asyncRequestState.SinkStack.DispatchException(new Exception(CoreChannel.GetResourceString("Remoting_nonClsCompliantException"))); } } // ProcessGetResponseCompletion
} // ProcessMessage public void AsyncProcessRequest(IClientChannelSinkStack sinkStack, IMessage msg, ITransportHeaders headers, Stream stream) { // Send the webrequest, headers, request stream, and retry count. AsyncHttpClientRequestState asyncRequestState = new AsyncHttpClientRequestState(this, sinkStack, msg, headers, stream, 1); asyncRequestState.StartRequest(); } // AsyncProcessRequest