在WebApi的ActionFilter中读取ResponseContent内容遇到的问题

2019年11月10日 阅读数:22
这篇文章主要向大家介绍在WebApi的ActionFilter中读取ResponseContent内容遇到的问题,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

最近用ActionFilter给REST Api加入本地缓存功能,在OnActionExecutedAsync重写中,须要将缓存对象的内容以byte[]的形式存入缓存,并缓存Etag、ContentType信息。web

而在该方法中以api

var content = await responseContent.ReadAsByteArrayAsync().ConfigureAwait(false);

获取byte[]形式的响应内容时,提示Cannot access a closed Stream.即认为响应流已关闭,但其实上下文中并未显示关闭Stream对象或以using操做流对象。缓存

 

为了验证,一样的代码在一个新建的web api2项目中使用,则可以正常获取响应内容,查了好久未果。。。。async

如下是上下文代码:ide

 1 public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
 2         {
 3             if (actionExecutedContext.ActionContext.Response == null
 4                 || !actionExecutedContext.ActionContext.Response.IsSuccessStatusCode)
 5                 return;
 6             if (!IsCachingAllowed(actionExecutedContext.ActionContext))
 7                 return;
 8             var cacheTime = CacheTimeQuery.Execute(DateTime.Now);
 9             if (cacheTime.AbsoluteExpiration > DateTime.Now)
10             {
11                 var httpConfig = actionExecutedContext.Request.GetConfiguration();
12                 var config = httpConfig.RestCacheConfiguration();
13                 var cacheKeyGenerator = config.GetCacheKeyGenerator(CacheKeyGenerator);
14                 var responseMediaType = actionExecutedContext.Request.Properties[CurrentRequestMediaType] as MediaTypeHeaderValue
15                     ?? GetExpectedMediaType(httpConfig, actionExecutedContext.ActionContext);
16                 var cachekey = cacheKeyGenerator.MakeCacheKey(actionExecutedContext.ActionContext, responseMediaType);
17                 if (!string.IsNullOrWhiteSpace(cachekey) && !(_restCache.Contains(cachekey)))
18                 {
19                     SetEtag(actionExecutedContext.Response, CreateEtag(actionExecutedContext, cachekey, cacheTime));
20                     var responseContent = actionExecutedContext.Response.Content;
21                     if (responseContent != null)
22                     {
23                         var baseKey = config.MakeBaseCachekey(actionExecutedContext.ActionContext.ControllerContext.ControllerDescriptor.ControllerType.FullName, actionExecutedContext.ActionContext.ActionDescriptor.ActionName);
24                         var contentType = responseContent.Headers.ContentType;
25                         string etag = actionExecutedContext.Response.Headers.ETag.Tag;
26                         try
27                         {
28                             var content = await responseContent.ReadAsByteArrayAsync().ConfigureAwait(false);
29                             responseContent.Headers.Remove("Content-Length");
30                             _restCache.Add(baseKey, string.Empty, cacheTime.AbsoluteExpiration);
31                             _restCache.Add(cachekey, content, cacheTime.AbsoluteExpiration, baseKey);
32                             _restCache.Add(cachekey + Constants.ContentTypeKey,
33                                 contentType,
34                                 cacheTime.AbsoluteExpiration, baseKey);
35                             _restCache.Add(cachekey + Constants.EtagKey,
36                                 etag,
37                                 cacheTime.AbsoluteExpiration, baseKey);
38                         }
39                         catch (Exception exp)
40                         {
41                             //捕捉到Cannot access a closed Stream.
42                             throw;
43                         }
44                     }
45                 }
46             }
47             ApplyCacheHeaders(actionExecutedContext.ActionContext.Response, cacheTime);
48         }
View Code

 


 

 

既然想以byte[]的形式缓存响应内容失效,只能另想办法,所以,直接缓存response content对象,在OnActionExecuting方法中取出该ObjectContent对象,再构建一个相同对象做为response content便可,如下为代码:ui

OnActionExecuting:spa

