Skip to content

Commit 15febdb

Browse files
committed
removed the hack around async filter
1 parent 71bb828 commit 15febdb

1 file changed

Lines changed: 3 additions & 65 deletions

File tree

src/WebApi.OutputCache.V2/CacheOutputAttribute.cs

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
namespace WebApi.OutputCache.V2
1919
{
2020
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
21-
public class CacheOutputAttribute : FilterAttribute, IActionFilter
21+
public class CacheOutputAttribute : ActionFilterAttribute
2222
{
2323
protected static MediaTypeHeaderValue DefaultMediaType = new MediaTypeHeaderValue("application/json") {CharSet = Encoding.UTF8.HeaderName};
2424

@@ -125,7 +125,7 @@ protected virtual MediaTypeHeaderValue GetExpectedMediaType(HttpConfiguration co
125125
return responseMediaType;
126126
}
127127

128-
private void OnActionExecuting(HttpActionContext actionContext)
128+
public override void OnActionExecuting(HttpActionContext actionContext)
129129
{
130130
if (actionContext == null) throw new ArgumentNullException("actionContext");
131131

@@ -175,7 +175,7 @@ private void OnActionExecuting(HttpActionContext actionContext)
175175
ApplyCacheHeaders(actionContext.Response, cacheTime);
176176
}
177177

178-
private async Task OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
178+
public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
179179
{
180180
if (actionExecutedContext.ActionContext.Response == null || !actionExecutedContext.ActionContext.Response.IsSuccessStatusCode) return;
181181

@@ -257,67 +257,5 @@ private static void SetEtag(HttpResponseMessage message, string etag)
257257
message.Headers.ETag = eTag;
258258
}
259259
}
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-
322260
}
323261
}

0 commit comments

Comments
 (0)