private object ProcessResourceSafely(Assembly callingAssembly, string name, string resourceUrl, string contentType, int requiredFreshnessDays, int timeoutMs, ResourceFileProcessor processor) { if (contentType == MimeHelper.TEXT_XML) { processor = new ResourceFileProcessor(new XPPResourceFileProcessor(processor).ProcessXmlResource); } // attempt to load and process the resource (potentially loading new data from the network) try { using (Stream stream = GetResource(callingAssembly, name, resourceUrl, contentType, requiredFreshnessDays, timeoutMs)) { return(processor(stream)); } // verify content type of responding url } catch (Exception ex) { // report failure Trace.WriteLine(String.Format(CultureInfo.InvariantCulture, "Unexpected exception occurred while processing resource {0}: {1}", name, ex.ToString())); // NOTE: we should save the last successfully download/used file and // "restore" from this rather than the embedded copy in the case of // an error or failure reading from the downloaded file. // safely delete the locally cached file (it contains bad data) SafeDeleteLocalCacheFile(callingAssembly, name); // try again on local data using (Stream stream = GetResourceLocal(callingAssembly, name)) return(processor(stream)); } }
/// <summary> /// Trims the working set after a given delay /// </summary> /// <param name="delayMs">The delay prior to trimming the working set</param> /// <returns></returns> public DelayUpdateHelper(Assembly callingAssembly, string name, string url, string contentType, ResourceFileDownloader downloader, ResourceFileProcessor processor, int delayMs) { _callingAssembly = callingAssembly; _name = name; _url = url; _contentType = contentType; _processor = processor; _downloader = downloader; _delayMs = delayMs; }
public object ProcessLocalResourceSafelyAndRefresh(string name, string url, string contentType, ResourceFileProcessor processor, int delayMs) { Assembly assembly = Assembly.GetCallingAssembly(); lock (_downloadedResources.SyncRoot) { if (!_downloadedResources.Contains(url)) { _downloadedResources.Add(url); new DelayUpdateHelper(assembly, name, url, contentType, this, processor, delayMs).StartBackgroundUpdate(); } } return ProcessResourceSafely(assembly, name, string.Empty, contentType, REQUIREDFRESHNESSDAYS, TIMEOUTMS, processor); }
public XPPResourceFileProcessor(ResourceFileProcessor processor) { _processor = processor; }
private object ProcessResourceSafely(Assembly callingAssembly, string name, string resourceUrl, string contentType, int requiredFreshnessDays, int timeoutMs, ResourceFileProcessor processor) { if (contentType == MimeHelper.TEXT_XML) processor = new ResourceFileProcessor(new XPPResourceFileProcessor(processor).ProcessXmlResource); // attempt to load and process the resource (potentially loading new data from the network) try { using (Stream stream = GetResource(callingAssembly, name, resourceUrl, contentType, requiredFreshnessDays, timeoutMs)) { return processor(stream); } // verify content type of responding url } catch (Exception ex) { // report failure Trace.WriteLine(String.Format(CultureInfo.InvariantCulture, "Unexpected exception occurred while processing resource {0}: {1}", name, ex.ToString())); // NOTE: we should save the last successfully download/used file and // "restore" from this rather than the embedded copy in the case of // an error or failure reading from the downloaded file. // safely delete the locally cached file (it contains bad data) SafeDeleteLocalCacheFile(callingAssembly, name); // try again on local data using (Stream stream = GetResourceLocal(callingAssembly, name)) return processor(stream); } }
/// <summary> /// Get and process a resource (see comment on GetResource for more info on resources) /// This method attempts to load the resource normally (i.e. from the network). If /// an exception of any kind happens during processing we revert back to processing /// the version built into the assembly. /// </summary> /// <param name="name">resource name</param> /// <param name="requiredFreshnessDays">number of days for which we can use cached copies</param> /// <param name="timeoutMs">timeout in ms for request to web server</param> /// <param name="processor">callback used for processing the resource</param> /// <returns>result of processing</returns> public object ProcessResourceSafely(string name, string resourceUrl, string contentType, int requiredFreshnessDays, int timeoutMs, ResourceFileProcessor processor) { // record calling assembly return ProcessResourceSafely(Assembly.GetCallingAssembly(), name, resourceUrl, contentType, requiredFreshnessDays, timeoutMs, processor); }
public object ProcessLocalResourceSafelyAndRefresh(string name, string url, string contentType, ResourceFileProcessor processor, int delayMs) { Assembly assembly = Assembly.GetCallingAssembly(); lock (_downloadedResources.SyncRoot) { if (!_downloadedResources.Contains(url)) { _downloadedResources.Add(url); new DelayUpdateHelper(assembly, name, url, contentType, this, processor, delayMs).StartBackgroundUpdate(); } } return(ProcessResourceSafely(assembly, name, string.Empty, contentType, REQUIREDFRESHNESSDAYS, TIMEOUTMS, processor)); }
/// <summary> /// Get and process a resource (see comment on GetResource for more info on resources) /// This method attempts to load the resource normally (i.e. from the network). If /// an exception of any kind happens during processing we revert back to processing /// the version built into the assembly. /// </summary> /// <param name="name">resource name</param> /// <param name="requiredFreshnessDays">number of days for which we can use cached copies</param> /// <param name="timeoutMs">timeout in ms for request to web server</param> /// <param name="processor">callback used for processing the resource</param> /// <returns>result of processing</returns> public object ProcessResourceSafely(string name, string resourceUrl, string contentType, int requiredFreshnessDays, int timeoutMs, ResourceFileProcessor processor) { // record calling assembly return(ProcessResourceSafely(Assembly.GetCallingAssembly(), name, resourceUrl, contentType, requiredFreshnessDays, timeoutMs, processor)); }