From 3ebc62338baa9ccb3938d22528a327c631711379 Mon Sep 17 00:00:00 2001 From: Michael Denny Date: Mon, 7 Mar 2016 17:48:02 +0100 Subject: [PATCH] cache the string rappresentation of MediaTypeHeaderValue instead of caching the framework object that is not-serializable --- src/WebApi.OutputCache.V2/CacheOutputAttribute.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/WebApi.OutputCache.V2/CacheOutputAttribute.cs b/src/WebApi.OutputCache.V2/CacheOutputAttribute.cs index ee6fbca..dc69752 100644 --- a/src/WebApi.OutputCache.V2/CacheOutputAttribute.cs +++ b/src/WebApi.OutputCache.V2/CacheOutputAttribute.cs @@ -176,12 +176,17 @@ public override void OnActionExecuting(HttpActionContext actionContext) var val = _webApiCache.Get(cachekey); if (val == null) return; - var contenttype = _webApiCache.Get(cachekey + Constants.ContentTypeKey) ?? new MediaTypeHeaderValue(cachekey.Split(new[] { ':' }, 2)[1].Split(';')[0]); + MediaTypeHeaderValue contentType; + var cachedContentType = _webApiCache.Get(cachekey + Constants.ContentTypeKey); + if (!MediaTypeHeaderValue.TryParse(cachedContentType, out contentType)) + { + contentType = new MediaTypeHeaderValue(cachekey.Split(new[] { ':' }, 2)[1].Split(';')[0]); + } actionContext.Response = actionContext.Request.CreateResponse(); actionContext.Response.Content = new ByteArrayContent(val); - actionContext.Response.Content.Headers.ContentType = contenttype; + actionContext.Response.Content.Headers.ContentType = contentType; var responseEtag = _webApiCache.Get(cachekey + Constants.EtagKey); if (responseEtag != null) SetEtag(actionContext.Response, responseEtag); @@ -214,7 +219,7 @@ public override async Task OnActionExecutedAsync(HttpActionExecutedContext actio if (responseContent != null) { var baseKey = config.MakeBaseCachekey(actionExecutedContext.ActionContext.ControllerContext.ControllerDescriptor.ControllerType.FullName, actionExecutedContext.ActionContext.ActionDescriptor.ActionName); - var contentType = responseContent.Headers.ContentType; + var contentType = responseContent.Headers.ContentType.ToString(); string etag = actionExecutedContext.Response.Headers.ETag.Tag; //ConfigureAwait false to avoid deadlocks var content = await responseContent.ReadAsByteArrayAsync().ConfigureAwait(false);