WCF REST JSON Cash Service

I have a WCF web service returning JSON.

[OperationContract] [WebGet(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)] Stream GetStuff(int arg); 

And I use this method to convert the graph of objects to 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)); } 

I would like the answers to be cached in the browser, but the browser still generates If-Modified-Since calls to the server, which are played using 304 Not Modified . I would like the browser to cache and use the response, each time calling an If-Modified-Since call to the server.

I noticed that although I specify Cache-Control "max-age=604800" in the code, the response header sent by WCF is Cache-Control no-cache,max-age=604800 . Why is WCF adding a no-cache part and how can I stop it from adding it?

+4
source share
1 answer

Try setting Cache-Control to "public, max-age = ...". This may prevent WCF from applying the default cache policy header.

There are also so-called "distant future expiration headers." For long-term caching, I use the Expires header instead of Cache-Control: "max-age = ..." and leave Cache-Control only "public."

+2
source

All Articles