Browse Source

Post processing event is now awaitable

Former-commit-id: 79488db462d8d721315b20fffc379f14d3dbf5fd
pull/17/head
James South 12 years ago
parent
commit
e7d5cd82c2
  1. 2
      src/ImageProcessor.Web/Caching/CacheIndexer.cs
  2. 2
      src/ImageProcessor.Web/Caching/DiskCache.cs
  3. 51
      src/ImageProcessor.Web/Helpers/TaskHelpers.cs
  4. 66
      src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs
  5. 1
      src/ImageProcessor.Web/ImageProcessor.Web.csproj
  6. 8
      src/TestWebsites/MVC/Global.asax.cs
  7. 2
      src/TestWebsites/MVC/Web.config

2
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)
{

2
src/ImageProcessor.Web/Caching/DiskCache.cs

@ -193,7 +193,7 @@ namespace ImageProcessor.Web.Caching
/// </returns>
internal async Task TrimCachedFolderAsync(string path)
{
await TaskHelpers.Run(() => this.TrimCachedFolders(path));
await Task.Run(() => this.TrimCachedFolders(path));
}
#endregion

51
src/ImageProcessor.Web/Helpers/TaskHelpers.cs

@ -1,51 +0,0 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TaskHelpers.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Provides some syntactic sugar to run tasks.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Web.Helpers
{
#region Using
using System;
using System.Threading.Tasks;
#endregion
/// <summary>
/// Provides some syntactic sugar to run tasks.
/// </summary>
public sealed class TaskHelpers
{
/// <summary>
/// Queues the specified work to run on the ThreadPool and returns a Task handle for that work.
/// </summary>
/// <param name="action">The work to execute asynchronously</param>
/// <returns>A Task that represents the work queued to execute in the ThreadPool.</returns>
/// <exception cref="T:System.ArgumentNullException">
/// The <paramref name="action"/> parameter was null.
/// </exception>
public static Task Run(Action action)
{
return Task.Factory.StartNew(action);
}
/// <summary>
/// Queues the specified work to run on the ThreadPool and returns a proxy for the
/// Task(TResult) returned by <paramref name="function"/>.
/// </summary>
/// <typeparam name="TResult">The type of the result returned by the proxy Task.</typeparam>
/// <param name="function">The work to execute asynchronously</param>
/// <returns>A Task(TResult) that represents a proxy for the Task(TResult) returned by <paramref name="function"/>.</returns>
/// <exception cref="T:System.ArgumentNullException">
/// The <paramref name="function"/> parameter was null.
/// </exception>
public static Task<TResult> Run<TResult>(Func<TResult> function)
{
return Task<TResult>.Factory.StartNew(function);
}
}
}

66
src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs

@ -44,6 +44,11 @@ namespace ImageProcessor.Web.HttpModules
/// </summary>
private const string CachedResponseTypeKey = "CACHED_IMAGE_RESPONSE_TYPE_054F217C-11CF-49FF-8D2F-698E8E6EB58F";
/// <summary>
/// The key for storing the cached path of the current image.
/// </summary>
private const string CachedPathKey = "CACHED_IMAGE_PATH_TYPE_E0741478-C17B-433D-96A8-6CDA797644E9";
/// <summary>
/// The key for storing the file dependency of the current image.
/// </summary>
@ -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);
}
/// <summary>
/// Occurs when the ASP.NET event handler finishes execution.
/// </summary>
/// <param name="sender">
/// The source of the event.
/// </param>
/// <param name="e">
/// An <see cref="T:System.EventArgs">EventArgs</see> that contains the event data.
/// </param>
/// <returns>
/// The <see cref="T:System.Threading.Tasks.Task"/>.
/// </returns>
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<PostProcessingEventArgs> handler = OnPostProcessing;
if (handler != null)
{
context.Items[CachedPathKey] = null;
return Task.Run(() => handler(this, new PostProcessingEventArgs { CachedImagePath = cachedPath }));
}
}
return Task.FromResult<object>(null);
}
/// <summary>
/// Occurs just before ASP.NET send HttpHeaders to the client.
/// </summary>
@ -386,14 +428,8 @@ namespace ImageProcessor.Web.HttpModules
// Trim the cache.
await cache.TrimCachedFolderAsync(cachedPath);
// Fire the post processing event.
EventHandler<PostProcessingEventArgs> 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<string> { cachedPath };
}
@ -426,14 +462,8 @@ namespace ImageProcessor.Web.HttpModules
// Trim the cache.
await cache.TrimCachedFolderAsync(cachedPath);
// Fire the post processing event.
EventHandler<PostProcessingEventArgs> 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<string> { requestPath, cachedPath };
}

1
src/ImageProcessor.Web/ImageProcessor.Web.csproj

@ -62,7 +62,6 @@
<Compile Include="Helpers\ResourceHelpers.cs" />
<Compile Include="Helpers\ImageHelpers.cs" />
<Compile Include="Helpers\RemoteFile.cs" />
<Compile Include="Helpers\TaskHelpers.cs" />
<Compile Include="HttpModules\ImageProcessingModule.cs" />
<Compile Include="ImageFactoryExtensions.cs" />
<Compile Include="Processors\Alpha.cs" />

8
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));
}
}
}

2
src/TestWebsites/MVC/Web.config

@ -28,7 +28,7 @@
<system.web>
<httpRuntime targetFramework="4.5" />
<customErrors mode="Off"/>
<compilation debug="true" targetFramework="4.5" />
<pages>

Loading…
Cancel
Save