public override void OnActionExecuting(HttpActionContext actionContext)
        {
            if (actionContext == null) throw new ArgumentNullException("actionContext");
            if (!IsCachingAllowed(actionContext)) return;
            var config = actionContext.Request.GetConfiguration();
            EnsureCacheTimeQuery();
            EnsureCache(config);
            var cacheKeyGenerator = config.RestCacheConfiguration().GetCacheKeyGenerator(CacheKeyGenerator);
            var responseMediaType = GetExpectedMediaType(config, actionContext);
            actionContext.Request.Properties[CurrentRequestMediaType] = responseMediaType;
            var cachekey = cacheKeyGenerator.MakeCacheKey(actionContext, responseMediaType);
            if (!_restCache.Contains(cachekey))
                return;
            if (actionContext.Request.Headers.IfNoneMatch != null)
            {
                var etag = _restCache.Get<string>(cachekey + Constants.EtagKey);
                if (etag != null)
                {
                    if (actionContext.Request.Headers.IfNoneMatch.Any(x => x.Tag == etag))
                    {
                        var time = CacheTimeQuery.Execute(DateTime.Now);
                        var quickResponse = actionContext.Request.CreateResponse(HttpStatusCode.NotModified);
                        ApplyCacheHeaders(quickResponse, time);
                        actionContext.Response = quickResponse;
                        return;
                    }
                }
            }
            var val = _restCache.Get<ObjectContent>(cachekey);
            if (val == null)
                return;
            var contenttype = _restCache.Get<MediaTypeHeaderValue>(cachekey + Constants.ContentTypeKey) ?? responseMediaType;
            actionContext.Response = actionContext.Request.CreateResponse();
            var newContent = new ObjectContent<object>(val.Value, val.Formatter);
            actionContext.Response.Content = newContent;
            actionContext.Response.Content.Headers.ContentType = contenttype;
            var responseEtag = _restCache.Get<string>(cachekey + Constants.EtagKey);
            if (responseEtag != null)
                SetEtag(actionContext.Response, responseEtag);
            var cacheTime = CacheTimeQuery.Execute(DateTime.Now);
            ApplyCacheHeaders(actionContext.Response, cacheTime);
        }
View Code

 

OnActionExecutedAsync:3d

public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
        {
            if (actionExecutedContext.ActionContext.Response == null
                || !actionExecutedContext.ActionContext.Response.IsSuccessStatusCode)
                return;
            if (!IsCachingAllowed(actionExecutedContext.ActionContext))
                return;
            var cacheTime = CacheTimeQuery.Execute(DateTime.Now);
            if (cacheTime.AbsoluteExpiration > DateTime.Now)
            {
                var httpConfig = actionExecutedContext.Request.GetConfiguration();
                var config = httpConfig.RestCacheConfiguration();
                var cacheKeyGenerator = config.GetCacheKeyGenerator(CacheKeyGenerator);
                var responseMediaType = actionExecutedContext.Request.Properties[CurrentRequestMediaType] as MediaTypeHeaderValue
                    ?? GetExpectedMediaType(httpConfig, actionExecutedContext.ActionContext);
                var cachekey = cacheKeyGenerator.MakeCacheKey(actionExecutedContext.ActionContext, responseMediaType);
                if (!string.IsNullOrWhiteSpace(cachekey) && !(_restCache.Contains(cachekey)))
                {
                    SetEtag(actionExecutedContext.Response, CreateEtag(actionExecutedContext, cachekey, cacheTime));
                    var responseContent = actionExecutedContext.Response.Content;
                    if (responseContent != null)
                    {
                        var baseKey = config.MakeBaseCachekey(actionExecutedContext.ActionContext.ControllerContext.ControllerDescriptor.ControllerType.FullName, actionExecutedContext.ActionContext.ActionDescriptor.ActionName);
                        var contentType = responseContent.Headers.ContentType;
                        string etag = actionExecutedContext.Response.Headers.ETag.Tag;
                        try
                        {
                            responseContent.Headers.Remove("Content-Length");
                            _restCache.Add(baseKey, string.Empty, cacheTime.AbsoluteExpiration);
                            _restCache.Add(cachekey, responseContent, cacheTime.AbsoluteExpiration, baseKey);
                            _restCache.Add(cachekey + Constants.ContentTypeKey,
                                contentType,
                                cacheTime.AbsoluteExpiration, baseKey);
                            _restCache.Add(cachekey + Constants.EtagKey,
                                etag,
                                cacheTime.AbsoluteExpiration, baseKey);
                        }
                        catch (Exception exp)
                        {
                            //捕捉到Cannot access a closed Stream.
                            throw;
                        }
                    }
                }
            }
            ApplyCacheHeaders(actionExecutedContext.ActionContext.Response, cacheTime);
        }
View Code

 


 

相同的代码在不一样上下文中对流操做表现不一样,该问题还未找到缘由,有待排查。rest