I would like to implement an internal Counter for dynamics crm plugins. But I'm not sure wether this is a good idea or not regarding multi threading, etc.
Would be like this:
sealed class Counter
{
private int current = 0;
public int NextValue()
{
return Interlocked.Increment(ref this.current);
}
public void Reset()
{
this.current = 0;
}
}
maybe this could be an issue in Dynamics plugins?
Thank you for your thoughts on this.
It isn't clear what's the purpose of this counter. Is it some business logic or it just to have statistics?
If you need counter for entity numbering, then OOB feature may be used https://learn.microsoft.com/en-us/dynamics365/customerengagement/on-premises/developer/create-auto-number-attributes
D365 has distributed deployments. Same plugin may be run on FE, Async or Sandbox app domains and usually each of them has several instances. So if you really like to write your own distributed counter, you will have to make it work on any number of different app domains. Multithreading can't help here. All instances will have to rely on single source of truth. This will have to be some external service or organization DB.
Some devs already investigated this problem and wrote articles with code snippets:
P.S. Also there are commercial solutions for autonumbering\counters on the market.