/// <summary> /// Loads this <see cref="MyCustomRssFeed"/> instance asynchronously using the specified <see cref="Uri"/>, <see cref="SyndicationResourceLoadSettings"/>, <see cref="ICredentials"/>, and <see cref="IWebProxy"/>. /// </summary> /// <param name="source">A <see cref="Uri"/> that represents the URL of the syndication resource XML data.</param> /// <param name="settings">The <see cref="SyndicationResourceLoadSettings"/> object used to configure the <see cref="MyCustomRssFeed"/> instance. This value can be <b>null</b>.</param> /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param> /// <param name="userToken">A user-defined object that is passed to the method invoked when the asynchronous operation completes.</param> /// <remarks> /// <para> /// To receive notification when the operation has completed or the operation has been canceled, add an event handler to the <see cref="Loaded"/> event. /// You can cancel a <see cref="LoadAsync(Uri, SyndicationResourceLoadSettings, ICredentials, IWebProxy, Object)"/> operation by calling the <see cref="LoadAsyncCancel()"/> method. /// </para> /// <para> /// After calling <see cref="LoadAsync(Uri, SyndicationResourceLoadSettings, ICredentials, IWebProxy, Object)"/>, /// you must wait for the load operation to complete before attempting to load the syndication resource using the <see cref="LoadAsync(Uri, Object)"/> method. /// </para> /// </remarks> /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="FormatException">The <paramref name="source"/> data does not conform to the expected syndication content format. In this case, the feed remains empty.</exception> /// <exception cref="InvalidOperationException">This <see cref="MyCustomRssFeed"/> has a <see cref="LoadAsync(Uri, SyndicationResourceLoadSettings, ICredentials, IWebProxy, Object)"/> call in progress.</exception> public void LoadAsync(Uri source, SyndicationResourceLoadSettings settings, WebRequestOptions options, Object userToken) { Guard.ArgumentNotNull(source, "source"); if (settings == null) { settings = new SyndicationResourceLoadSettings(); } if (this.LoadOperationInProgress) { throw new InvalidOperationException(); } this.LoadOperationInProgress = true; this.AsyncLoadHasBeenCancelled = false; asyncHttpWebRequest = SyndicationEncodingUtility.CreateWebRequest(source, options); asyncHttpWebRequest.Timeout = Convert.ToInt32(settings.Timeout.TotalMilliseconds, System.Globalization.NumberFormatInfo.InvariantInfo); object[] state = new object[6] { asyncHttpWebRequest, this, source, settings, options, userToken }; IAsyncResult result = asyncHttpWebRequest.BeginGetResponse(new AsyncCallback(AsyncLoadCallback), state); ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(AsyncTimeoutCallback), state, settings.Timeout, true); }
/// <summary> /// Called when a corresponding asynchronous load operation completes. /// </summary> /// <param name="result">The result of the asynchronous operation.</param> private static void AsyncLoadCallback(IAsyncResult result) { System.Text.Encoding encoding = System.Text.Encoding.UTF8; XPathNavigator navigator = null; WebRequest httpWebRequest = null; MyCustomRssFeed feed = null; Uri source = null; WebRequestOptions options = null; SyndicationResourceLoadSettings settings = null; if (result.IsCompleted) { object[] parameters = (object[])result.AsyncState; httpWebRequest = parameters[0] as WebRequest; feed = parameters[1] as MyCustomRssFeed; source = parameters[2] as Uri; settings = parameters[3] as SyndicationResourceLoadSettings; options = parameters[4] as WebRequestOptions; object userToken = parameters[5]; if (feed != null) { WebResponse httpWebResponse = (WebResponse)httpWebRequest.EndGetResponse(result); using (Stream stream = httpWebResponse.GetResponseStream()) { if (settings != null) { encoding = settings.CharacterEncoding; } using (StreamReader streamReader = new StreamReader(stream, encoding)) { XmlReaderSettings readerSettings = new XmlReaderSettings(); readerSettings.IgnoreComments = true; readerSettings.IgnoreWhitespace = true; readerSettings.DtdProcessing = DtdProcessing.Ignore; using (XmlReader reader = XmlReader.Create(streamReader, readerSettings)) { if (encoding == System.Text.Encoding.UTF8) { navigator = SyndicationEncodingUtility.CreateSafeNavigator(source, options, null); } else { navigator = SyndicationEncodingUtility.CreateSafeNavigator(source, options, settings.CharacterEncoding); } // Code to load the syndication resource using the XPathNavigator would go here. // If you support legacy formats, you would use a SyndicationResourceAdapter to fill the feed; // otherwise you would utilize the feed's Load method. feed.OnFeedLoaded(new SyndicationResourceLoadedEventArgs(navigator, source, options, userToken)); } } } feed.LoadOperationInProgress = false; } } }
/// <summary> /// Loads the syndication resource from the specified <see cref="Stream"/>. /// </summary> /// <param name="stream">The <b>Stream</b> used to load the syndication resource.</param> /// <param name="settings">The <see cref="SyndicationResourceLoadSettings"/> object used to configure the <see cref="MyCustomRssFeed"/> instance. This value can be <b>null</b>.</param> /// <remarks> /// After the load operation has successfully completed, the <see cref="MyCustomRssFeed.Loaded"/> event will be raised. /// </remarks> /// <exception cref="ArgumentNullException">The <paramref name="stream"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="FormatException">The <paramref name="stream"/> data does not conform to the expected syndication content format. In this case, the feed remains empty.</exception> /// <exception cref="XmlException">There is a load or parse error in the XML. In this case, the feed remains empty.</exception> public void Load(Stream stream, SyndicationResourceLoadSettings settings) { Guard.ArgumentNotNull(stream, "stream"); if (settings != null) { this.Load(SyndicationEncodingUtility.CreateSafeNavigator(stream, settings.CharacterEncoding), settings); } else { this.Load(SyndicationEncodingUtility.CreateSafeNavigator(stream), settings); } }
/// <summary> /// Loads this <see cref="AtomEntry"/> instance asynchronously using the specified <see cref="Uri"/>, <see cref="SyndicationResourceLoadSettings"/>, <see cref="ICredentials"/>, and <see cref="IWebProxy"/>. /// </summary> /// <param name="source">A <see cref="Uri"/> that represents the URL of the syndication resource XML data.</param> /// <param name="settings">The <see cref="SyndicationResourceLoadSettings"/> object used to configure the <see cref="AtomEntry"/> instance. This value can be <b>null</b>.</param> /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param> /// <param name="userToken">A user-defined object that is passed to the method invoked when the asynchronous operation completes.</param> /// <remarks> /// <para> /// To receive notification when the operation has completed or the operation has been canceled, add an event handler to the <see cref="AtomEntry.Loaded"/> event. /// You can cancel a <see cref="LoadAsync(Uri, SyndicationResourceLoadSettings, ICredentials, IWebProxy, Object)"/> operation by calling the <see cref="LoadAsyncCancel()"/> method. /// </para> /// <para> /// After calling <see cref="LoadAsync(Uri, SyndicationResourceLoadSettings, ICredentials, IWebProxy, Object)"/>, /// you must wait for the load operation to complete before attempting to load the syndication resource using the <see cref="AtomEntry.LoadAsync(Uri, Object)"/> method. /// </para> /// </remarks> /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="FormatException">The <paramref name="source"/> data does not conform to the expected syndication content format. In this case, the feed remains empty.</exception> /// <exception cref="InvalidOperationException">This <see cref="AtomEntry"/> has a <see cref="LoadAsync(Uri, SyndicationResourceLoadSettings, ICredentials, IWebProxy, Object)"/> call in progress.</exception> public new void LoadAsync(Uri source, SyndicationResourceLoadSettings settings, WebRequestOptions options, Object userToken) { //------------------------------------------------------------ // Validate parameters //------------------------------------------------------------ Guard.ArgumentNotNull(source, "source"); //------------------------------------------------------------ // Use default settings if none specified by the caller //------------------------------------------------------------ if (settings == null) { settings = new SyndicationResourceLoadSettings(); } //------------------------------------------------------------ // Validate syndication resource state //------------------------------------------------------------ if (this.LoadOperationInProgress) { throw new InvalidOperationException(); } //------------------------------------------------------------ // Indicate that a load operation is in progress //------------------------------------------------------------ this.LoadOperationInProgress = true; //------------------------------------------------------------ // Reset the asynchronous load operation cancelled indicator //------------------------------------------------------------ this.AsyncLoadHasBeenCancelled = false; //------------------------------------------------------------ // Build HTTP web request used to retrieve the syndication resource //------------------------------------------------------------ asyncHttpWebRequest = SyndicationEncodingUtility.CreateWebRequest(source, options); asyncHttpWebRequest.Timeout = Convert.ToInt32(settings.Timeout.TotalMilliseconds, System.Globalization.NumberFormatInfo.InvariantInfo); //------------------------------------------------------------ // Get the async response to the web request //------------------------------------------------------------ object[] state = new object[6] { asyncHttpWebRequest, this, source, settings, options, userToken }; IAsyncResult result = asyncHttpWebRequest.BeginGetResponse(new AsyncCallback(AsyncLoadCallback), state); //------------------------------------------------------------ // Register the timeout callback //------------------------------------------------------------ ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(AsyncTimeoutCallback), state, settings.Timeout, true); }
/// <summary> /// Loads the syndication resource from the supplied <see cref="Uri"/> using the specified <see cref="ICredentials">credentials</see>, <see cref="IWebProxy">proxy</see> and <see cref="SyndicationResourceLoadSettings"/>. /// </summary> /// <param name="source">A <see cref="Uri"/> that points to the location of the web resource used to load the syndication resource.</param> /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param> /// <param name="settings">The <see cref="SyndicationResourceLoadSettings"/> object used to configure the <see cref="MyCustomRssFeed"/> instance. This value can be <b>null</b>.</param> /// <remarks> /// <para> /// <list type="bullet"> /// <item> /// <description> /// If <paramref name="settings"/> has a <see cref="SyndicationResourceLoadSettings.CharacterEncoding">character encoding</see> of <see cref="System.Text.Encoding.UTF8"/> /// the character encoding of the <paramref name="source"/> will be attempt to be determined automatically, otherwise the specified character encoding will be used. /// If automatic detection fails, a character encoding of <see cref="System.Text.Encoding.UTF8"/> is used by default. /// </description> /// </item> /// <item> /// <description> /// After the load operation has successfully completed, the <see cref="MyCustomRssFeed.Loaded"/> event will be raised. /// </description> /// </item> /// </list> /// </para> /// </remarks> /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="FormatException">The <paramref name="source"/> data does not conform to the expected syndication content format. In this case, the feed remains empty.</exception> /// <exception cref="XmlException">There is a load or parse error in the XML. In this case, the feed remains empty.</exception> public void Load(Uri source, WebRequestOptions options, SyndicationResourceLoadSettings settings) { XPathNavigator navigator = null; Guard.ArgumentNotNull(source, "source"); if (settings == null) { settings = new SyndicationResourceLoadSettings(); } if (settings.CharacterEncoding == System.Text.Encoding.UTF8) { navigator = SyndicationEncodingUtility.CreateSafeNavigator(source, options, null); } else { navigator = SyndicationEncodingUtility.CreateSafeNavigator(source, options, settings.CharacterEncoding); } this.Load(navigator, settings, new SyndicationResourceLoadedEventArgs(navigator, source, options)); }
//============================================================ // CALLBACK DELEGATE METHODS //============================================================ #region AsyncLoadCallback(IAsyncResult result) /// <summary> /// Called when a corresponding asynchronous load operation completes. /// </summary> /// <param name="result">The result of the asynchronous operation.</param> private static void AsyncLoadCallback(IAsyncResult result) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ System.Text.Encoding encoding = System.Text.Encoding.UTF8; XPathNavigator navigator = null; WebRequest httpWebRequest = null; AtomEntryResource entry = null; Uri source = null; WebRequestOptions options = null; SyndicationResourceLoadSettings settings = null; //------------------------------------------------------------ // Determine if the async send operation completed //------------------------------------------------------------ if (result.IsCompleted) { //------------------------------------------------------------ // Extract the send operations parameters from the user state //------------------------------------------------------------ object[] parameters = (object[])result.AsyncState; httpWebRequest = parameters[0] as WebRequest; entry = parameters[1] as AtomEntryResource; source = parameters[2] as Uri; settings = parameters[3] as SyndicationResourceLoadSettings; options = parameters[4] as WebRequestOptions; object userToken = parameters[5]; //------------------------------------------------------------ // Verify expected parameters were found //------------------------------------------------------------ if (entry != null) { //------------------------------------------------------------ // Get the response to the syndication resource request //------------------------------------------------------------ WebResponse httpWebResponse = (WebResponse)httpWebRequest.EndGetResponse(result); //------------------------------------------------------------ // Load syndication resource //------------------------------------------------------------ using (Stream stream = httpWebResponse.GetResponseStream()) { if (settings != null) { encoding = settings.CharacterEncoding; } using (StreamReader streamReader = new StreamReader(stream, encoding)) { XmlReaderSettings readerSettings = new XmlReaderSettings(); readerSettings.IgnoreComments = true; readerSettings.IgnoreWhitespace = true; readerSettings.ProhibitDtd = false; using (XmlReader reader = XmlReader.Create(streamReader, readerSettings)) { if (encoding == System.Text.Encoding.UTF8) { navigator = SyndicationEncodingUtility.CreateSafeNavigator(source, options, null); } else { navigator = SyndicationEncodingUtility.CreateSafeNavigator(source, options, settings.CharacterEncoding); } //------------------------------------------------------------ // Load syndication resource using the framework adapters //------------------------------------------------------------ SyndicationResourceAdapter adapter = new SyndicationResourceAdapter(navigator, settings); adapter.Fill(entry, SyndicationContentFormat.Atom); //------------------------------------------------------------ // Extract member resource information from extensions //------------------------------------------------------------ AtomPublishingEditedSyndicationExtension editedExtension = entry.FindExtension(AtomPublishingEditedSyndicationExtension.MatchByType) as AtomPublishingEditedSyndicationExtension; if (editedExtension != null) { entry.EditedOn = editedExtension.Context.EditedOn; } AtomPublishingControlSyndicationExtension controlExtension = entry.FindExtension(AtomPublishingControlSyndicationExtension.MatchByType) as AtomPublishingControlSyndicationExtension; if (controlExtension != null) { entry.IsDraft = controlExtension.Context.IsDraft; } //------------------------------------------------------------ // Raise Loaded event to notify registered handlers of state change //------------------------------------------------------------ entry.OnEntryLoaded(new SyndicationResourceLoadedEventArgs(navigator, source, options, userToken)); } } } //------------------------------------------------------------ // Reset load operation in progress indicator //------------------------------------------------------------ entry.LoadOperationInProgress = false; } } }
/// <summary> /// Called when a corresponding asynchronous load operation completes. /// </summary> /// <param name="result">The result of the asynchronous operation.</param> private static void AsyncLoadCallback(IAsyncResult result) { System.Text.Encoding encoding = System.Text.Encoding.UTF8; XPathNavigator navigator = null; WebRequest httpWebRequest = null; AtomEntryResource entry = null; Uri source = null; WebRequestOptions options = null; SyndicationResourceLoadSettings settings = null; if (result.IsCompleted) { object[] parameters = (object[])result.AsyncState; httpWebRequest = parameters[0] as WebRequest; entry = parameters[1] as AtomEntryResource; source = parameters[2] as Uri; settings = parameters[3] as SyndicationResourceLoadSettings; options = parameters[4] as WebRequestOptions; object userToken = parameters[5]; if (entry != null) { WebResponse httpWebResponse = (WebResponse)httpWebRequest.EndGetResponse(result); using (Stream stream = httpWebResponse.GetResponseStream()) { if (settings != null) { encoding = settings.CharacterEncoding; } using (StreamReader streamReader = new StreamReader(stream, encoding)) { XmlReaderSettings readerSettings = new XmlReaderSettings(); readerSettings.IgnoreComments = true; readerSettings.IgnoreWhitespace = true; readerSettings.DtdProcessing = DtdProcessing.Ignore; using (XmlReader reader = XmlReader.Create(streamReader, readerSettings)) { if (encoding == System.Text.Encoding.UTF8) { navigator = SyndicationEncodingUtility.CreateSafeNavigator(source, options, null); } else { navigator = SyndicationEncodingUtility.CreateSafeNavigator(source, options, settings.CharacterEncoding); } SyndicationResourceAdapter adapter = new SyndicationResourceAdapter(navigator, settings); adapter.Fill(entry, SyndicationContentFormat.Atom); AtomPublishingEditedSyndicationExtension editedExtension = entry.FindExtension(AtomPublishingEditedSyndicationExtension.MatchByType) as AtomPublishingEditedSyndicationExtension; if (editedExtension != null) { entry.EditedOn = editedExtension.Context.EditedOn; } AtomPublishingControlSyndicationExtension controlExtension = entry.FindExtension(AtomPublishingControlSyndicationExtension.MatchByType) as AtomPublishingControlSyndicationExtension; if (controlExtension != null) { entry.IsDraft = controlExtension.Context.IsDraft; } entry.OnEntryLoaded(new SyndicationResourceLoadedEventArgs(navigator, source, options, userToken)); } } } entry.LoadOperationInProgress = false; } } }