/// <summary>
        /// Ends executing this async request.
        /// </summary>
        /// <param name="asyncResult">The async result</param>
        /// <returns>Service response object.</returns>
        internal object EndInternalExecute(IAsyncResult asyncResult)
        {
            // We have done enough validation before
            AsyncRequestResult asyncRequestResult = (AsyncRequestResult)asyncResult;

            IEwsHttpWebResponse response = this.EndGetEwsHttpWebResponse(asyncRequestResult.WebRequest, asyncRequestResult.WebAsyncResult);

            return(this.ReadResponse(response));
        }
Beispiel #2
0
        /// <summary>
        /// Async callback method for HttpWebRequest async requests.
        /// </summary>
        /// <param name="webAsyncResult">An IAsyncResult that references the asynchronous request.</param>
        private static void WebRequestAsyncCallback(IAsyncResult webAsyncResult)
        {
            WebAsyncCallStateAnchor wrappedState = webAsyncResult.AsyncState as WebAsyncCallStateAnchor;

            if (wrappedState != null && wrappedState.AsyncCallback != null)
            {
                AsyncRequestResult asyncRequestResult = new AsyncRequestResult(
                    wrappedState.ServiceRequest,
                    wrappedState.WebRequest,
                    webAsyncResult, /* web async result */
                    wrappedState.AsyncState /* user state */);

                // Call user's call back
                wrappedState.AsyncCallback(asyncRequestResult);
            }
        }
        /// <summary>
        /// Extracts the original service request from the specified IAsyncResult instance
        /// </summary>
        /// <typeparam name="T">Desired service request type</typeparam>
        /// <param name="exchangeService">The ExchangeService object to validate the integrity of asyncResult</param>
        /// <param name="asyncResult">An IAsyncResult that references the asynchronous request.</param>
        /// <returns>The original service request</returns>
        public static T ExtractServiceRequest <T>(ExchangeService exchangeService, IAsyncResult asyncResult) where T : SimpleServiceRequestBase
        {
            // Validate null first
            EwsUtilities.ValidateParam(asyncResult, "asyncResult");

            AsyncRequestResult asyncRequestResult = asyncResult as AsyncRequestResult;

            if (asyncRequestResult == null)
            {
                // Strings.InvalidAsyncResult is copied from the error message of HttpWebRequest.EndGetResponse()
                // Just use this simple string for all kinds of invalid IAsyncResult parameters
                throw new ArgumentException(Strings.InvalidAsyncResult, "asyncResult");
            }

            // Validate the service request
            if (asyncRequestResult.ServiceRequest == null)
            {
                throw new ArgumentException(Strings.InvalidAsyncResult, "asyncResult");
            }

            //Validate the service object
            if (!Object.ReferenceEquals(asyncRequestResult.ServiceRequest.Service, exchangeService))
            {
                throw new ArgumentException(Strings.InvalidAsyncResult, "asyncResult");
            }

            // Validate the request type
            T serviceRequest = asyncRequestResult.ServiceRequest as T;

            if (serviceRequest == null)
            {
                throw new ArgumentException(Strings.InvalidAsyncResult, "asyncResult");
            }

            return(serviceRequest);
        }