c# – WCF REST JSON服务缓存
发布时间:2020-12-15 17:17:28 所属栏目:百科 来源:网络整理
导读:我有一个返回 JSON的WCF Web服务. [OperationContract][WebGet(BodyStyle = WebMessageBodyStyle.Bare,ResponseFormat = WebMessageFormat.Json)]Stream GetStuff(int arg); 我正在使用此方法将对象图转换为JSON: private static Stream ToJson(object obj)
我有一个返回
JSON的WCF Web服务.
[OperationContract] [WebGet(BodyStyle = WebMessageBodyStyle.Bare,ResponseFormat = WebMessageFormat.Json)] Stream GetStuff(int arg); 我正在使用此方法将对象图转换为JSON: private static Stream ToJson(object obj) { JavaScriptSerializer serializer = new JavaScriptSerializer(); string json = serializer.Serialize(obj); if (WebOperationContext.Current != null) { OutgoingWebResponseContext outgoingResponse = WebOperationContext.Current.OutgoingResponse; outgoingResponse.ContentType = "application/json; charset=utf-8"; outgoingResponse.Headers.Add(HttpResponseHeader.CacheControl,"max-age=604800"); // one week outgoingResponse.LastModified = DateTime.Now; } return new MemoryStream(Encoding.UTF8.GetBytes(json)); } 我希望将响应缓存在浏览器上,但浏览器仍在生成If-Modified-Since对服务器的调用,这些调用将使用304 Not Modified重播.我希望浏览器缓存并使用响应,而不是每次都对服务器进行If-Modified-Since调用. 我注意到,即使我在代码中指定了Cache-Control“max-age = 604800”,WCF发送的响应头是Cache-Control no-cache,max-age = 604800.为什么WCF添加“无缓存”部分,如何阻止它添加? 解决方法
尝试将Cache-Control设置为“public,max-age = …”.这可能会阻止WCF应用默认缓存策略标头.
此外,还有所谓的’远期未来过期标题’.对于繁重的长期缓存,我使用Expires头而不是Cache-Control:’max-age = …’并将Cache-Control保留为“public”. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读