Whenever you change a layer in the DB or publish a new server you might notice that the REST API will not automatically update. In order for it to update you might need to clear the REST API cache.
There are several ways to do so:
The default way:
ArcGis Server comes with a REST API admin page, which can be found here:
http://SERVERNAME/ArcGIS/rest/admin
Logging in to this page gives you the Clear Cache Options:
The page has a bunch of options for clearing the catch semi-automatically:
What you need to do is simply click on the "Clear Cache Now" several times (at some version of the Server one time was not enough…).
Disadvantages:
1. You need to have the admin user/password in order to clear the cache
2. The process needs to be done manually.
3. If you have multiple server you will have to do so for each server
The token way:
ESRI gives another way which is to use a generated token and then you can simply call a link whenever you want to clear the cache. More on that here (ESRI refers to this method as “the easy way”).
Disadvantages:
1. Different token for each server
2. The process needs to be manually set at each server
With Console Application (with code):
The problem with all of these methods is that you can’t really use them to install servers, at my company we have more servers then flies and having to clear the cache after each installation is tedious work that is often forgotten (and when it is forgotten I find myself wasting hours trying to debug the “problem”). So I decided to write a console application that does the clearing of the cache for me. This idea actually came from this forum post, the only problem was that the code didn’t really work for me…
So I tweaked it a bit with WebRequest and the end result is this:
- public static string ClearCache(string serverName, string userName, string password)
- {
- var result = HttpUtils.PostRequest("http://" + serverName + "/ArcGIS/rest/admin/login",
- new List<KeyValuePair<string, string>>
- {
- new KeyValuePair<string, string>("username", userName),
- new KeyValuePair<string, string>("password", password),
- new KeyValuePair<string, string>("redirect", String.Format("http%3a%2f%2f{0}%2fArcGIS%2frest%2fadmin%2fcache%2fclear", serverName)),
- }, String.Format("http://srvp7d-gtm/ArcGIS/rest/admin/login?redirect=http%3a%2f%2f{0}%2fArcGIS%2frest%2fadmin%2fcache%2fclear", serverName));
- //The response is in html, only return the response if its not a success
- return result.Contains("Cache Cleared.") ? "Cache Cleared." : result;
- }
And HttpUtils.PostRequest looks like:
- public static string PostRequest(string url, List<KeyValuePair<string, string>> paramters, string referer)
- {
- var req = WebRequest.Create(url);
- req.ContentType = "application/x-www-form-urlencoded";
- req.Method = "POST";
- var parameters = GetParameters(paramters);
- //We need to count how many bytes we're sending.
- var bytes = Encoding.ASCII.GetBytes(parameters);
- req.ContentLength = bytes.Length;
- ((HttpWebRequest)req).CookieContainer = new CookieContainer();
- if (!String.IsNullOrEmpty(referer))
- ((HttpWebRequest)req).Referer = referer;
- var os = req.GetRequestStream();
- os.Write(bytes, 0, bytes.Length); //Push it out there
- os.Close();
- var resp = req.GetResponse();
- if (resp == null)
- return null;
- var sr = new StreamReader(resp.GetResponseStream());
- return sr.ReadToEnd().Trim();
- }
I think that this way is the best since all I need is the server name, the admin user and it’s password. I can do it remotely and I can do it automatically after each deploy. The only thing that is not so good is the using of the clear password (and lets be honest here the next step is putting the call to the console application in a batch file for the development environment and giving that file to the rest of the team so that a hacker…).
Source code can be found at my Codeplex project, here (the code has a few more useful commands like publishing a MSD file, changing the connection of MSD file etc.).
Resources:
Clearing the ArcGIS Services Directory cache “the easy way” – ArcGIS Server Blog
Clear cache through code by demand – ArcGis Server Forum
Hanselman: HTTP POSTs and HTTP GETs with WebClient and C# and Faking a PostBack
Keywords: ESRI,ArcGIS Server,REST API