Duncan Watts Duncan Watts - 1 month ago 8
C# Question

FetchExpression results appear to be cached, how do I prevent this?

I am using a

FetchExpression
against a
RetrieveMultiple
operation on a
CrmOrganizationServiceContext
within a windows service to fetch and process items out of a queue.

The first time this runs this fetches the items to be processed correctly. On subsequent calls using the same
CrmOrganizationServiceContext
instance it always retrieves zero entities with no errors thrown. I have added in new entities and reactivated existing ones that should be fetched using the FetchXml and they aren't retrieved.

As soon as I restart my service it creates a new instance of the
CrmOrganizationServiceContext
and fetches the new items.

What am I doing wrong here?

public CrmConnector(string connectionString)
{
Context = new CrmOrganizationServiceContext(CrmConnection.Parse(connectionString));
}

public void FetchStuff()
{
string fetchXml = "...";
FetchExpression fetchExpression = new FetchExpression(fetchXml);
EntityCollection entityCollection = Context.RetrieveMultiple(fetchExpression);
// entityCollection.Entities is always empty following first run
}

private CrmOrganizationServiceContext Context { get; set; }


Fetch Xml as requested, the only customisation is the count attribute which limits the number of items being returned (as this is a queue processor)

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" count="10">
<entity name="xxx1">
<attribute name="xxx_name" />
<attribute name="createdon" />
<attribute name="xxx_1" />
<attribute name="xxx_2" />
<attribute name="xxx_3" />
<attribute name="xxx_4" />
<attribute name="statecode" />
<order attribute="createdon" descending="false" />
<filter type="and">
<condition attribute="xxx_exported" value="0" operator="eq"/>
</filter>
</entity>
</fetch>

Answer

It is the CrmOrganizationServiceContext that is doing the caching - I found the following worked a treat and the results of my RetrieveMultiple are no longer cached :)

Context = new CrmOrganizationServiceContext(CrmConnection.Parse(connectionString));
Context.TryAccessCache(cache => cache.Mode = OrganizationServiceCacheMode.Disabled);