Remove feed reading async code

Update to new binding expression code on options dialog
Update to .NET framework 4.5.1
This commit is contained in:
2014-07-11 20:40:47 -04:00
parent 6cdc334d53
commit 7e3eedd844
8 changed files with 63 additions and 83 deletions

View File

@@ -1,18 +1,13 @@
using System;
using System.Collections.Generic;
using Common.Debug;
using Common.Xml;
using FeedCenter.FeedParsers;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using Common.Debug;
using Common.Extensions;
using Common.Xml;
using FeedCenter.Data;
using FeedCenter.FeedParsers;
using System.Threading.Tasks;
namespace FeedCenter
{
#region Enumerations
@@ -73,7 +68,7 @@ namespace FeedCenter
Tracer.WriteLine("Reading feed: {0}", Source);
Tracer.IncrementIndentLevel();
FeedReadResult result = ReadFeed(database, forceRead);
var result = ReadFeed(database, forceRead);
// Handle the result
switch (result)
@@ -92,7 +87,7 @@ namespace FeedCenter
}
// If the feed was successfully read and we have no last update timestamp - set the last update timestamp to now
if (result == FeedReadResult.Success && LastUpdated == FeedCenter.Data.Extensions.SqlDateTimeZero.Value)
if (result == FeedReadResult.Success && LastUpdated == Data.Extensions.SqlDateTimeZero.Value)
LastUpdated = DateTime.Now;
Tracer.DecrementIndentLevel();
@@ -101,25 +96,28 @@ namespace FeedCenter
return result;
}
private async Task<Tuple<FeedReadResult, string>> RetrieveFeed()
private Tuple<FeedReadResult, string> RetrieveFeed()
{
try
{
// Create the web request
WebRequest oRequest = WebRequest.Create(new Uri(Source));
var oRequest = WebRequest.Create(new Uri(Source));
// Attempt to cast to a web request
var webRequest = oRequest as HttpWebRequest;
// If this is an http request set some special properties
if (oRequest is HttpWebRequest)
if (webRequest != null)
{
// Cast the request
HttpWebRequest webRequest = (HttpWebRequest) oRequest;
// Make sure to use HTTP version 1.1
webRequest.ProtocolVersion = HttpVersion.Version11;
// Set that we'll accept compressed data
webRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
// Set a timeout
webRequest.Timeout = 10000;
// If we need to authenticate then set the credentials
if (Authenticate)
webRequest.Credentials = new NetworkCredential(Username, Password, Domain);
@@ -129,21 +127,17 @@ namespace FeedCenter
}
// Set the default encoding
Encoding encoding = Encoding.UTF8;
// Attempt to get the response
var response = (HttpWebResponse) await oRequest.GetResponseAsync().WithTimeout(10000).ConfigureAwait(false);
var encoding = Encoding.UTF8;
// If there was no response assume it was a timeout of the async method
if (response == null)
return Tuple.Create(FeedReadResult.Timeout, string.Empty);
// Attempt to get the response
var response = (HttpWebResponse) oRequest.GetResponse();
// If the response included an encoding then change the encoding
if (response.ContentEncoding.Length > 0)
encoding = Encoding.GetEncoding(response.ContentEncoding);
// Get the response stream
Stream responseStream = response.GetResponseStream();
var responseStream = response.GetResponseStream();
if (responseStream == null)
return Tuple.Create(FeedReadResult.NoResponse, string.Empty);
@@ -152,7 +146,7 @@ namespace FeedCenter
StreamReader textReader = new XmlSanitizingStream(responseStream, encoding);
// Get the feed text
string feedText = textReader.ReadToEnd();
var feedText = textReader.ReadToEnd();
// Get rid of any leading and trailing whitespace
feedText = feedText.Trim();
@@ -170,12 +164,12 @@ namespace FeedCenter
}
catch (WebException webException)
{
FeedReadResult result = FeedReadResult.UnknownError;
var result = FeedReadResult.UnknownError;
if (webException.Response is HttpWebResponse)
var errorResponse = webException.Response as HttpWebResponse;
if (errorResponse != null)
{
HttpWebResponse errorResponse = (HttpWebResponse) webException.Response;
switch (errorResponse.StatusCode)
{
case HttpStatusCode.InternalServerError:
@@ -224,8 +218,6 @@ namespace FeedCenter
{
try
{
string feedText;
// If not enabled then do nothing
if (!Enabled)
return FeedReadResult.NotEnabled;
@@ -234,7 +226,7 @@ namespace FeedCenter
if (!forceRead)
{
// Figure out how long since we last checked
TimeSpan timeSpan = DateTime.Now - LastChecked;
var timeSpan = DateTime.Now - LastChecked;
// Check if we are due to read the feed
if (timeSpan.TotalMinutes < CheckInterval)
@@ -247,19 +239,16 @@ namespace FeedCenter
// Read the feed text
var retrieveResult = RetrieveFeed();
// Wait on the result
retrieveResult.Wait();
// Get the information out of the async result
FeedReadResult result = retrieveResult.Result.Item1;
feedText = retrieveResult.Result.Item2;
var result = retrieveResult.Item1;
var feedText = retrieveResult.Item2;
// If we didn't successfully retrieve the feed then stop
if (result != FeedReadResult.Success)
return result;
// Create a new RSS parser
FeedParserBase feedParser = FeedParserBase.CreateFeedParser(this, feedText);
var feedParser = FeedParserBase.CreateFeedParser(this, feedText);
// Parse the feed
result = feedParser.ParseFeed(feedText);
@@ -269,14 +258,14 @@ namespace FeedCenter
return result;
// Create the removed items list - if an item wasn't seen during this check then remove it
List<FeedItem> removedItems = Items.Where(testItem => testItem.LastFound != LastChecked).ToList();
var removedItems = Items.Where(testItem => testItem.LastFound != LastChecked).ToList();
// If items were removed the feed was updated
if (removedItems.Count > 0)
LastUpdated = DateTime.Now;
// Loop over the items to be removed
foreach (FeedItem itemToRemove in removedItems)
foreach (var itemToRemove in removedItems)
{
// Delete the item from the database
database.FeedItems.DeleteObject(itemToRemove);
@@ -308,7 +297,7 @@ namespace FeedCenter
{
var sortedActions = from action in Actions orderby action.Sequence ascending select action;
foreach (FeedAction feedAction in sortedActions)
foreach (var feedAction in sortedActions)
{
switch (feedAction.Field)
{
@@ -329,10 +318,10 @@ namespace FeedCenter
var lastReadResult = (FeedReadResult) LastReadResult;
// Build the name of the resource using the enum name and the value
string resourceName = string.Format("{0}_{1}", typeof(FeedReadResult).Name, lastReadResult);
var resourceName = string.Format("{0}_{1}", typeof(FeedReadResult).Name, lastReadResult);
// Try to get the value from the resources
string resourceValue = Properties.Resources.ResourceManager.GetString(resourceName);
var resourceValue = Properties.Resources.ResourceManager.GetString(resourceName);
// Return the value or just the enum value if not found
return resourceValue ?? lastReadResult.ToString();