locked
XRM caching problem RRS feed

  • Question

  • We are trying to implement xrm that came with new crm sdk but there is a caching problem with that. Does any body know any work around for this?

     

    For example: I am showing the contact attributes on a web form using XRM sdk. When I go back to CRM and change the values they are not getting reflected in my web form.


    Santosh Kore CRM Developer
    Tuesday, July 13, 2010 12:45 AM

Answers

  • When I was looking through these forums I found the below code at this link http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/3d4a2501-3d02-4f39-bae7-790db0c0e633 and this seems to be working. But this way I need to call this method for each and every entity. If anybody had already implemented this please let me know.

    public static void ClearCache(string entityName)
           {
               const string format = "adxdependency:crm:entity:{0}";
               var dependency = string.Format(format, entityName).ToLower();

               var cache = Microsoft.Xrm.Client.Caching.CacheManager.GetBaseCache();
               cache.Remove(dependency);
           }

    Santosh Kore CRM Developer
    Tuesday, July 13, 2010 12:52 AM

All replies

  • When I was looking through these forums I found the below code at this link http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/3d4a2501-3d02-4f39-bae7-790db0c0e633 and this seems to be working. But this way I need to call this method for each and every entity. If anybody had already implemented this please let me know.

    public static void ClearCache(string entityName)
           {
               const string format = "adxdependency:crm:entity:{0}";
               var dependency = string.Format(format, entityName).ToLower();

               var cache = Microsoft.Xrm.Client.Caching.CacheManager.GetBaseCache();
               cache.Remove(dependency);
           }

    Santosh Kore CRM Developer
    Tuesday, July 13, 2010 12:52 AM
  • Not sure if this is of any use to you or not but this blog explains the limitations with XRM Caching in latest SDK

    http://www.xrmlinq.com/crm-sdk-4-0-12-bugs-limitations/


    MSCRM Developer
    Tuesday, July 13, 2010 10:51 AM
  • You can also disable caching in the web.config

    <microsoft.xrm.client>
      <cache defaultProvider="InMemoryCachingProvider">
       <providers>
        <add name="InMemoryCachingProvider" type="Microsoft.Xrm.Client.Caching.InMemoryCacheProvider, Microsoft.Xrm.Client" absoluteExpiration="00:00:00" />
       </providers>
      </cache>
      <contexts default="Crm">
       <add name="Crm" type="Ideaca.Ta.Xrm.IdeacaDataContext" />
      </contexts>
     </microsoft.xrm.client>
    
    or write your own caching provider, as disabling the cache really slows down xrm.
    Thursday, October 28, 2010 9:29 PM
  • I ended up rolling my own anti-caching mechanism, based on what I'd found around the forums:

    Web.config snippet:

    	<appSettings>
    		<add key="Cache.Removal" value="entity"/><!-- all or entity -->
    		<add key="Cache.Removal.Entities" value="incident,contact,activity"/>
    	</appSettings>
    
    

    Global.asax.cs snippet:

    	public static class Extensions
    	{
    		public static void RemoveAll(this Microsoft.Xrm.Client.Caching.BaseCache cache)
    		{
    			foreach (KeyValuePair<string, object> pair in (IEnumerable<KeyValuePair<string, object>>)cache)
    			{
    				cache.Remove(pair.Key);
    			}
    		}
    	}
    
    	public class Global : System.Web.HttpApplication
    	{
    		#region Helper Routines
    		private static void ClearCache(string entityName)
    		{
    			var dependency = string.Format("adxdependency:crm:entity:{0}", entityName).ToLower();
    
    			var cache = Microsoft.Xrm.Client.Caching.CacheManager.GetBaseCache();
    			cache.Remove(dependency);
    		}
    		private static void ClearCache()
    		{
    			Microsoft.Xrm.Client.Caching.CacheManager.GetBaseCache().RemoveAll();
    		}
    		#endregion
    
    		protected void Application_BeginRequest(object sender, EventArgs e)
    		{
    			string cacheRemoval = System.Configuration.ConfigurationManager.AppSettings["Cache.Removal"];
    
    			switch (cacheRemoval.ToLower())
    			{
    				case "all":
    					// clear all cache items for the following entities
    					ClearCache();
    					break;
    
    				case "entity":
    					string[] entities = System.Configuration.ConfigurationManager.AppSettings["Cache.Removal.Entities"].Split(new char[] { ',' });
    					foreach (string entity in entities)
    					{
    						ClearCache(entity);
    					}
    					break;
    			}
    		}
    	}
    
    This allows you to control the anti-caching, for either "all" entities, or those you specify.
    --pogo (pat)
    Thursday, October 28, 2010 10:30 PM
  • Hello.
    This is not the best solution, because when two or more users make the same query , and then try to clear the cache, as a result an error occured:
    System.UnauthorizedAccessException:
    Message: Access to the path 'appDomain=/LM/W3SVC/2/Root/...:key=Microsoft.Xrm.Client.Services.InMemoryCrmCacheProvider:1177205267:User=00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000:Query=-107068185' is denied.
    That's because user2 clears the cache for user1.
    So, the solution is:
    1) When you use Xrm client, after update/create/delete operations, cache will be cleaned automatically. And you don't need to use your custom mechanism of cleaning cache.
    2) If you don't need in caching your queries, you can turn it off (disabled XRM caching). In XRM SDK this solution is described. Do the following:
             CrmConnection xrmConnection = CrmConnection.Parse(Configuration.AppSettings["XrmConnectiontWithImpersonate"]); // use your own connection string, for example like this "Authentication Type=Integrated; Server=http://crmserver:<port>/orgname/"
             DataContext xrmDataContextWithImpersonate_CacheDisabled = new DataContext("CacheDisabled", () => new OrganizationService("CacheDisabled", xrmConnection));
    Best Regards, Alexey.
    Monday, October 24, 2011 7:30 AM