diff --git a/src/ImageProcessor.Web/Caching/CacheIndexer.cs b/src/ImageProcessor.Web/Caching/CacheIndexer.cs
index cd40c9439..3fee08f60 100644
--- a/src/ImageProcessor.Web/Caching/CacheIndexer.cs
+++ b/src/ImageProcessor.Web/Caching/CacheIndexer.cs
@@ -41,7 +41,7 @@ namespace ImageProcessor.Web.Caching
if (cachedImage == null)
{
- cachedImage = await TaskHelpers.Run(() => GetCachedImage(cachedPath));
+ cachedImage = await Task.Run(() => GetCachedImage(cachedPath));
if (cachedImage != null)
{
diff --git a/src/ImageProcessor.Web/Caching/DiskCache.cs b/src/ImageProcessor.Web/Caching/DiskCache.cs
index 21fdc974a..5acfc95ca 100644
--- a/src/ImageProcessor.Web/Caching/DiskCache.cs
+++ b/src/ImageProcessor.Web/Caching/DiskCache.cs
@@ -193,7 +193,7 @@ namespace ImageProcessor.Web.Caching
///
internal async Task TrimCachedFolderAsync(string path)
{
- await TaskHelpers.Run(() => this.TrimCachedFolders(path));
+ await Task.Run(() => this.TrimCachedFolders(path));
}
#endregion
diff --git a/src/ImageProcessor.Web/Helpers/TaskHelpers.cs b/src/ImageProcessor.Web/Helpers/TaskHelpers.cs
deleted file mode 100644
index 961498c09..000000000
--- a/src/ImageProcessor.Web/Helpers/TaskHelpers.cs
+++ /dev/null
@@ -1,51 +0,0 @@
-// --------------------------------------------------------------------------------------------------------------------
-//
-// Copyright (c) James South.
-// Licensed under the Apache License, Version 2.0.
-//
-//
-// Provides some syntactic sugar to run tasks.
-//
-// --------------------------------------------------------------------------------------------------------------------
-
-namespace ImageProcessor.Web.Helpers
-{
- #region Using
- using System;
- using System.Threading.Tasks;
- #endregion
-
- ///
- /// Provides some syntactic sugar to run tasks.
- ///
- public sealed class TaskHelpers
- {
- ///
- /// Queues the specified work to run on the ThreadPool and returns a Task handle for that work.
- ///
- /// The work to execute asynchronously
- /// A Task that represents the work queued to execute in the ThreadPool.
- ///
- /// The parameter was null.
- ///
- public static Task Run(Action action)
- {
- return Task.Factory.StartNew(action);
- }
-
- ///
- /// Queues the specified work to run on the ThreadPool and returns a proxy for the
- /// Task(TResult) returned by .
- ///
- /// The type of the result returned by the proxy Task.
- /// The work to execute asynchronously
- /// A Task(TResult) that represents a proxy for the Task(TResult) returned by .
- ///
- /// The parameter was null.
- ///
- public static Task Run(Func function)
- {
- return Task.Factory.StartNew(function);
- }
- }
-}
diff --git a/src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs b/src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs
index a817918c3..51adccf41 100644
--- a/src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs
+++ b/src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs
@@ -44,6 +44,11 @@ namespace ImageProcessor.Web.HttpModules
///
private const string CachedResponseTypeKey = "CACHED_IMAGE_RESPONSE_TYPE_054F217C-11CF-49FF-8D2F-698E8E6EB58F";
+ ///
+ /// The key for storing the cached path of the current image.
+ ///
+ private const string CachedPathKey = "CACHED_IMAGE_PATH_TYPE_E0741478-C17B-433D-96A8-6CDA797644E9";
+
///
/// The key for storing the file dependency of the current image.
///
@@ -134,8 +139,12 @@ namespace ImageProcessor.Web.HttpModules
preserveExifMetaData = ImageProcessorConfiguration.Instance.PreserveExifMetaData;
}
- EventHandlerTaskAsyncHelper wrapper = new EventHandlerTaskAsyncHelper(this.PostAuthorizeRequest);
- context.AddOnPostAuthorizeRequestAsync(wrapper.BeginEventHandler, wrapper.EndEventHandler);
+ EventHandlerTaskAsyncHelper postAuthorizeHelper = new EventHandlerTaskAsyncHelper(this.PostAuthorizeRequest);
+ context.AddOnPostAuthorizeRequestAsync(postAuthorizeHelper.BeginEventHandler, postAuthorizeHelper.EndEventHandler);
+
+ EventHandlerTaskAsyncHelper postProcessHelper = new EventHandlerTaskAsyncHelper(this.PostProcessImage);
+ context.AddOnPostRequestHandlerExecuteAsync(postProcessHelper.BeginEventHandler, postProcessHelper.EndEventHandler);
+
context.PreSendRequestHeaders += this.ContextPreSendRequestHeaders;
}
@@ -195,6 +204,39 @@ namespace ImageProcessor.Web.HttpModules
return this.ProcessImageAsync(context);
}
+ ///
+ /// Occurs when the ASP.NET event handler finishes execution.
+ ///
+ ///
+ /// The source of the event.
+ ///
+ ///
+ /// An EventArgs that contains the event data.
+ ///
+ ///
+ /// The .
+ ///
+ private Task PostProcessImage(object sender, EventArgs e)
+ {
+ HttpContext context = ((HttpApplication)sender).Context;
+ object cachedPathObject = context.Items[CachedPathKey];
+
+ if (cachedPathObject != null)
+ {
+ string cachedPath = cachedPathObject.ToString();
+
+ // Fire the post processing event.
+ EventHandler handler = OnPostProcessing;
+ if (handler != null)
+ {
+ context.Items[CachedPathKey] = null;
+ return Task.Run(() => handler(this, new PostProcessingEventArgs { CachedImagePath = cachedPath }));
+ }
+ }
+
+ return Task.FromResult