From e7d5cd82c2e117e5ce72fa4a96ed3bd5dd2ca55e Mon Sep 17 00:00:00 2001 From: James South Date: Sun, 17 Aug 2014 12:45:43 +0100 Subject: [PATCH] Post processing event is now awaitable Former-commit-id: 79488db462d8d721315b20fffc379f14d3dbf5fd --- .../Caching/CacheIndexer.cs | 2 +- src/ImageProcessor.Web/Caching/DiskCache.cs | 2 +- src/ImageProcessor.Web/Helpers/TaskHelpers.cs | 51 -------------- .../HttpModules/ImageProcessingModule.cs | 66 ++++++++++++++----- .../ImageProcessor.Web.csproj | 1 - src/TestWebsites/MVC/Global.asax.cs | 8 +++ src/TestWebsites/MVC/Web.config | 2 +- 7 files changed, 59 insertions(+), 73 deletions(-) delete mode 100644 src/ImageProcessor.Web/Helpers/TaskHelpers.cs 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(null); + } + /// /// Occurs just before ASP.NET send HttpHeaders to the client. /// @@ -386,14 +428,8 @@ namespace ImageProcessor.Web.HttpModules // Trim the cache. await cache.TrimCachedFolderAsync(cachedPath); - // Fire the post processing event. - EventHandler handler = OnPostProcessing; - if (handler != null) - { - handler.Invoke(this, new PostProcessingEventArgs() { CachedImagePath = cachedPath }); - } - - // Store the response type and cache dependency in the context for later retrieval. + // Store the cached path, response type, and cache dependency in the context for later retrieval. + context.Items[CachedPathKey] = cachedPath; context.Items[CachedResponseTypeKey] = imageFactory.CurrentImageFormat.MimeType; context.Items[CachedResponseFileDependency] = new List { cachedPath }; } @@ -426,14 +462,8 @@ namespace ImageProcessor.Web.HttpModules // Trim the cache. await cache.TrimCachedFolderAsync(cachedPath); - // Fire the post processing event. - EventHandler handler = OnPostProcessing; - if (handler != null) - { - handler.Invoke(this, new PostProcessingEventArgs() { CachedImagePath = cachedPath }); - } - - // Store the response type and cache dependencies in the context for later retrieval. + // Store the cached path, response type, and cache dependencies in the context for later retrieval. + context.Items[CachedPathKey] = cachedPath; context.Items[CachedResponseTypeKey] = imageFactory.CurrentImageFormat.MimeType; context.Items[CachedResponseFileDependency] = new List { requestPath, cachedPath }; } diff --git a/src/ImageProcessor.Web/ImageProcessor.Web.csproj b/src/ImageProcessor.Web/ImageProcessor.Web.csproj index e4e20c719..47b69f8ac 100644 --- a/src/ImageProcessor.Web/ImageProcessor.Web.csproj +++ b/src/ImageProcessor.Web/ImageProcessor.Web.csproj @@ -62,7 +62,6 @@ - diff --git a/src/TestWebsites/MVC/Global.asax.cs b/src/TestWebsites/MVC/Global.asax.cs index 87315956d..54afb402f 100644 --- a/src/TestWebsites/MVC/Global.asax.cs +++ b/src/TestWebsites/MVC/Global.asax.cs @@ -9,7 +9,9 @@ using System.Web.Routing; namespace Test_Website_NET45 { using System.Diagnostics; + using System.Threading.Tasks; + using ImageProcessor.Web.Helpers; using ImageProcessor.Web.HttpModules; // Note: For instructions on enabling IIS6 or IIS7 classic mode, @@ -26,6 +28,12 @@ namespace Test_Website_NET45 // Test the post processing event. ImageProcessingModule.OnPostProcessing += (sender, args) => Debug.WriteLine(args.CachedImagePath); + //ImageProcessingModule.OnPostProcessing += this.WritePath; + } + + private async void WritePath(object sender, PostProcessingEventArgs e) + { + await Task.Run(() => Debug.WriteLine(e.CachedImagePath)); } } } \ No newline at end of file diff --git a/src/TestWebsites/MVC/Web.config b/src/TestWebsites/MVC/Web.config index 8cfc303d3..3b4f02f51 100644 --- a/src/TestWebsites/MVC/Web.config +++ b/src/TestWebsites/MVC/Web.config @@ -28,7 +28,7 @@ - +