Search code examples
c#asp.net-web-apirabbitmqdynamics-crmservice-broker

How handle auto generated IDs when load leveling via queue message?


We have an asp.net core Web Api with MS Dynamics CRM as database, we want to implement queue-messaging to gain better performance, our choice is RabbitMQ.

We have many cases where we write in the CRM, but the ID of a new CRM entity record is an auto generated GUID. This new GUIDs will be used in other request like: lookup in other entities, update this record..

Let's say we have 3 entities:

Topics            [ID(auto-generated), Name]
UserTopic         [ID(auto-generated), UserId, TopicId]

UserComment [ID(auto-generated), UserTopicId(1), Comment]

When user subscribe for a topic we should do the following at the Web API level:

 1- Send a message to the queue of register in topic
 2- Add an object UserTopic to the cache (with an empty or auto-generated GUID)

So the message handler will insert a new record in the UserTopic entity where CRM will generate a new GUID(2) which the Web API don't know about it.

If the user needs add a comment for this Topic we need the GUID(2) to inserted the entity UserComment (1).

So how to handle such auto-generated GUID created by CRM in the message handler?


Solution

  • Basically you have three different options

    1. Like suggested in the commments, you could retrieve the id which was assigned during the creation of your records. This would require a RPC pattern in your messaging, which contradicts the nature of async messaging

    2. You could define the id in the create request. The id which you assign in either the constructor of an entity or the id property is then used to create the record in CRM.

      public Entity (string logicalName, Guid id) {…}

      You have to ensure that the passed id is not yet used for this entity

    3. You could make use alternate keys instead on relying on the primary keys of CRM. Therefore you could use an identifier of your web api application to identify corresponding records in CRM.

    You will find further information for 2. and 3. at Use an alternate key to create a record