2

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>
Duncan Watts
  • 1,331
  • 9
  • 26
  • I had done something similiar and had the same kind of issues though in my case, I changed my service so that each time it was polled to run ( something like each 10 minutes ) I re-instantiated the context and associated classes to get a fresh start, is that an option? – Chris Dec 08 '11 at 15:23
  • For the sake of completeness, could you post your `FetchXml`? – Peter Majeed Dec 08 '11 at 15:25
  • 1
    @Chris - I would prefer not to as it takes bloody ages to create the instance each time :( – Duncan Watts Dec 08 '11 at 15:41
  • 2
    And yes, as I tell my colleagues, `bloody ages` is a technical term :) – Duncan Watts Dec 08 '11 at 15:50
  • @Chris have tried recreating the instance each time and it still doesn't pick up the entity so something else going on... – Duncan Watts Dec 08 '11 at 15:52
  • @DuncanWatts does the same behaviour occurr if you change from using fetch xml and use say, a query expression or linq? (I only ask as If i remember rightly, You can pass other parameters other than a fetch expression to a retrieve multiple request) If this works as anticipated it could be more to do with the fetch xml caching the results on retrieve rather than say the context. – Chris Dec 08 '11 at 16:08
  • I'm currently using FetchXml as I'm using late bound entities, however I've just discovered the `ICodeWriterFilterService` so I am going to attempt to do the same using early bound entities without needing the 6mb cs file. – Duncan Watts Dec 08 '11 at 16:33
  • I've altered my FetchXml to add in a filter on the createdon where the milliseconds component is a random number to try and prevent the caching and this appears to be working and new items are now found. This will have to suffice until I find out how to disable the caching. – Duncan Watts Dec 12 '11 at 09:07

1 Answers1

3

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);
Duncan Watts
  • 1,331
  • 9
  • 26