Esempio n. 1
0
        public void XmlAsyncDownloadTest()
        {
            UTF8Encoding encoding = new UTF8Encoding();

            byte[]       contentAsBytes       = encoding.GetBytes(TestResources.CharacterSheet);
            MemoryStream sourceResponseStream = new MemoryStream();

            sourceResponseStream.Write(contentAsBytes, 0, contentAsBytes.Length);
            sourceResponseStream.Position = 0;
            XmlDocument contentAsXml = new XmlDocument();

            contentAsXml.Load(new StringReader(TestResources.CharacterSheet));

            Mock <HttpWebRequest>        mockRequest  = MockManager.Mock <HttpWebRequest>(Constructor.NotMocked);
            MockObject <HttpWebResponse> mockResponse = MockManager.MockObject <HttpWebResponse>(Constructor.Mocked);

            mockResponse.ExpectAndReturn("GetResponseStream", sourceResponseStream);
            mockRequest.ExpectAndReturn("GetResponse", mockResponse.Object);

            _xmlAsyncCompletedTrigger = new AutoResetEvent(false);
            HttpWebService service = new HttpWebService();

            service.DownloadXmlAsync("http://www.battleclinic.com", null, XmlAysncDownloadTestCompleted, null);
            _xmlAsyncCompletedTrigger.WaitOne();
            if (_xmlAsyncDownloadResult.Error != null)
            {
                Assert.Fail(_xmlAsyncDownloadResult.Error.Message);
            }
            StringAssert.AreEqualIgnoringCase(contentAsXml.ToString(), _xmlAsyncDownloadResult.Result.ToString());
            sourceResponseStream.Close();
            _xmlAsyncCompletedTrigger = null;
            _xmlAsyncDownloadResult   = null;
        }
Esempio n. 2
0
        /// <summary>
        /// When BC returned us results, we parse them.
        /// </summary>
        /// <remarks>Invoked on some background thread.</remarks>
        /// <param name="e"></param>
        /// <param name="userState"></param>
        private static void OnCheckCompleted(DownloadXmlAsyncResult e, object userState)
        {
            // If disabled, reschedule in ten minutes and quits
            if (!s_enabled)
            {
                Dispatcher.Schedule(TimeSpan.FromMinutes(10), () => BeginCheck());
                return;
            }

            // Was there an HTTP error ??
            if (e.Error != null)
            {
                // Stores the error and reschedule in ten minutes
                Trace.WriteLine("UpdateManager: " + e.Error.Message);
                Dispatcher.Schedule(TimeSpan.FromMinutes(10), () => BeginCheck());
                return;
            }

            // No http error, let's try to deserialize
            try
            {
                ScanUpdateFeed(e.Result);
            }
            // An error occured during the deserialization
            catch (InvalidOperationException exc)
            {
                ExceptionHandler.LogException(exc, true);
            }
            finally
            {
                // Reschedule in one hour
                Dispatcher.Schedule(TimeSpan.FromHours(2), () => BeginCheck());
            }
        }
Esempio n. 3
0
 private void XmlAysncDownloadTestCompleted(DownloadXmlAsyncResult e, object state)
 {
     _xmlAsyncDownloadResult = e;
     _xmlAsyncCompletedTrigger.Set();
 }