|
18 | 18 | namespace WebApi.OutputCache.V2 |
19 | 19 | { |
20 | 20 | [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] |
21 | | - public class CacheOutputAttribute : FilterAttribute, IActionFilter |
| 21 | + public class CacheOutputAttribute : ActionFilterAttribute |
22 | 22 | { |
23 | 23 | protected static MediaTypeHeaderValue DefaultMediaType = new MediaTypeHeaderValue("application/json") {CharSet = Encoding.UTF8.HeaderName}; |
24 | 24 |
|
@@ -125,7 +125,7 @@ protected virtual MediaTypeHeaderValue GetExpectedMediaType(HttpConfiguration co |
125 | 125 | return responseMediaType; |
126 | 126 | } |
127 | 127 |
|
128 | | - private void OnActionExecuting(HttpActionContext actionContext) |
| 128 | + public override void OnActionExecuting(HttpActionContext actionContext) |
129 | 129 | { |
130 | 130 | if (actionContext == null) throw new ArgumentNullException("actionContext"); |
131 | 131 |
|
@@ -175,7 +175,7 @@ private void OnActionExecuting(HttpActionContext actionContext) |
175 | 175 | ApplyCacheHeaders(actionContext.Response, cacheTime); |
176 | 176 | } |
177 | 177 |
|
178 | | - private async Task OnActionExecuted(HttpActionExecutedContext actionExecutedContext) |
| 178 | + public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken) |
179 | 179 | { |
180 | 180 | if (actionExecutedContext.ActionContext.Response == null || !actionExecutedContext.ActionContext.Response.IsSuccessStatusCode) return; |
181 | 181 |
|
@@ -257,67 +257,5 @@ private static void SetEtag(HttpResponseMessage message, string etag) |
257 | 257 | message.Headers.ETag = eTag; |
258 | 258 | } |
259 | 259 | } |
260 | | - |
261 | | - Task<HttpResponseMessage> IActionFilter.ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation) |
262 | | - { |
263 | | - if (actionContext == null) |
264 | | - { |
265 | | - throw new ArgumentNullException("actionContext"); |
266 | | - } |
267 | | - |
268 | | - if (continuation == null) |
269 | | - { |
270 | | - throw new ArgumentNullException("continuation"); |
271 | | - } |
272 | | - |
273 | | - OnActionExecuting(actionContext); |
274 | | - |
275 | | - if (actionContext.Response != null) |
276 | | - { |
277 | | - return Task.FromResult(actionContext.Response); |
278 | | - } |
279 | | - |
280 | | - return CallOnActionExecutedAsync(actionContext, cancellationToken, continuation); |
281 | | - } |
282 | | - |
283 | | - private async Task<HttpResponseMessage> CallOnActionExecutedAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation) |
284 | | - { |
285 | | - cancellationToken.ThrowIfCancellationRequested(); |
286 | | - |
287 | | - HttpResponseMessage response = null; |
288 | | - Exception exception = null; |
289 | | - try |
290 | | - { |
291 | | - response = await continuation(); |
292 | | - } |
293 | | - catch (Exception e) |
294 | | - { |
295 | | - exception = e; |
296 | | - } |
297 | | - |
298 | | - try |
299 | | - { |
300 | | - var executedContext = new HttpActionExecutedContext(actionContext, exception) { Response = response }; |
301 | | - await OnActionExecuted(executedContext); |
302 | | - |
303 | | - if (executedContext.Response != null) |
304 | | - { |
305 | | - return executedContext.Response; |
306 | | - } |
307 | | - |
308 | | - if (executedContext.Exception != null) |
309 | | - { |
310 | | - ExceptionDispatchInfo.Capture(executedContext.Exception).Throw(); |
311 | | - } |
312 | | - } |
313 | | - catch (Exception e) |
314 | | - { |
315 | | - actionContext.Response = null; |
316 | | - ExceptionDispatchInfo.Capture(e).Throw(); |
317 | | - } |
318 | | - |
319 | | - throw new InvalidOperationException(GetType().Name); |
320 | | - } |
321 | | - |
322 | 260 | } |
323 | 261 | } |
0 commit comments