Exemplo n.º 1
0
        private void FetcherAsynch_OnDownloadComplete(RequestState state)
        {
            PostNetworkResponse.StatusCode     = state.StatusCode;
            PostNetworkResponse.URI            = state.AbsoluteUri;
            PostNetworkResponse.Verb           = state.Verb;
            PostNetworkResponse.ResponseString = state.ResponseString;
            PostNetworkResponse.ResponseBytes  = state.ResponseBytes;
            PostNetworkResponse.Expiration     = state.Expiration;
            PostNetworkResponse.Message        = state.ErrorMessage;

            switch (PostNetworkResponse.StatusCode)
            {
            case HttpStatusCode.OK:
            case HttpStatusCode.Created:
            case HttpStatusCode.Accepted:
                // things are ok, no event required
                break;

            case HttpStatusCode.NoContent:               // return when an object is not found
            case HttpStatusCode.Unauthorized:            // return when session expires
            case HttpStatusCode.InternalServerError:     // return when an exception happens
            case HttpStatusCode.ServiceUnavailable:      // return when the database or siteminder are unavailable
                PostNetworkResponse.Message = String.Format("Network Service responded with status code {0}", state.StatusCode);
                MXDevice.PostNetworkResponse(PostNetworkResponse);
                break;

            default:
                PostNetworkResponse.Message = String.Format("FetcherAsynch completed but received HTTP {0}", state.StatusCode);
                MXDevice.Log.Error(PostNetworkResponse.Message);
                MXDevice.PostNetworkResponse(PostNetworkResponse);
                return;
            }
        }
Exemplo n.º 2
0
        private void PostRequest_OnError(RequestState state)
        {
            Exception exc = new Exception("PosterAsynch call to RequestAsynch threw an exception", state.Exception);

            MXDevice.Log.Error(exc);

            PostNetworkResponse.StatusCode = state.StatusCode;
            PostNetworkResponse.Message    = exc.Message;
            PostNetworkResponse.Exception  = exc;
            PostNetworkResponse.URI        = state.Uri;
            PostNetworkResponse.Verb       = state.Verb;

            MXDevice.PostNetworkResponse(PostNetworkResponse);
        }
