2 Commits

Author SHA1 Message Date
59bb8ae3cd Make sure to dispose resources created during feed reading 2018-07-17 12:02:53 -04:00
840bd1acd0 Remove post-build steps 2018-04-06 13:10:58 -04:00
2 changed files with 71 additions and 62 deletions

View File

@@ -1,7 +1,4 @@
using Common.Debug; using System;
using Common.Xml;
using FeedCenter.FeedParsers;
using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@@ -9,7 +6,12 @@ using System.Net;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using Common.Debug;
using Common.Update; using Common.Update;
using Common.Xml;
using FeedCenter.Data;
using FeedCenter.FeedParsers;
using FeedCenter.Properties;
namespace FeedCenter namespace FeedCenter
{ {
@@ -55,6 +57,25 @@ namespace FeedCenter
public partial class Feed public partial class Feed
{ {
// ReSharper disable once UnusedMember.Global
public string LastReadResultDescription
{
get
{
// Cast the last read result to the proper enum
var lastReadResult = LastReadResult;
// Build the name of the resource using the enum name and the value
var resourceName = $"{typeof(FeedReadResult).Name}_{lastReadResult}";
// Try to get the value from the resources
var resourceValue = Resources.ResourceManager.GetString(resourceName);
// Return the value or just the enum value if not found
return resourceValue ?? lastReadResult.ToString();
}
}
public static Feed Create(FeedCenterEntities database) public static Feed Create(FeedCenterEntities database)
{ {
return new Feed { ID = Guid.NewGuid(), CategoryID = database.DefaultCategory.ID }; return new Feed { ID = Guid.NewGuid(), CategoryID = database.DefaultCategory.ID };
@@ -75,6 +96,7 @@ namespace FeedCenter
case FeedReadResult.NotDue: case FeedReadResult.NotDue:
case FeedReadResult.NotEnabled: case FeedReadResult.NotEnabled:
case FeedReadResult.NotModified: case FeedReadResult.NotModified:
// Ignore // Ignore
break; break;
@@ -86,7 +108,7 @@ namespace FeedCenter
} }
// If the feed was successfully read and we have no last update timestamp - set the last update timestamp to now // 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 == Data.Extensions.SqlDateTimeZero.Value) if (result == FeedReadResult.Success && LastUpdated == Extensions.SqlDateTimeZero.Value)
LastUpdated = DateTime.Now; LastUpdated = DateTime.Now;
Tracer.DecrementIndentLevel(); Tracer.DecrementIndentLevel();
@@ -120,13 +142,10 @@ namespace FeedCenter
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
// Create the web request // Create the web request
var oRequest = WebRequest.Create(new Uri(Source)); var request = 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 this is an http request set some special properties
if (webRequest != null) if (request is HttpWebRequest webRequest)
{ {
// Make sure to use HTTP version 1.1 // Make sure to use HTTP version 1.1
webRequest.ProtocolVersion = HttpVersion.Version11; webRequest.ProtocolVersion = HttpVersion.Version11;
@@ -137,36 +156,39 @@ namespace FeedCenter
// Set a timeout // Set a timeout
webRequest.Timeout = 10000; webRequest.Timeout = 10000;
// Make sure the service point closes the connection right away
webRequest.ServicePoint.ConnectionLeaseTimeout = 0;
// If we need to authenticate then set the credentials // If we need to authenticate then set the credentials
if (Authenticate) if (Authenticate)
webRequest.Credentials = new NetworkCredential(Username, Password, Domain); webRequest.Credentials = new NetworkCredential(Username, Password, Domain);
// Set a user agent string // Set a user agent string
if (string.IsNullOrWhiteSpace(Properties.Settings.Default.DefaultUserAgent)) if (string.IsNullOrWhiteSpace(Settings.Default.DefaultUserAgent))
webRequest.UserAgent = "FeedCenter/" + UpdateCheck.LocalVersion; webRequest.UserAgent = "FeedCenter/" + UpdateCheck.LocalVersion;
else else
webRequest.UserAgent = Properties.Settings.Default.DefaultUserAgent; webRequest.UserAgent = Settings.Default.DefaultUserAgent;
} }
// Set the default encoding // Set the default encoding
var encoding = Encoding.UTF8; var encoding = Encoding.UTF8;
// Attempt to get the response // Attempt to get the response
var response = (HttpWebResponse) oRequest.GetResponse(); using (var response = (HttpWebResponse) request.GetResponse())
{
// If the response included an encoding then change the encoding // If the response included an encoding then change the encoding
if (response.ContentEncoding.Length > 0) if (response.ContentEncoding.Length > 0)
encoding = Encoding.GetEncoding(response.ContentEncoding); encoding = Encoding.GetEncoding(response.ContentEncoding);
// Get the response stream // Get the response stream
var responseStream = response.GetResponseStream(); using (var responseStream = response.GetResponseStream())
{
if (responseStream == null) if (responseStream == null)
return Tuple.Create(FeedReadResult.NoResponse, string.Empty); return Tuple.Create(FeedReadResult.NoResponse, string.Empty);
// Create the text reader // Create the text reader
StreamReader textReader = new XmlSanitizingStream(responseStream, encoding); using (StreamReader textReader = new XmlSanitizingStream(responseStream, encoding))
{
// Get the feed text // Get the feed text
var feedText = textReader.ReadToEnd(); var feedText = textReader.ReadToEnd();
@@ -182,6 +204,9 @@ namespace FeedCenter
return Tuple.Create(FeedReadResult.Success, feedText); return Tuple.Create(FeedReadResult.Success, feedText);
} }
}
}
}
catch (IOException ioException) catch (IOException ioException)
{ {
Tracer.WriteLine(ioException.Message); Tracer.WriteLine(ioException.Message);
@@ -192,23 +217,25 @@ namespace FeedCenter
{ {
var result = FeedReadResult.UnknownError; var result = FeedReadResult.UnknownError;
var errorResponse = webException.Response as HttpWebResponse; if (webException.Response is HttpWebResponse errorResponse)
if (errorResponse != null)
{ {
switch (errorResponse.StatusCode) switch (errorResponse.StatusCode)
{ {
case HttpStatusCode.InternalServerError: case HttpStatusCode.InternalServerError:
return Tuple.Create(FeedReadResult.ServerError, string.Empty); return Tuple.Create(FeedReadResult.ServerError, string.Empty);
case HttpStatusCode.NotModified: case HttpStatusCode.NotModified:
return Tuple.Create(FeedReadResult.NotModified, string.Empty); return Tuple.Create(FeedReadResult.NotModified, string.Empty);
case HttpStatusCode.NotFound: case HttpStatusCode.NotFound:
return Tuple.Create(FeedReadResult.NotFound, string.Empty); return Tuple.Create(FeedReadResult.NotFound, string.Empty);
case HttpStatusCode.Unauthorized: case HttpStatusCode.Unauthorized:
case HttpStatusCode.Forbidden: case HttpStatusCode.Forbidden:
return Tuple.Create(FeedReadResult.Unauthorized, string.Empty); return Tuple.Create(FeedReadResult.Unauthorized, string.Empty);
} }
} }
@@ -218,10 +245,12 @@ namespace FeedCenter
case WebExceptionStatus.ConnectFailure: case WebExceptionStatus.ConnectFailure:
case WebExceptionStatus.NameResolutionFailure: case WebExceptionStatus.NameResolutionFailure:
result = FeedReadResult.ConnectionFailed; result = FeedReadResult.ConnectionFailed;
break; break;
case WebExceptionStatus.Timeout: case WebExceptionStatus.Timeout:
result = FeedReadResult.Timeout; result = FeedReadResult.Timeout;
break; break;
} }
@@ -321,7 +350,7 @@ namespace FeedCenter
private void ProcessActions() private void ProcessActions()
{ {
var sortedActions = from action in Actions orderby action.Sequence ascending select action; var sortedActions = from action in Actions orderby action.Sequence select action;
foreach (var feedAction in sortedActions) foreach (var feedAction in sortedActions)
{ {
@@ -329,30 +358,12 @@ namespace FeedCenter
{ {
case 0: case 0:
Title = Title.Replace(feedAction.Search, feedAction.Replace); Title = Title.Replace(feedAction.Search, feedAction.Replace);
break; break;
} }
} }
} }
#endregion #endregion
// ReSharper disable once UnusedMember.Global
public string LastReadResultDescription
{
get
{
// Cast the last read result to the proper enum
var lastReadResult = LastReadResult;
// Build the name of the resource using the enum name and the value
var resourceName = $"{typeof(FeedReadResult).Name}_{lastReadResult}";
// Try to get the value from the resources
var resourceValue = Properties.Resources.ResourceManager.GetString(resourceName);
// Return the value or just the enum value if not found
return resourceValue ?? lastReadResult.ToString();
}
}
} }
} }

View File

@@ -61,9 +61,7 @@
</ItemGroup> </ItemGroup>
<Import Project="$(WixTargetsPath)" /> <Import Project="$(WixTargetsPath)" />
<PropertyGroup> <PropertyGroup>
<PostBuildEvent>rem D:\Code\Personal\CreateInstallDescriptor\bin\Debug\CreateInstallDescriptor.exe !(TargetPath) $(TargetDir)\$(TargetName).xml <PostBuildEvent />
rem copy $(TargetDir)\$(TargetName).xml \\server\d\FeedCenter
rem copy $(TargetDir)\$(TargetName).exe \\server\d\FeedCenter</PostBuildEvent>
</PropertyGroup> </PropertyGroup>
<!-- <!--
To modify your build process, add your task inside one of the targets below and uncomment it. To modify your build process, add your task inside one of the targets below and uncomment it.