I’ve tried applying several suggestions found on the web, but still experiencing cached results when querying CRM 2011 with linq. My web.config reads as follows, which is supposed to disable result caching:
<configSections>
<section name="microsoft.xrm.client" type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client"/>
</configSections>
<connectionStrings>
...
<connectionStrings>
<microsoft.xrm.client>
<contexts>
<add name="Xrm" type="Xrm.XrmServiceContext, Xrm" serviceName="Xrm"/>
</contexts>
<services>
<add name="Xrm" type="Microsoft.Xrm.Client.Services.OrganizationService, Microsoft.Xrm.Client"/>
</services>
</microsoft.xrm.client>
In code, I have a little test loop to wait for an external change to some data:
Dim crm As New XrmServiceContext("Xrm")
Dim oOpptyGuid = ' <an existing GUID in the system>
' Get opportunity by guid.
Dim oOppty As Xrm.Opportunity = (From c In crm.OpportunitySet Where c.Id.Equals(oOpptyGuid) Select c).SingleOrDefault
Dim sName As String = oOppty.Name
Dim iTries As Int16 = 0
' Wait till name is changed or tried too many times.
Do
' Sleep between tries.
Threading.Thread.Sleep(10000)
iTries += 1
' Get opportunity by guid.
oOppty = (From c In crm.OpportunitySet Where c.Id.Equals(oOpptyGuid) Select c).SingleOrDefault
Loop Until oOppty.Name <> sName Or iTries > 10
The above loop never detects when the name is changed elsewhere in the CRM. I’ve tried removing items from the cache manually, before the query in the loop, but with no joy:
oCacheManager = Microsoft.Xrm.Client.Caching.ObjectCacheManager.GetInstance()
For Each x As String In From y In oCacheManager Select y.Key
oCacheManager.Remove(x)
Next
The only thing that works for me is this:
crm.Dispose()
crm = New XrmServiceContext("Xrm")
I can live with that, but it would be nicer, instead of recreating the context, to have a way of ensuring no caching either in code or in web.config. But I can’t find a solution anywhere that works for me. Am I missing something?
I don’t think your issue is caching of the
OrganizationService. I believe your issue is related to the service context tracking the status of the selected entity. If you callIOrganizationServiceContext.Detachon the entity that you are selecting, it will no longer be tracked by the context and a retrieve should return most recent data from the service.