Web service caching does not work when a request has a query string

I am trying to implement client-side caching of web service calls and based on information from the Internet, I was able to do this in accordance with the SetCachingPolicy () function, as shown below in code 1.

I managed to successfully get it to work with the RetrieveX web method, but not with the RetrieveY method. I noticed that RetrieveX has no parameters, and RetrieveY has one string parameter, and when checking under Fiddler the difference is that the HTTP GET request to retrieveY the web service has a query string for the parameter.

All HTTP GET HTTP requests so far without a query string are caching correctly, but not this call, which has a query string.

The exam in the Fiddler section indicates that RetrieveX has the following caching information at output 1, and RetrieveY has information at output 2.

Is this a limitation of this caching method or can I do something to make RetrieveY client-side caching work?


Code 1: SetCachingPolicy

private void SetCachingPolicy() { HttpCachePolicy cache = HttpContext.Current.Response.Cache; cache.SetCacheability(HttpCacheability.Private); cache.SetExpires(DateTime.Now.AddSeconds((double)30)); FieldInfo maxAgeField = cache.GetType().GetField( "_maxAge", BindingFlags.Instance | BindingFlags.NonPublic); maxAgeField.SetValue(cache, new TimeSpan(0, 0, 30)); } 

Code 2: RetrieveX

 [System.Web.Services.WebMethod] [System.Web.Script.Services.ScriptMethod(UseHttpGet = true)] public string[] RetrieveX() { SetCachingPolicy(); // Implementation details here. return array; } 

Code 3: RetrieveY

 [System.Web.Services.WebMethod] [System.Web.Script.Services.ScriptMethod(UseHttpGet = true)] public string[] RetrieveY(string arg1) { SetCachingPolicy(); // Implementation details here. return array; } 

Output 1: RetrieveX Caching Information

 HTTP/200 responses are cacheable by default, unless Expires, Pragma, or Cache-Control headers are present and forbid caching. HTTP/1.0 Expires Header is present: Wed, 12 Sep 2012 03:16:50 GMT HTTP/1.1 Cache-Control Header is present: private, max-age=30 private: This response MUST NOT be cached by a shared cache. max-age: This resource will expire in .5 minutes. [30 sec] 

Output 2: RetrieveY Caching Information

 HTTP/200 responses are cacheable by default, unless Expires, Pragma, or Cache-Control headers are present and forbid caching. HTTP/1.0 Expires Header is present: -1 Legacy Pragma Header is present: no-cache HTTP/1.1 Cache-Control Header is present: no-cache no-cache: This response MUST NOT be reused without successful revalidation with the origin server. 
+4
source share
1 answer

I ran into this problem, I thought I would share what worked for me. The main problem is that VaryByParams is not set in the response. If you add this to your SetCachingPolicy () method, RetrieveY should start working as expected:

 cache.VaryByParams["*"] = true 
+1
source

Source: https://habr.com/ru/post/1433704/


All Articles