diff --git a/src/ImageProcessor.Web/Caching/PersistantDictionary.cs b/src/ImageProcessor.Web/Caching/PersistantDictionary.cs index 81f79c669..f20efdf82 100644 --- a/src/ImageProcessor.Web/Caching/PersistantDictionary.cs +++ b/src/ImageProcessor.Web/Caching/PersistantDictionary.cs @@ -10,6 +10,9 @@ namespace ImageProcessor.Web.Caching #region Using using System; using System.Collections.Generic; + + using ImageProcessor.Web.Helpers; + #endregion /// diff --git a/src/ImageProcessor.Web/Caching/LockedDictionary.cs b/src/ImageProcessor.Web/Helpers/LockedDictionary.cs similarity index 99% rename from src/ImageProcessor.Web/Caching/LockedDictionary.cs rename to src/ImageProcessor.Web/Helpers/LockedDictionary.cs index afa4edab4..3f41d25cc 100644 --- a/src/ImageProcessor.Web/Caching/LockedDictionary.cs +++ b/src/ImageProcessor.Web/Helpers/LockedDictionary.cs @@ -5,7 +5,7 @@ // // ----------------------------------------------------------------------- -namespace ImageProcessor.Web.Caching +namespace ImageProcessor.Web.Helpers { #region Using using System.Collections.Generic; diff --git a/src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs b/src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs index a1b968cd8..409181289 100644 --- a/src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs +++ b/src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs @@ -19,9 +19,6 @@ namespace ImageProcessor.Web.HttpModules using ImageProcessor.Web.Caching; using ImageProcessor.Web.Config; using ImageProcessor.Web.Helpers; - using System.Collections.Concurrent; - using System.Threading.Tasks; - using System.Threading; #endregion /// @@ -51,15 +48,19 @@ namespace ImageProcessor.Web.HttpModules private static readonly string AssemblyVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString(); /// - /// The thread safe fifo queue. + /// A value indicating whether the application has started. /// - private static ConcurrentQueue imageOperations; + private static bool hasModuleInitialized; + #endregion /// - /// A value indicating whether the application has started. + /// The delegate void representing the ProcessImage method. /// - private static bool hasAppStarted = false; - #endregion + /// + /// the HttpContext object that provides + /// references to the intrinsic server objects + /// + private delegate void ProcessImageDelegate(HttpContext context); #region IHttpModule Members /// @@ -72,23 +73,20 @@ namespace ImageProcessor.Web.HttpModules /// public void Init(HttpApplication context) { - if (!hasAppStarted) + if (!hasModuleInitialized) { lock (SyncRoot) { - if (!hasAppStarted) + if (!hasModuleInitialized) { - imageOperations = new ConcurrentQueue(); DiskCache.CreateCacheDirectories(); - hasAppStarted = true; + hasModuleInitialized = true; } } } - context.AddOnBeginRequestAsync(OnBeginAsync, OnEndAsync); - //context.BeginRequest += this.ContextBeginRequest; + context.AddOnBeginRequestAsync(this.OnBeginAsync, this.OnEndAsync); context.PreSendRequestHeaders += this.ContextPreSendRequestHeaders; - } /// @@ -102,28 +100,30 @@ namespace ImageProcessor.Web.HttpModules /// /// The that starts asynchronous processing - /// of the . + /// of the . /// /// The source of the event. /// /// An EventArgs that contains /// the event data. /// - /// + /// /// The delegate to call when the asynchronous method call is complete. - /// If cb is null, the delegate is not called. + /// If the callback is null, the delegate is not called. /// - /// + /// /// Any additional data needed to process the request. /// - /// - IAsyncResult OnBeginAsync(object sender, EventArgs e, AsyncCallback cb, object extraData) + /// + /// The status of the asynchronous operation. + /// + private IAsyncResult OnBeginAsync(object sender, EventArgs e, AsyncCallback callBack, object state) { HttpContext context = ((HttpApplication)sender).Context; - EnqueueDelegate enqueueDelegate = new EnqueueDelegate(Enqueue); - return enqueueDelegate.BeginInvoke(context, cb, extraData); + ProcessImageDelegate processImage = this.ProcessImage; + return processImage.BeginInvoke(context, callBack, state); } /// @@ -133,53 +133,13 @@ namespace ImageProcessor.Web.HttpModules /// The that is the result of the /// operation. /// - public void OnEndAsync(IAsyncResult result) + private void OnEndAsync(IAsyncResult result) { - // An action to consume the ConcurrentQueue. - Action action = () => + // Ensure our ProcessImage has completed in the background. + while (!result.IsCompleted) { - Action op; - - while (imageOperations.TryDequeue(out op)) - { - op(); - } - }; - - // Start 4 concurrent consuming actions. - Parallel.Invoke(action, action, action, action); - } - - /// - /// The delegate void representing the Enqueue method. - /// - /// - /// the HttpContext object that provides - /// references to the intrinsic server objects - /// - private delegate void EnqueueDelegate(HttpContext context); - - /// - /// Adds the method to the queue. - /// - /// - /// the HttpContext object that provides - /// references to the intrinsic server objects - /// - private void Enqueue(HttpContext context) - { - imageOperations.Enqueue(() => ProcessImage(context)); - } - - /// - /// Occurs as the first event in the HTTP pipeline chain of execution when ASP.NET responds to a request. - /// - /// The source of the event. - /// An EventArgs that contains the event data. - private void ContextBeginRequest(object sender, EventArgs e) - { - HttpContext context = ((HttpApplication)sender).Context; - imageOperations.Enqueue(() => ProcessImage(context)); + System.Threading.Thread.Sleep(1); + } } /// @@ -197,6 +157,7 @@ namespace ImageProcessor.Web.HttpModules { string responseType = (string)responseTypeObject; + // Set the headers this.SetHeaders(context, responseType); context.Items[CachedResponseTypeKey] = null; @@ -270,21 +231,21 @@ namespace ImageProcessor.Web.HttpModules { //lock (SyncRoot) //{ - // Trim the cache. - DiskCache.TrimCachedFolders(); + // Trim the cache. + DiskCache.TrimCachedFolders(); - responseStream.CopyTo(memoryStream); + responseStream.CopyTo(memoryStream); - imageFactory.Load(memoryStream) - .AddQueryString(queryString) - .Format(ImageUtils.GetImageFormat(imageName)) - .AutoProcess().Save(cachedPath); + imageFactory.Load(memoryStream) + .AddQueryString(queryString) + .Format(ImageUtils.GetImageFormat(imageName)) + .AutoProcess().Save(cachedPath); - // Ensure that the LastWriteTime property of the source and cached file match. - DateTime dateTime = DiskCache.SetCachedLastWriteTime(path, cachedPath, true); + // Ensure that the LastWriteTime property of the source and cached file match. + DateTime dateTime = DiskCache.SetCachedLastWriteTime(path, cachedPath, true); - // Add to the cache. - DiskCache.AddImageToCache(cachedPath, dateTime); + // Add to the cache. + DiskCache.AddImageToCache(cachedPath, dateTime); //} } } @@ -294,16 +255,16 @@ namespace ImageProcessor.Web.HttpModules { //lock (SyncRoot) //{ - // Trim the cache. - DiskCache.TrimCachedFolders(); + // Trim the cache. + DiskCache.TrimCachedFolders(); - imageFactory.Load(fullPath).AutoProcess().Save(cachedPath); + imageFactory.Load(fullPath).AutoProcess().Save(cachedPath); - // Ensure that the LastWriteTime property of the source and cached file match. - DateTime dateTime = DiskCache.SetCachedLastWriteTime(path, cachedPath, false); + // Ensure that the LastWriteTime property of the source and cached file match. + DateTime dateTime = DiskCache.SetCachedLastWriteTime(path, cachedPath, false); - // Add to the cache. - DiskCache.AddImageToCache(cachedPath, dateTime); + // Add to the cache. + DiskCache.AddImageToCache(cachedPath, dateTime); //} } } diff --git a/src/ImageProcessor.Web/ImageProcessor.Web.csproj b/src/ImageProcessor.Web/ImageProcessor.Web.csproj index 405a17081..0ab6f04c4 100644 --- a/src/ImageProcessor.Web/ImageProcessor.Web.csproj +++ b/src/ImageProcessor.Web/ImageProcessor.Web.csproj @@ -88,7 +88,7 @@ - + diff --git a/src/ImageProcessor.Web/Properties/AssemblyInfo.cs b/src/ImageProcessor.Web/Properties/AssemblyInfo.cs index a2f13edc1..7ecd2ad29 100644 --- a/src/ImageProcessor.Web/Properties/AssemblyInfo.cs +++ b/src/ImageProcessor.Web/Properties/AssemblyInfo.cs @@ -31,5 +31,5 @@ using System.Runtime.InteropServices; // // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: -[assembly: AssemblyVersion("2.0.0.0")] -[assembly: AssemblyFileVersion("2.0.0.0")] +[assembly: AssemblyVersion("2.0.1.0")] +[assembly: AssemblyFileVersion("2.0.1.0")] diff --git a/src/Nuget/ImageProcessor.Web.2.0.1.0.nupkg b/src/Nuget/ImageProcessor.Web.2.0.1.0.nupkg new file mode 100644 index 000000000..cbd19db4a Binary files /dev/null and b/src/Nuget/ImageProcessor.Web.2.0.1.0.nupkg differ