Exemplo n.º 3
0
        private void FetcherAsynch_OnError(RequestState state)
        {
            Exception exc = new Exception("FetcherAsynch call to FetchAsynch threw an exception", state.Exception);

            MXDevice.Log.Error(exc);

            PostNetworkResponse.StatusCode     = state.StatusCode;
            PostNetworkResponse.Message        = exc.Message;
            PostNetworkResponse.Exception      = exc;
            PostNetworkResponse.URI            = state.AbsoluteUri;
            PostNetworkResponse.Verb           = state.Verb;
            PostNetworkResponse.ResponseString = null;
            PostNetworkResponse.ResponseBytes  = null;

            MXDevice.PostNetworkResponse(PostNetworkResponse);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Synchronous Wrapper method around FetchAsynch() method
        /// </summary>
        /// <param name="cacheIndex"></param>
        /// <param name="cacheIndexItem"></param>
        public NetworkResponse Fetch(string uri, string filename, Dictionary <string, string> headers)
        {
            PostNetworkResponse = new NetworkResponse();
            FetchParameters fetchParameters = new FetchParameters()
            {
                Uri      = uri,
                Headers  = headers,
                FileName = filename
            };

            DateTime dtMetric = DateTime.UtcNow;

            // set callback and error handler
            OnDownloadComplete += new NetworkingEventHandler(FetcherAsynch_OnDownloadComplete);
            OnError            += new NetworkingErrorHandler(FetcherAsynch_OnError);

            System.Threading.ThreadPool.QueueUserWorkItem(parameters =>
            {
                try
                {
                    FetchAsynch(parameters);
                }
                //catch ( Exception e )
                //{
                //    // You could react or save the exception to an 'outside' variable
                //    // threadExc = e;
                //}
                finally
                {
                    autoEvent.Set(); // if you're firing and not forgetting ;)
                }
            }, fetchParameters);

            // WaitOne returns true if autoEvent were signaled (i.e. process completed before timeout expired)
            // WaitOne returns false it the timeout expired before the process completed.
            if (!autoEvent.WaitOne(DefaultTimeout))
            {
                string message = "FetcherAsynch call to FetchAsynch timed out. uri " + fetchParameters.Uri;
                MXDevice.Log.Error(message);

                MXDevice.Log.Debug(string.Format("FetchAsynch timed out: Uri: {0} Time: {1} milliseconds ", uri, DateTime.UtcNow.Subtract(dtMetric).TotalMilliseconds));

                NetworkResponse networkResponse = new NetworkResponse()
                {
                    Message        = message,
                    URI            = fetchParameters.Uri,
                    StatusCode     = HttpStatusCode.RequestTimeout,
                    ResponseString = string.Empty,
                    Expiration     = DateTime.MinValue.ToUniversalTime(),
                };

                MXDevice.PostNetworkResponse(networkResponse);
                return(networkResponse);
            }

            //if ( threadExc != null )
            //{
            //    throw threadExc;
            //}

            MXDevice.Log.Debug(string.Format("FetchAsynch Completed: Uri: {0} Time: {1} milliseconds  Size: {2} ", uri, DateTime.UtcNow.Subtract(dtMetric).TotalMilliseconds, (PostNetworkResponse.ResponseBytes != null ? PostNetworkResponse.ResponseBytes.Length : -1)));


            return(PostNetworkResponse);
        }
Exemplo n.º 5
0
        public NetworkResponse PostBytes(string uri, byte[] postBytes, string contentType, string verb, Dictionary <string, string> headers)
        {
            PostNetworkResponse = new NetworkResponse();
            RequestParameters requestParameters = new RequestParameters()
            {
                PostBytes   = postBytes,
                Uri         = uri,
                Headers     = headers,
                ContentType = contentType,
                Verb        = verb
            };
            DateTime dtMetric = DateTime.UtcNow;

            // set callback and error handler
            OnComplete += new PostRequestEventHandler(PostRequest_OnComplete);
            OnError    += new PostRequestErrorHandler(PostRequest_OnError);

            System.Threading.ThreadPool.QueueUserWorkItem(parameters =>
            {
                try
                {
                    RequestAsynch(parameters);
                }
                //catch ( Exception e )
                //{
                //    // You could react or save the exception to an 'outside' variable
                //    // threadExc = e;
                //}
                finally
                {
                    autoEvent.Set(); // if you're firing and not forgetting ;)
                }
            }, requestParameters);

            // WaitOne returns true if autoEvent were signaled (i.e. process completed before timeout expired)
            // WaitOne returns false it the timeout expired before the process completed.
            if (!autoEvent.WaitOne(DefaultTimeout))
            {
                //throw TimeoutException( "Waited ages for a thread to come in." );
                string message = "PosterAsynch call to RequestAsynch timed out. uri " + requestParameters.Uri;
                MXDevice.Log.Error(message);

                MXDevice.Log.Debug(string.Format("PosterAsynch timed out: Uri: {0} Time: {1} milliseconds ", uri, DateTime.UtcNow.Subtract(dtMetric).TotalMilliseconds));

                NetworkResponse networkResponse = new NetworkResponse()
                {
                    Message        = message,
                    URI            = requestParameters.Uri,
                    StatusCode     = HttpStatusCode.RequestTimeout,
                    ResponseString = string.Empty,
                    Expiration     = DateTime.MinValue.ToUniversalTime(),
                };

                MXDevice.PostNetworkResponse(networkResponse);
                return(networkResponse);
            }

            //if ( threadExc != null )
            //{
            //    throw threadExc;
            //}
            MXDevice.Log.Debug(string.Format("PosterAsynch Completed: Uri: {0} Time: {1} milliseconds  Size: {2} ", uri, DateTime.UtcNow.Subtract(dtMetric).TotalMilliseconds, (PostNetworkResponse.ResponseBytes != null ? PostNetworkResponse.ResponseBytes.Length : -1)));

            return(PostNetworkResponse);
        }