Exemplo n.º 1
0
        /// <summary>
        /// Parses the responses.
        /// </summary>
        /// <param name="state">The state.</param>
        private void ParseResponses(object state)
        {
            try
            {
                Guid traceId = Guid.Empty;
                HangingTraceStream tracingStream = null;
                MemoryStream       responseCopy  = null;

                try
                {
                    bool traceEwsResponse = this.Service.IsTraceEnabledFor(TraceFlags.EwsResponse);

                    using (Stream responseStream = this.response.GetResponseStream())
                    {
                        tracingStream = new HangingTraceStream(responseStream, this.Service);

                        // EwsServiceMultiResponseXmlReader.Create causes a read.
                        if (traceEwsResponse)
                        {
                            responseCopy = new MemoryStream();
                            tracingStream.SetResponseCopy(responseCopy);
                        }

                        EwsServiceMultiResponseXmlReader ewsXmlReader = EwsServiceMultiResponseXmlReader.Create(tracingStream, this.Service);

                        while (this.IsConnected)
                        {
                            object responseObject = null;
                            if (traceEwsResponse)
                            {
                                try
                                {
                                    responseObject = this.ReadResponse(ewsXmlReader, this.response.Headers);
                                }
                                finally
                                {
                                    this.Service.TraceXml(TraceFlags.EwsResponse, responseCopy);
                                }

                                // reset the stream collector.
                                responseCopy.Dispose();
                                responseCopy = new MemoryStream();
                                tracingStream.SetResponseCopy(responseCopy);
                            }
                            else
                            {
                                responseObject = this.ReadResponse(ewsXmlReader, this.response.Headers);
                            }

                            this.responseHandler(responseObject);
                        }
                    }
                }
                catch (TimeoutException ex)
                {
                    // The connection timed out.
                    this.Disconnect(HangingRequestDisconnectReason.Timeout, ex);
                    return;
                }
                catch (IOException ex)
                {
                    // Stream is closed, so disconnect.
                    this.Disconnect(HangingRequestDisconnectReason.Exception, ex);
                    return;
                }
                catch (HttpRequestException ex)
                {
                    // Stream is closed, so disconnect.
                    this.Disconnect(HangingRequestDisconnectReason.Exception, ex);
                    return;
                }
                catch (WebException ex)
                {
                    // Stream is closed, so disconnect.
                    this.Disconnect(HangingRequestDisconnectReason.Exception, ex);
                    return;
                }
                catch (ObjectDisposedException ex)
                {
                    // Stream is closed, so disconnect.
                    this.Disconnect(HangingRequestDisconnectReason.Exception, ex);
                    return;
                }
                catch (NotSupportedException)
                {
                    // This is thrown if we close the stream during a read operation due to a user method call.
                    // Trying to delay closing until the read finishes simply results in a long-running connection.
                    this.Disconnect(HangingRequestDisconnectReason.UserInitiated, null);
                    return;
                }
                catch (XmlException ex)
                {
                    // Thrown if server returned no XML document.
                    this.Disconnect(HangingRequestDisconnectReason.UserInitiated, ex);
                    return;
                }
                finally
                {
                    if (responseCopy != null)
                    {
                        responseCopy.Dispose();
                        responseCopy = null;
                    }
                }
            }
            catch (ServiceLocalException exception)
            {
                this.Disconnect(HangingRequestDisconnectReason.Exception, exception);
            }
        }
        /// <summary>
        /// Parses the responses.
        /// </summary>
        /// <param name="state">The state.</param>
        private void ParseResponses(object state)
        {
            try
            {
                Guid traceId = Guid.Empty;
                HangingTraceStream tracingStream = null;
                MemoryStream responseCopy = null;

                try
                {
                    bool traceEwsResponse = this.Service.IsTraceEnabledFor(TraceFlags.EwsResponse);

                    using (Stream responseStream = this.response.GetResponseStream())
                    {
                        responseStream.ReadTimeout = 2 * this.heartbeatFrequencyMilliseconds;
                        tracingStream = new HangingTraceStream(responseStream, this.Service);

                        // EwsServiceMultiResponseXmlReader.Create causes a read.
                        if (traceEwsResponse)
                        {
                            responseCopy = new MemoryStream();
                            tracingStream.SetResponseCopy(responseCopy);
                        }

                        EwsServiceMultiResponseXmlReader ewsXmlReader = EwsServiceMultiResponseXmlReader.Create(tracingStream, this.Service);

                        while (this.IsConnected)
                        {
                            object responseObject = null;
                            if (traceEwsResponse)
                            {
                                try
                                {
                                    responseObject = this.ReadResponse(ewsXmlReader, this.response.Headers);
                                }
                                finally
                                {
                                    this.Service.TraceXml(TraceFlags.EwsResponse, responseCopy);
                                }

                                // reset the stream collector.
                                responseCopy.Close();
                                responseCopy = new MemoryStream();
                                tracingStream.SetResponseCopy(responseCopy);
                            }
                            else
                            {
                                responseObject = this.ReadResponse(ewsXmlReader, this.response.Headers);
                            }

                            this.responseHandler(responseObject);
                        }
                    }
                }
                catch (TimeoutException ex)
                {
                    // The connection timed out.
                    this.Disconnect(HangingRequestDisconnectReason.Timeout, ex);
                    return;
                }
                catch (IOException ex)
                {
                    // Stream is closed, so disconnect.
                    this.Disconnect(HangingRequestDisconnectReason.Exception, ex);
                    return;
                }
                catch (HttpException ex)
                {
                    // Stream is closed, so disconnect.
                    this.Disconnect(HangingRequestDisconnectReason.Exception, ex);
                    return;
                }
                catch (WebException ex)
                {
                    // Stream is closed, so disconnect.
                    this.Disconnect(HangingRequestDisconnectReason.Exception, ex);
                    return;
                }
                catch (ObjectDisposedException ex)
                {
                    // Stream is closed, so disconnect.
                    this.Disconnect(HangingRequestDisconnectReason.Exception, ex);
                    return;
                }
                catch (NotSupportedException)
                {
                    // This is thrown if we close the stream during a read operation due to a user method call.
                    // Trying to delay closing until the read finishes simply results in a long-running connection.
                    this.Disconnect(HangingRequestDisconnectReason.UserInitiated, null);
                    return;
                }
                catch (XmlException ex)
                {
                    // Thrown if server returned no XML document.
                    this.Disconnect(HangingRequestDisconnectReason.UserInitiated, ex);
                    return;
                }
                finally
                {
                    if (responseCopy != null)
                    {
                        responseCopy.Dispose();
                        responseCopy = null;
                    }
                }
            }
            catch (ServiceLocalException exception)
            {
                this.Disconnect(HangingRequestDisconnectReason.Exception, exception);
            }
        }