예제 #1
0
        private void AsyncStreamCallback(IAsyncResult asyncResult)
        {
            var state = asyncResult.AsyncState as Pair<WebRequest, Pair<TimeSpan, int>>;
            if (state == null)
            {
                // Unrecognized state signature
                throw new ArgumentNullException("asyncResult",
                                                "The asynchronous post failed to return its state");
            }

            var request = state.First;
            var duration = state.Second.First;
            var resultCount = state.Second.Second;

            WebResponse response = null;
            Stream stream = null;

            try
            {
                using (response = request.EndGetResponse(asyncResult))
                {
#if SILVERLIGHT
                    if (DecompressionMethods == Silverlight.Compat.DecompressionMethods.GZip ||
                        DecompressionMethods == Silverlight.Compat.DecompressionMethods.Deflate ||
                        DecompressionMethods == (Silverlight.Compat.DecompressionMethods.GZip | Silverlight.Compat.DecompressionMethods.Deflate)
                        )
                    {
                        response = new GzipHttpWebResponse((HttpWebResponse)response);
                    }
#endif
                    StreamImpl(out stream, request, response);
                }
            }
            catch (WebException ex)
            {
                HandleWebException(ex);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                }

                WebResponse = response;
            }
        }
예제 #2
0
        protected virtual void PostAsyncResponseCallback(IAsyncResult asyncResult)
        {
#if WindowsPhone
            Interlocked.Increment(ref completed);
#endif

            WebRequest request;
            Triplet<WebRequest, Triplet<ICache, object, string>, object> state;
            try
            {
                state = asyncResult.AsyncState as Triplet<WebRequest, Triplet<ICache, object, string>, object>;
                if (state == null)
                {
                    throw new ArgumentNullException("asyncResult", "The asynchronous post failed to return its state");
                }

                request = state.First;
                if (request == null)
                {
                    throw new ArgumentNullException("asyncResult", "The asynchronous post failed to return a request");
                }
            }
            catch(Exception ex)
            {
                var args = new WebQueryResponseEventArgs(new MemoryStream(), ex);
                OnQueryResponse(args);
                return;
            }

            try
            {
                // Avoid disposing until no longer needed to build results
                var response = request.EndGetResponse(asyncResult);

#if SILVERLIGHT
                if (DecompressionMethods == Silverlight.Compat.DecompressionMethods.GZip ||
                    DecompressionMethods == Silverlight.Compat.DecompressionMethods.Deflate ||
                    DecompressionMethods == (Silverlight.Compat.DecompressionMethods.GZip | Silverlight.Compat.DecompressionMethods.Deflate)
                    )
                {
                    response = new GzipHttpWebResponse((HttpWebResponse)response);
                }
#endif
                WebResponse = response;

                ContentStream = response.GetResponseStream();
                if (state.Second != null)
                {
                    var cache = state.Second.First;
                    var expiry = state.Second.Second;
                    var url = request.RequestUri.ToString();

                    var prefix = state.Second.Third;
                    var key = CreateCacheKey(prefix, url);

                    if (expiry is DateTime)
                    {
                        // absolute
                        cache.Insert(key, ContentStream, (DateTime)expiry);
                    }

                    if (expiry is TimeSpan)
                    {
                        // sliding
                        cache.Insert(key, ContentStream, (TimeSpan)expiry);
                    }
                }

                var args = new WebQueryResponseEventArgs(ContentStream);
                OnQueryResponse(args);
            }
            catch (WebException ex)
            {
                HandleWebException(ex);
            }
        }
예제 #3
0
        protected virtual void GetAsyncResponseCallback(IAsyncResult asyncResult)
        {

#if WindowsPhone
            Interlocked.Increment(ref completed);
#endif

            Console.Out.WriteLine("----- WebQuery.Async.cs GetAsyncResponseCallback");

            object store;
            var request = GetAsyncCacheStore(asyncResult, out store);

            try
            {
                var response = request.EndGetResponse(asyncResult);
                using (response)
                {
#if SILVERLIGHT
                    if (DecompressionMethods == Silverlight.Compat.DecompressionMethods.GZip ||
                        DecompressionMethods == Silverlight.Compat.DecompressionMethods.Deflate ||
                        DecompressionMethods == (Silverlight.Compat.DecompressionMethods.GZip | Silverlight.Compat.DecompressionMethods.Deflate)
                        )
                    {
                        response = new GzipHttpWebResponse((HttpWebResponse)response);
                    }
#endif
                    WebResponse = response;
                    
                    ContentStream = response.GetResponseStream();
                    
                    if (store != null)
                    {
                        // No expiration specified
                        if (store is Pair<ICache, string>)
                        {
                            var cache = store as Pair<ICache, string>;
                            cache.First.Insert(cache.Second, ContentStream);
                        }

                        // Absolute expiration specified
                        if (store is Pair<ICache, Pair<string, DateTime>>)
                        {
                            var cache = store as Pair<ICache, Pair<string, DateTime>>;
                            cache.First.Insert(cache.Second.First, ContentStream, cache.Second.Second);
                        }

                        // Sliding expiration specified
                        if (store is Pair<ICache, Pair<string, TimeSpan>>)
                        {
                            var cache = store as Pair<ICache, Pair<string, TimeSpan>>;
                            cache.First.Insert(cache.Second.First, ContentStream, cache.Second.Second);
                        }
                    }

                    // Only send query when caching is complete
                    var args = new WebQueryResponseEventArgs(ContentStream);
                    OnQueryResponse(args);
                }
            }
            catch (WebException ex)
            {
                HandleWebException(ex);
            }
        }