@@ -19,9 +19,6 @@ namespace ImageProcessor.Web.HttpModules
1919 using ImageProcessor . Web . Caching ;
2020 using ImageProcessor . Web . Config ;
2121 using ImageProcessor . Web . Helpers ;
22- using System . Collections . Concurrent ;
23- using System . Threading . Tasks ;
24- using System . Threading ;
2522 #endregion
2623
2724 /// <summary>
@@ -51,15 +48,19 @@ public class ImageProcessingModule : IHttpModule
5148 private static readonly string AssemblyVersion = Assembly . GetExecutingAssembly ( ) . GetName ( ) . Version . ToString ( ) ;
5249
5350 /// <summary>
54- /// The thread safe fifo queue .
51+ /// A value indicating whether the application has started .
5552 /// </summary>
56- private static ConcurrentQueue < Action > imageOperations ;
53+ private static bool hasModuleInitialized ;
54+ #endregion
5755
5856 /// <summary>
59- /// A value indicating whether the application has started .
57+ /// The delegate void representing the ProcessImage method .
6058 /// </summary>
61- private static bool hasAppStarted = false ;
62- #endregion
59+ /// <param name="context">
60+ /// the <see cref="T:System.Web.HttpContext">HttpContext</see> object that provides
61+ /// references to the intrinsic server objects
62+ /// </param>
63+ private delegate void ProcessImageDelegate ( HttpContext context ) ;
6364
6465 #region IHttpModule Members
6566 /// <summary>
@@ -72,23 +73,20 @@ public class ImageProcessingModule : IHttpModule
7273 /// </param>
7374 public void Init ( HttpApplication context )
7475 {
75- if ( ! hasAppStarted )
76+ if ( ! hasModuleInitialized )
7677 {
7778 lock ( SyncRoot )
7879 {
79- if ( ! hasAppStarted )
80+ if ( ! hasModuleInitialized )
8081 {
81- imageOperations = new ConcurrentQueue < Action > ( ) ;
8282 DiskCache . CreateCacheDirectories ( ) ;
83- hasAppStarted = true ;
83+ hasModuleInitialized = true ;
8484 }
8585 }
8686 }
8787
88- context . AddOnBeginRequestAsync ( OnBeginAsync , OnEndAsync ) ;
89- //context.BeginRequest += this.ContextBeginRequest;
88+ context . AddOnBeginRequestAsync ( this . OnBeginAsync , this . OnEndAsync ) ;
9089 context . PreSendRequestHeaders += this . ContextPreSendRequestHeaders ;
91-
9290 }
9391
9492 /// <summary>
@@ -102,28 +100,30 @@ public void Dispose()
102100
103101 /// <summary>
104102 /// The <see cref="T:System.Web.BeginEventHandler"/> that starts asynchronous processing
105- /// of the <see cref="T: System.Web.HttpApplication.BeginRequest"/>.
103+ /// of the <see cref="System.Web.HttpApplication.BeginRequest"/>.
106104 /// </summary>
107105 /// <param name="sender">The source of the event.</param>
108106 /// <param name="e">
109107 /// An <see cref="T:System.EventArgs">EventArgs</see> that contains
110108 /// the event data.
111109 /// </param>
112- /// <param name="cb ">
110+ /// <param name="callBack ">
113111 /// The delegate to call when the asynchronous method call is complete.
114- /// If cb is null, the delegate is not called.
112+ /// If the callback is null, the delegate is not called.
115113 /// </param>
116- /// <param name="extraData ">
114+ /// <param name="state ">
117115 /// Any additional data needed to process the request.
118116 /// </param>
119- /// <returns></returns>
120- IAsyncResult OnBeginAsync ( object sender , EventArgs e , AsyncCallback cb , object extraData )
117+ /// <returns>
118+ /// The status of the asynchronous operation.
119+ /// </returns>
120+ private IAsyncResult OnBeginAsync ( object sender , EventArgs e , AsyncCallback callBack , object state )
121121 {
122122 HttpContext context = ( ( HttpApplication ) sender ) . Context ;
123- EnqueueDelegate enqueueDelegate = new EnqueueDelegate ( Enqueue ) ;
124123
125- return enqueueDelegate . BeginInvoke ( context , cb , extraData ) ;
124+ ProcessImageDelegate processImage = this . ProcessImage ;
126125
126+ return processImage . BeginInvoke ( context , callBack , state ) ;
127127 }
128128
129129 /// <summary>
@@ -133,53 +133,13 @@ IAsyncResult OnBeginAsync(object sender, EventArgs e, AsyncCallback cb, object e
133133 /// The <see cref="T:System.IAsyncResult"/> that is the result of the
134134 /// <see cref="T:System.Web.BeginEventHandler"/> operation.
135135 /// </param>
136- public void OnEndAsync ( IAsyncResult result )
136+ private void OnEndAsync ( IAsyncResult result )
137137 {
138- // An action to consume the ConcurrentQueue .
139- Action action = ( ) =>
138+ // Ensure our ProcessImage has completed in the background .
139+ while ( ! result . IsCompleted )
140140 {
141- Action op ;
142-
143- while ( imageOperations . TryDequeue ( out op ) )
144- {
145- op ( ) ;
146- }
147- } ;
148-
149- // Start 4 concurrent consuming actions.
150- Parallel . Invoke ( action , action , action , action ) ;
151- }
152-
153- /// <summary>
154- /// The delegate void representing the Enqueue method.
155- /// </summary>
156- /// <param name="context">
157- /// the <see cref="T:System.Web.HttpContext">HttpContext</see> object that provides
158- /// references to the intrinsic server objects
159- /// </param>
160- private delegate void EnqueueDelegate ( HttpContext context ) ;
161-
162- /// <summary>
163- /// Adds the method to the queue.
164- /// </summary>
165- /// <param name="context">
166- /// the <see cref="T:System.Web.HttpContext">HttpContext</see> object that provides
167- /// references to the intrinsic server objects
168- /// </param>
169- private void Enqueue ( HttpContext context )
170- {
171- imageOperations . Enqueue ( ( ) => ProcessImage ( context ) ) ;
172- }
173-
174- /// <summary>
175- /// Occurs as the first event in the HTTP pipeline chain of execution when ASP.NET responds to a request.
176- /// </summary>
177- /// <param name="sender">The source of the event.</param>
178- /// <param name="e">An <see cref="T:System.EventArgs">EventArgs</see> that contains the event data.</param>
179- private void ContextBeginRequest ( object sender , EventArgs e )
180- {
181- HttpContext context = ( ( HttpApplication ) sender ) . Context ;
182- imageOperations . Enqueue ( ( ) => ProcessImage ( context ) ) ;
141+ System . Threading . Thread . Sleep ( 1 ) ;
142+ }
183143 }
184144
185145 /// <summary>
@@ -197,6 +157,7 @@ private void ContextPreSendRequestHeaders(object sender, EventArgs e)
197157 {
198158 string responseType = ( string ) responseTypeObject ;
199159
160+ // Set the headers
200161 this . SetHeaders ( context , responseType ) ;
201162
202163 context . Items [ CachedResponseTypeKey ] = null ;
@@ -270,21 +231,21 @@ private void ProcessImage(HttpContext context)
270231 {
271232 //lock (SyncRoot)
272233 //{
273- // Trim the cache.
274- DiskCache . TrimCachedFolders ( ) ;
234+ // Trim the cache.
235+ DiskCache . TrimCachedFolders ( ) ;
275236
276- responseStream . CopyTo ( memoryStream ) ;
237+ responseStream . CopyTo ( memoryStream ) ;
277238
278- imageFactory . Load ( memoryStream )
279- . AddQueryString ( queryString )
280- . Format ( ImageUtils . GetImageFormat ( imageName ) )
281- . AutoProcess ( ) . Save ( cachedPath ) ;
239+ imageFactory . Load ( memoryStream )
240+ . AddQueryString ( queryString )
241+ . Format ( ImageUtils . GetImageFormat ( imageName ) )
242+ . AutoProcess ( ) . Save ( cachedPath ) ;
282243
283- // Ensure that the LastWriteTime property of the source and cached file match.
284- DateTime dateTime = DiskCache . SetCachedLastWriteTime ( path , cachedPath , true ) ;
244+ // Ensure that the LastWriteTime property of the source and cached file match.
245+ DateTime dateTime = DiskCache . SetCachedLastWriteTime ( path , cachedPath , true ) ;
285246
286- // Add to the cache.
287- DiskCache . AddImageToCache ( cachedPath , dateTime ) ;
247+ // Add to the cache.
248+ DiskCache . AddImageToCache ( cachedPath , dateTime ) ;
288249 //}
289250 }
290251 }
@@ -294,16 +255,16 @@ private void ProcessImage(HttpContext context)
294255 {
295256 //lock (SyncRoot)
296257 //{
297- // Trim the cache.
298- DiskCache . TrimCachedFolders ( ) ;
258+ // Trim the cache.
259+ DiskCache . TrimCachedFolders ( ) ;
299260
300- imageFactory . Load ( fullPath ) . AutoProcess ( ) . Save ( cachedPath ) ;
261+ imageFactory . Load ( fullPath ) . AutoProcess ( ) . Save ( cachedPath ) ;
301262
302- // Ensure that the LastWriteTime property of the source and cached file match.
303- DateTime dateTime = DiskCache . SetCachedLastWriteTime ( path , cachedPath , false ) ;
263+ // Ensure that the LastWriteTime property of the source and cached file match.
264+ DateTime dateTime = DiskCache . SetCachedLastWriteTime ( path , cachedPath , false ) ;
304265
305- // Add to the cache.
306- DiskCache . AddImageToCache ( cachedPath , dateTime ) ;
266+ // Add to the cache.
267+ DiskCache . AddImageToCache ( cachedPath , dateTime ) ;
307268 //}
308269 }
309270 }
0 commit comments